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 38eb3ed
Show file tree
Hide file tree
Showing 2 changed files with 17 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
17 changes: 12 additions & 5 deletions Source/SwiftLintFramework/Rules/Performance/EmptyStringRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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 +23,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 38eb3ed

Please sign in to comment.