Skip to content

Commit

Permalink
Fix false false positive in empty_enum_arguments
Browse files Browse the repository at this point in the history
Fixes #3562
  • Loading branch information
marcelofabri committed Apr 29, 2021
1 parent a10158c commit e496077
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

#### Bug Fixes

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

## 0.43.1: Laundroformat

Expand Down
13 changes: 11 additions & 2 deletions Source/SwiftLintFramework/Rules/Style/EmptyEnumArgumentsRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public struct EmptyEnumArgumentsRule: SubstitutionCorrectableASTRule, Configurat
wrapInSwitch("case (let f as () -> String)?"),
wrapInSwitch("default"),
Example("if case .bar = foo {\n}"),
Example("guard case .bar = foo else {\n}")
Example("guard case .bar = foo else {\n}"),
Example("if foo == .bar() {}"),
Example("guard foo == .bar() else { return }")
],
triggeringExamples: [
wrapInSwitch("case .bar↓(_)"),
Expand All @@ -53,7 +55,9 @@ public struct EmptyEnumArgumentsRule: SubstitutionCorrectableASTRule, Configurat
wrapInSwitch("case .bar↓() where method() > 2"),
wrapInFunc("case .bar↓(_)"),
Example("if case .bar↓(_) = foo {\n}"),
Example("guard case .bar↓(_) = foo else {\n}")
Example("guard case .bar↓(_) = foo else {\n}"),
Example("if case .bar↓() = foo {\n}"),
Example("guard case .bar↓() = foo else {\n}")
],
corrections: [
wrapInSwitch("case .bar↓(_)"): wrapInSwitch("case .bar"),
Expand Down Expand Up @@ -85,6 +89,7 @@ public struct EmptyEnumArgumentsRule: SubstitutionCorrectableASTRule, Configurat
return []
}

let needsCase = kind == .if || kind == .guard
let contents = file.stringView

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

if needsCase, file.match(pattern: "\\bcase\\b", with: [.keyword], range: caseRange).isEmpty {
return nil
}

if callsRanges.contains(where: parenthesesRange.intersects) {
return nil
}
Expand Down

0 comments on commit e496077

Please sign in to comment.