diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d645a77e..b57eeac63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Source/SwiftLintFramework/Rules/Style/ClosureSpacingRule.swift b/Source/SwiftLintFramework/Rules/Style/ClosureSpacingRule.swift index 4132bf7e6..2f92763aa 100644 --- a/Source/SwiftLintFramework/Rules/Style/ClosureSpacingRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/ClosureSpacingRule.swift @@ -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 }