Fix false positives in closure_spacing

Fixes #3270
This commit is contained in:
Marcelo Fabri
2020-08-04 20:59:02 -07:00
parent 5ad5bf2dae
commit abf97f3c55
2 changed files with 25 additions and 1 deletions
+5
View File
@@ -54,6 +54,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#3186](https://github.com/realm/SwiftLint/issues/3186)
* Fix false positives in `closure_spacing` rule reported in
non-closures expressions.
[Marcelo Fabri](https://github.com/marcelofabri)
[#3270](https://github.com/realm/SwiftLint/issues/3270)
## 0.39.2: Stay Home
This is the last release to support building with Swift 5.0.x.
@@ -30,7 +30,17 @@ public struct ClosureSpacingRule: CorrectableRule, ConfigurationProviderRule, Op
Example("[].map ({ $0.description })"),
Example("[].filter { $0.contains(location) }"),
Example("extension UITableViewCell: ReusableView { }"),
Example("extension UITableViewCell: ReusableView {}")
Example("extension UITableViewCell: ReusableView {}"),
Example("""
protocol SomeProtocol {
var field: Int {get}
}
"""),
Example("""
func something(field: Int) {
guard field != 0 else {return}
}
""")
],
triggeringExamples: [
Example("[].filter(↓{$0.contains(location)})"),
@@ -131,6 +141,15 @@ public struct ClosureSpacingRule: CorrectableRule, ConfigurationProviderRule, Op
let cleaned = content.trimmingCharacters(in: .whitespaces)
return content != " " + cleaned + " "
}
.filter { range in
guard SwiftVersion.current > .fourDotTwo else {
return true
}
let byteOffset = file.stringView.byteOffset(fromLocation: range.location)
let structures = file.structureDictionary.structures(forByteOffset: byteOffset)
return structures.last?.kind.flatMap(SwiftExpressionKind.init) == .closure
}
.sorted {
$0.location < $1.location
}