diff --git a/CHANGELOG.md b/CHANGELOG.md index be786e3de4..a5bbab0560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,11 @@ ### Enhancements +* Fix `redundant_self` false positive when `self` is used in labeled string interpolation + expressions such as os.Logger privacy arguments. + [leno23](https://github.com/leno23) + [#6542](https://github.com/realm/SwiftLint/issues/6542) + * Print fixed code read from stdin to stdout. [SimplyDanny](https://github.com/SimplyDanny) [#6501](https://github.com/realm/SwiftLint/issues/6501) diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRule.swift index f08e4a5e22..6e5582f21c 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRule.swift @@ -113,6 +113,9 @@ private extension RedundantSelfRule { if configuration.keepInInitializers, initializerScopes.peek() == true { return } + if requiresExplicitSelf(node) { + return + } if closureExprScopes.isNotEmpty, !isSelfRedundant { return } @@ -157,6 +160,21 @@ private extension RedundantSelfRule { || selfCapture == .strong && SwiftVersion.current >= .fiveDotThree || selfCapture == .weak && SwiftVersion.current >= .fiveDotEight } + + private func requiresExplicitSelf(_ node: MemberAccessExprSyntax) -> Bool { + guard node.isBaseSelf else { + return false + } + var current: Syntax? = Syntax(node) + while let parent = current?.parent { + if let labeledExpr = parent.as(LabeledExprSyntax.self), + labeledExpr.label?.text == "privacy" { + return true + } + current = parent + } + return false + } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRuleExamples.swift index 8b39e82164..a5122a413b 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRuleExamples.swift @@ -117,6 +117,18 @@ struct RedundantSelfRuleExamples { } } """), + Example(""" + import os + + class Name { + private let test = "" + + func testLog() { + Logger(subsystem: "sub", category: "cat") + .warning("test: \\(self.test, privacy: .private(mask: .hash))") + } + } + """), Example(""" struct S { var x = 0, y = 0