Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions Source/SwiftLintBuiltInRules/Rules/Style/RedundantSelfRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ private extension RedundantSelfRule {
if configuration.keepInInitializers, initializerScopes.peek() == true {
return
}
if requiresExplicitSelf(node) {
return
}
if closureExprScopes.isNotEmpty, !isSelfRedundant {
return
}
Expand Down Expand Up @@ -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
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down