Skip to content

Commit

Permalink
Merge pull request realm#1967 from marcelofabri/bugfix-1946
Browse files Browse the repository at this point in the history
Fix false positives in control_statement when methods with keyword names are used
  • Loading branch information
marcelofabri authored Dec 14, 2017
2 parents f01ed99 + 02792c1 commit 755f3ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

##### Bug Fixes

* None.
* Fix false positives in `control_statement` rule when methods with keyword
names are used.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1946](https://github.com/realm/SwiftLint/issues/1946)

## 0.24.0: Timed Dry

Expand Down
4 changes: 4 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,10 @@ do {
}
```

```swift
foo().catch(all: true) {}
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
12 changes: 11 additions & 1 deletion Source/SwiftLintFramework/Rules/ControlStatementRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public struct ControlStatementRule: ConfigurationProviderRule {
"} while condition {\n",
"do { ; } while condition {\n",
"switch foo {\n",
"do {\n} catch let error as NSError {\n}"
"do {\n} catch let error as NSError {\n}",
"foo().catch(all: true) {}"
],
triggeringExamples: [
"↓if (condition) {\n",
Expand Down Expand Up @@ -72,6 +73,15 @@ public struct ControlStatementRule: ConfigurationProviderRule {
let matchString = file.contents.substring(from: match.location, length: match.length)
return !isFalsePositive(matchString, syntaxKind: syntaxKinds.first)
}
.filter { match, _ -> Bool in
let contents = file.contents.bridge()
guard let byteOffset = contents.NSRangeToByteRange(start: match.location, length: 1)?.location,
let outerKind = file.structure.kinds(forByteOffset: byteOffset).last else {
return true
}

return SwiftExpressionKind(rawValue: outerKind.kind) != .call
}
.map { match, _ -> StyleViolation in
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
Expand Down

0 comments on commit 755f3ad

Please sign in to comment.