Skip to content

Commit

Permalink
Merge pull request #2044 from marcelofabri/bugfix-2041
Browse files Browse the repository at this point in the history
Fix false positive in `empty_enum_arguments` rule when using closures
  • Loading branch information
marcelofabri authored Feb 11, 2018
2 parents 72e155d + 8c2c4b0 commit 3b80e15
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
 `ignores_function_declarations` configuration of `line_length` rule.
 [Manabu Nakazawa](https://github.com/mshibanami)

* Fix false positive in `empty_enum_arguments` rule when using closures.
[Marcelo Fabri](https://github.com/marcelofabri)
[#2041](https://github.com/realm/SwiftLint/issues/2041)

## 0.24.2: Dented Tumbler

#### Breaking
Expand Down
12 changes: 12 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2969,6 +2969,18 @@ switch (foo, bar) {
}
```

```swift
switch foo {
case (let f as () -> String)?: break
}
```

```swift
switch foo {
default: break
}
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Scott Hoyt on 12/28/15.
// Copyright © 2015 Realm. All rights reserved.
//
// Generated using Sourcery 0.9.0 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.10.1 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

public let masterRuleList = RuleList(rules: [
Expand Down
19 changes: 9 additions & 10 deletions Source/SwiftLintFramework/Rules/EmptyEnumArgumentsRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public struct EmptyEnumArgumentsRule: ASTRule, ConfigurationProviderRule, Correc
wrapInSwitch("case let .bar(x)"),
wrapInSwitch(variable: "(foo, bar)", "case (_, _)"),
wrapInSwitch("case \"bar\".uppercased()"),
wrapInSwitch(variable: "(foo, bar)", "case (_, _) where !something")
wrapInSwitch(variable: "(foo, bar)", "case (_, _) where !something"),
wrapInSwitch("case (let f as () -> String)?"),
wrapInSwitch("default")
],
triggeringExamples: [
wrapInSwitch("case .bar↓(_)"),
Expand Down Expand Up @@ -83,16 +85,13 @@ public struct EmptyEnumArgumentsRule: ASTRule, ConfigurationProviderRule, Correc
return []
}

return file.match(pattern: "\\([,\\s_]*\\)", range: caseRange).flatMap { range, kinds in
guard Set(kinds).isSubset(of: [.keyword]),
case let byteRange = NSRange(location: offset, length: length),
Set(file.syntaxMap.kinds(inByteRange: byteRange)) != [.keyword] else {
return nil
}
let emptyArgumentRegex = regex("\\.\\S+\\s*(\\([,\\s_]*\\))")
return emptyArgumentRegex.matches(in: file.contents, options: [], range: caseRange).flatMap { match in
let parenthesesRange = match.range(at: 1)

// avoid matches after `where` keyworkd
if let whereRange = file.match(pattern: "where", with: [.keyword], range: caseRange).first {
if whereRange.location < range.location {
if whereRange.location < parenthesesRange.location {
return nil
}

Expand All @@ -106,11 +105,11 @@ public struct EmptyEnumArgumentsRule: ASTRule, ConfigurationProviderRule, Correc
}
}

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

return range
return parenthesesRange
}
}
}
Expand Down

0 comments on commit 3b80e15

Please sign in to comment.