Skip to content

Fix explicit_self false positives around string interpolation - #6610

Closed
jffmrk wants to merge 1 commit into
realm:mainfrom
jffmrk:fix-explicit-self-string-interpolation
Closed

jffmrk wants to merge 1 commit into
realm:mainfrom
jffmrk:fix-explicit-self-string-interpolation

Conversation

@jffmrk

@jffmrk jffmrk commented Apr 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

SourceKit can report misleading instance-member refs for explicit_self when code uses string literals and \(...) interpolation (e.g. literal text that matches a property name, or bogus offsets at the interpolation open paren).

This PR filters those cases using SwiftSyntax static string segments, skips offsets at \(, and verifies explicit self.` spans in source text.

Fixes #6611

Testing

  • swift test --filter ExplicitSelfRuleGeneratedTests

Made with Cursor

@SwiftLintBot

SwiftLintBot commented Apr 18, 2026 •

Copy link
Copy Markdown
1 Warning
⚠️ This PR may need tests.
19 Messages
📖 Building this branch resulted in a binary size of 27624.77 KiB vs 27621.7 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.82 s vs 0.83 s on main (1% faster).
📖 Linting Alamofire with this PR took 1.04 s vs 1.06 s on main (1% faster).
📖 Linting Brave with this PR took 7.4 s vs 7.4 s on main (0% slower).
📖 Linting DuckDuckGo with this PR took 28.55 s vs 28.58 s on main (0% faster).
📖 Linting Firefox with this PR took 12.32 s vs 12.37 s on main (0% faster).
📖 Linting Kickstarter with this PR took 8.54 s vs 8.56 s on main (0% faster).
📖 Linting Moya with this PR took 0.45 s vs 0.46 s on main (2% faster).
📖 Linting NetNewsWire with this PR took 2.64 s vs 2.64 s on main (0% slower).
📖 Linting Nimble with this PR took 0.67 s vs 0.69 s on main (2% faster).
📖 Linting PocketCasts with this PR took 7.93 s vs 8.14 s on main (2% faster).
📖 Linting Quick with this PR took 0.46 s vs 0.46 s on main (0% slower).
📖 Linting Realm with this PR took 2.97 s vs 3.08 s on main (3% faster).
📖 Linting Sourcery with this PR took 1.91 s vs 1.9 s on main (0% slower).
📖 Linting Swift with this PR took 4.81 s vs 4.78 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.35 s vs 0.35 s on main (0% slower).
📖 Linting VLC with this PR took 1.25 s vs 1.22 s on main (2% slower).
📖 Linting Wire with this PR took 19.17 s vs 19.22 s on main (0% faster).
📖 Linting WordPress with this PR took 12.93 s vs 12.95 s on main (0% faster).

Generated by 🚫 Danger

@jffmrk
jffmrk marked this pull request as draft April 18, 2026 21:06
@jffmrk
jffmrk force-pushed the fix-explicit-self-string-interpolation branch from 01f0fc8 to cedcca5 Compare April 19, 2026 00:36
@jffmrk jffmrk changed the title Fix explicit_self false positives around string interpolation Fix explicit_self false positives around string interpolation Apr 19, 2026
@jffmrk
jffmrk marked this pull request as ready for review April 19, 2026 00:38
@jffmrk
jffmrk marked this pull request as draft April 19, 2026 23:11
@jffmrk
jffmrk force-pushed the fix-explicit-self-string-interpolation branch from f5e32b7 to 6dfa8fc Compare April 20, 2026 01:23
SourceKit can report instance-member refs for text inside string literals, at
the open paren of \(...), or with offsets that do not match a simple '.' check.
Combine SwiftSyntax static segment ranges with source-text verification for
explicit self.<member> spans, and skip bogus offsets at string interpolation
open parens.

Add non-triggering, triggering, and correction examples for implicit member
access inside string interpolation.
@jffmrk
jffmrk force-pushed the fix-explicit-self-string-interpolation branch from 6dfa8fc to 12dd5f5 Compare April 20, 2026 01:29
@jffmrk
jffmrk marked this pull request as ready for review April 20, 2026 01:29
@SimplyDanny

Copy link
Copy Markdown
Collaborator

Hey @jffmrk, thanks for bringing this up and sorry for the late response.

While reviewing your code, I thought about simplifying the whole rule a lot by making use of your idea to let SwiftSyntax help to find correct positions more reliably. This is what led to #6662. It uses SwiftSyntax to find symbols in the first place and only asks SourceKit about their origin. Doing so, a lot of code could be removed making the rule quite a bit easier to comprehend.

I hope you don't mind if we move forward with #6662. I have retained you as a (co-)author in the commit and the changelog entry.

@jffmrk

jffmrk commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

Cool. Thanks! I'll try it out on our code tomorrow. This was Cursor helping me out.

@SimplyDanny

Copy link
Copy Markdown
Collaborator

Cool. Thanks! I'll try it out on our code tomorrow. This was Cursor helping me out.

Let me know about the results. I'll cut a new release shortly after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: explicit_self false positive with string interpolation

3 participants