Skip to content

Commit

Permalink
Fix false positive in empty_string rule with multiline literals
Browse files Browse the repository at this point in the history
Fixes #3100
  • Loading branch information
marcelofabri committed Feb 10, 2020
1 parent c5e7bb3 commit ccded17
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#3079](https://github.com/realm/SwiftLint/issues/3079)

* Fix false positive in `empty_string` rule when using multiline string
literals.
[Marcelo Fabri](https://github.com/marcelofabri)
[#3100](https://github.com/realm/SwiftLint/issues/3100)

## 0.38.2: Machine Repair Manual

#### Breaking
Expand Down
18 changes: 13 additions & 5 deletions Source/SwiftLintFramework/Rules/Performance/EmptyStringRule.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import SourceKittenFramework

public struct EmptyStringRule: ConfigurationProviderRule, OptInRule, AutomaticTestableRule {
Expand All @@ -12,7 +13,8 @@ public struct EmptyStringRule: ConfigurationProviderRule, OptInRule, AutomaticTe
kind: .performance,
nonTriggeringExamples: [
Example("myString.isEmpty"),
Example("!myString.isEmpty")
Example("!myString.isEmpty"),
Example("\"\"\"\nfoo==\n\"\"\"")
],
triggeringExamples: [
Example("myString↓ == \"\""),
Expand All @@ -22,10 +24,16 @@ public struct EmptyStringRule: ConfigurationProviderRule, OptInRule, AutomaticTe

public func validate(file: SwiftLintFile) -> [StyleViolation] {
let pattern = "\\b\\s*(==|!=)\\s*\"\""
return file.match(pattern: pattern, with: [.string]).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
return file.match(pattern: pattern, with: [.string]).compactMap { range in
guard let byteRange = file.stringView.NSRangeToByteRange(NSRange(location: range.location, length: 1)),
case let kinds = file.syntaxMap.kinds(inByteRange: byteRange),
kinds.isEmpty else {
return nil
}

return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location))
}
}
}

0 comments on commit ccded17

Please sign in to comment.