You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
prefer_self_in_static_references rewrites composition/existential types to Self in extensions (e.g. any A & B → any Self & B) #6748
When prefer_self_in_static_references (opt-in) is enabled, the rule treats an extension as class-like and rewrites references to the extended type into Self. However, it also does this when the type appears inside a protocol composition or as the constraint of an existential/opaque type, where Self is not interchangeable with the named type. The autocorrection changes the meaning of the code (and in the existential/opaque case produces code that does not compile).
This was observed inside a function in a protocol extension.
Complete output when running SwiftLint, including the stack trace and command used
Given:
protocolA{}protocolB{}extensionA{funcf(_x:Any)->Bool{x is anyA&B}}
.swiftlint.yml:
only_rules:
- prefer_self_in_static_references
Running:
swiftlint lint --fix --no-cache
rewrites the body to x is any Self & B.
The same happens for a bare existential:
protocolA{}extensionA{funcf(_x:Any)->Bool{x is anyA} // → `x is any Self`, which is invalid
}
and for a member-type extension with a composition:
classOuter{classInner{}}protocolB{}extensionOuter.Inner{funcg(_x:Any)->Bool{x is Outer.Inner&B} // → `x is Self & B`
}
Expected behavior
The rule should not rewrite a type to Self when it appears:
as one element of a protocol composition (any A & B, Outer.Inner & B), or
as the constraint of an existential or opaque type (any A, some A).
In a protocol extension, Self denotes the conforming concrete type, so any Self/some Self is not a valid substitution, and Self & B is not equivalent to A & B. Genuine static references in the same extension (e.g. K.i → Self.i) should still be corrected.
Environment
SwiftLint version: 0.63.3
Installation method used: Homebrew / Swift Package Manager
Paste your configuration file: (see above)
Are you using nested configurations? no
Which Xcode version are you using: Xcode 26 / Swift 6.3
Do you have a sample that shows the issue? Yes — see the snippets above.
Activity