Skip to content

prefer_self_in_static_references rewrites composition/existential types to Self in extensions (e.g. any A & B → any Self & B) #6748

Closed

Description

@Brett-Best

New Issue Checklist

Describe the bug

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:

protocol A {}
protocol B {}
extension A {
    func f(_ x: Any) -> Bool { x is any A & 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:

protocol A {}
extension A {
    func f(_ x: Any) -> Bool { x is any A }   // → `x is any Self`, which is invalid
}

and for a member-type extension with a composition:

class Outer { class Inner {} }
protocol B {}
extension Outer.Inner {
    func g(_ 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

  1. added
    bug
    on Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions