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
False negatives in prefer_self_in_static_references since 0.46.2 #3993
The prefer_self_in_static_references is not reporting some issues since in from version 0.46.2 onward. Our CI was still on 0.46.1 and caught many more issues with the older version
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint
Linting Swift files in current working directory
Linting 'foo.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.
# After downgrading to 0.46.1
$ swiftlint lint
Linting Swift files in current working directory
Linting 'foo.swift' (1/1)
/Users/neo/Desktop/swiftlint/foo.swift:6:27: warning: Prefer Self in Static References Violation: Static references should be prefixed by `Self` instead of the class name. (prefer_self_in_static_references)
Done linting! Found 1 violation, 0 serious in 1 file.
Environment
SwiftLint version (run swiftlint version to be sure)? 0.47.1
Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew
Which Xcode version are you using (check xcodebuild -version)? Xcode 13.4.1 Build version 13F100
Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
to quickly test if your example is really demonstrating the issue. If your example is more
complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
The rule has been disabled generally in extensions since it is not possible to know whether an extension is for a struct or a class or an enum in the current implementation. There are some special cases where static references cannot use Self in the context of a class. So to avoid false-positives in classes (leading to non-compilable code) this decision was made in #3813 intentionally. A new far more time consuming "analyzer" rule could be added to improve the situation, though, as mentioned in the PR.
So this is rather a request for a new rule than a bug.
Thinking about this issue again, while my previous statement about a new Analyzer rule remains true to really hit all cases where Self is allowed, skipping extensions entirely is actually a bit too cautious. Instead, extensions could be treated like extensions for classes to find at least a few violations of the rule. Clearly, there were even more when SwiftLint would know which exact type is extended.
Allowing the rule for extensions by treating the type as it was a class, would be achievable as a first step without much effort. This might be a "good first issue".
Activity
SimplyDanny commentedon Jun 14, 2022
The rule has been disabled generally in extensions since it is not possible to know whether an extension is for a struct or a class or an enum in the current implementation. There are some special cases where static references cannot use
Selfin the context of a class. So to avoid false-positives in classes (leading to non-compilable code) this decision was made in #3813 intentionally. A new far more time consuming "analyzer" rule could be added to improve the situation, though, as mentioned in the PR.So this is rather a request for a new rule than a bug.
SimplyDanny commentedon Feb 11, 2024
Thinking about this issue again, while my previous statement about a new Analyzer rule remains true to really hit all cases where
Selfis allowed, skipping extensions entirely is actually a bit too cautious. Instead, extensions could be treated like extensions for classes to find at least a few violations of the rule. Clearly, there were even more when SwiftLint would know which exact type is extended.Allowing the rule for extensions by treating the type as it was a class, would be achievable as a first step without much effort. This might be a "good first issue".
Surface violations of `prefer_self_in_static_references` inside exten…