From 02792c1a011fd66072e8c2909edee6fc7722b2ca Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Mon, 4 Dec 2017 23:18:16 -0800 Subject: [PATCH] Fix false positives in control_statement when methods with keyword names are used Fixes #1946 --- CHANGELOG.md | 5 ++++- Rules.md | 4 ++++ .../Rules/ControlStatementRule.swift | 12 +++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e64e732ea..c9446b8304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Rules.md b/Rules.md index d382925865..67b7e2ab2a 100644 --- a/Rules.md +++ b/Rules.md @@ -1740,6 +1740,10 @@ do { } ``` +```swift +foo().catch(all: true) {} +``` +
Triggering Examples diff --git a/Source/SwiftLintFramework/Rules/ControlStatementRule.swift b/Source/SwiftLintFramework/Rules/ControlStatementRule.swift index 73aa4c3a87..200f64f497 100644 --- a/Source/SwiftLintFramework/Rules/ControlStatementRule.swift +++ b/Source/SwiftLintFramework/Rules/ControlStatementRule.swift @@ -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", @@ -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,