Skip to content

Commit 4e3c87e

Browse files
authored
Fix false positive in empty_enum_arguments (realm#3616)
Fixes realm#3562
1 parent a10158c commit 4e3c87e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
#### Bug Fixes
2424

25-
* None.
25+
* Fix false positives in `empty_enum_arguments` rule when comparing values
26+
with a static member (e.g. `if number == .zero`).
27+
[Marcelo Fabri](https://github.com/marcelofabri)
28+
[#3562](https://github.com/realm/SwiftLint/issues/3562)
2629

2730
## 0.43.1: Laundroformat
2831

Source/SwiftLintFramework/Rules/Style/EmptyEnumArgumentsRule.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public struct EmptyEnumArgumentsRule: SubstitutionCorrectableASTRule, Configurat
4444
wrapInSwitch("case (let f as () -> String)?"),
4545
wrapInSwitch("default"),
4646
Example("if case .bar = foo {\n}"),
47-
Example("guard case .bar = foo else {\n}")
47+
Example("guard case .bar = foo else {\n}"),
48+
Example("if foo == .bar() {}"),
49+
Example("guard foo == .bar() else { return }")
4850
],
4951
triggeringExamples: [
5052
wrapInSwitch("case .bar↓(_)"),
@@ -53,7 +55,9 @@ public struct EmptyEnumArgumentsRule: SubstitutionCorrectableASTRule, Configurat
5355
wrapInSwitch("case .bar↓() where method() > 2"),
5456
wrapInFunc("case .bar↓(_)"),
5557
Example("if case .bar↓(_) = foo {\n}"),
56-
Example("guard case .bar↓(_) = foo else {\n}")
58+
Example("guard case .bar↓(_) = foo else {\n}"),
59+
Example("if case .bar↓() = foo {\n}"),
60+
Example("guard case .bar↓() = foo else {\n}")
5761
],
5862
corrections: [
5963
wrapInSwitch("case .bar↓(_)"): wrapInSwitch("case .bar"),
@@ -85,6 +89,7 @@ public struct EmptyEnumArgumentsRule: SubstitutionCorrectableASTRule, Configurat
8589
return []
8690
}
8791

92+
let needsCase = kind == .if || kind == .guard
8893
let contents = file.stringView
8994

9095
let callsRanges = dictionary.substructure.compactMap { dict -> NSRange? in
@@ -127,6 +132,10 @@ public struct EmptyEnumArgumentsRule: SubstitutionCorrectableASTRule, Configurat
127132
}
128133
}
129134

135+
if needsCase, file.match(pattern: "\\bcase\\b", with: [.keyword], range: caseRange).isEmpty {
136+
return nil
137+
}
138+
130139
if callsRanges.contains(where: parenthesesRange.intersects) {
131140
return nil
132141
}

0 commit comments

Comments
 (0)