diff --git a/CHANGELOG.md b/CHANGELOG.md index 02357fd76..16ecc7588 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,12 @@ #### Enhancements -* None. - #### Bug Fixes +* Fix false positive for LetVarWhitespaceRule. + [PaulTaykalo](https://github.com/PaulTaykalo) + [#2956](https://github.com/realm/SwiftLint/issues/2956) + * Fix for false-positive identical operands rule. [PaulTaykalo](https://github.com/PaulTaykalo) [#2953](https://github.com/realm/SwiftLint/issues/2953) diff --git a/Source/SwiftLintFramework/Rules/Style/LetVarWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/Style/LetVarWhitespaceRule.swift index f6924d388..7d9d1104c 100644 --- a/Source/SwiftLintFramework/Rules/Style/LetVarWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/LetVarWhitespaceRule.swift @@ -196,10 +196,9 @@ public struct LetVarWhitespaceRule: ConfigurationProviderRule, OptInRule, Automa } } - let directives: Set = ["#if", "#elseif", "#else", "#endif", "#!", "#warning", "#error"] let directiveLines = file.lines.filter { - let trimmed = $0.content.trimmingCharacters(in: .whitespaces) - return directives.contains(where: trimmed.hasPrefix) + return regex(#"^\s*#(if|elseif|else|endif|\!|warning|error)"#) + .firstMatch(in: $0.content, options: [], range: $0.content.fullNSRange) != nil } result.formUnion(directiveLines.map { $0.index - 1 })