Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive in empty_enum_arguments rule when using closures #2044

Merged
merged 1 commit into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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