diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c5e3a40a4..34cf4a1023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,12 @@ * Prevent crash for private types named `_` in `type_name` rules. [sinoru](https://github.com/sinoru) [#3971](https://github.com/realm/SwiftLint/issues/3971) + +* Make `for_where` rule implementation independent of order in structure + dictionary. This fixes the rule in Xcode 13.3 where some violation were + no longer reported. + [SimplyDanny](https://github.com/SimplyDanny) + [#3975](https://github.com/realm/SwiftLint/issues/3975) ## 0.47.1: Smarter Appliance diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/ForWhereRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/ForWhereRule.swift index 747c900f6f..7598a4bd3c 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/ForWhereRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/ForWhereRule.swift @@ -80,6 +80,14 @@ public struct ForWhereRule: ASTRule, ConfigurationProviderRule, AutomaticTestabl for user in users { ↓if user.id == 1 { return true } } + """), + Example(""" + for subview in subviews { + ↓if !(subview is UIStackView) { + subview.removeConstraints(subview.constraints) + subview.removeFromSuperview() + } + } """) ] ) @@ -115,11 +123,9 @@ public struct ForWhereRule: ASTRule, ConfigurationProviderRule, AutomaticTestabl private func isOnlyOneIf(dictionary: SourceKittenDictionary) -> Bool { let substructure = dictionary.substructure - guard substructure.count == 1 else { - return false - } - - return dictionary.substructure.first?.statementKind == .brace + let onlyOneBlock = substructure.filter { $0.statementKind == .brace }.count == 1 + let noOtherIf = substructure.allSatisfy { $0.statementKind != .if } + return onlyOneBlock && noOtherIf } private func isOnlyIfInsideFor(forDictionary: SourceKittenDictionary,