From ffacf229ce362c78abf2fc2021ab3629bc446cac Mon Sep 17 00:00:00 2001 From: wuyangfan <1102042793@qq.com> Date: Sun, 17 May 2026 22:51:08 +0800 Subject: [PATCH 1/2] fix(redundant_self): allow self in os.Logger privacy interpolations Skip redundant_self when explicit self appears in labeled arguments such as `privacy:` inside string interpolations, which require explicit capture. Fixes #6542 --- .../Rules/Style/RedundantSelfRule.swift | 18 ++++++++++++++++++ .../Style/RedundantSelfRuleExamples.swift | 12 ++++++++++++ 2 files changed, 30 insertions(+) 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 From e4cfeaaaa2dfbb2febe8be2602ad4626db98a100 Mon Sep 17 00:00:00 2001 From: wuyangfan <1102042793@qq.com> Date: Sun, 17 May 2026 22:51:54 +0800 Subject: [PATCH 2/2] chore: add changelog entry for redundant_self fix --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) 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)