Skip to content

Surface violations of prefer_self_in_static_references inside extensions#6639

Open
itsybitsybootsy wants to merge 2 commits into
realm:mainfrom
itsybitsybootsy:fix/prefer-self-in-extensions
Open

Surface violations of prefer_self_in_static_references inside extensions#6639
itsybitsybootsy wants to merge 2 commits into
realm:mainfrom
itsybitsybootsy:fix/prefer-self-in-extensions

Conversation

@itsybitsybootsy
Copy link
Copy Markdown

Problem

prefer_self_in_static_references currently pushes .skipReferences for every ExtensionDeclSyntax, which silences the rule inside the entire extension body. The rule misses violations like:

class Foo {
    static let value = 1
}

extension Foo {
    func helper() -> Int { Foo.value }      // not flagged, should be Self.value
    var alias: Int { Foo.value }            // not flagged, should be Self.value
}

Per @SimplyDanny's comment on #3993:

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".

Fix

When the extended type is a plain identifier (extension Foo) or a member type (extension Foo.Bar), push .likeClass(name: extendedTypeName) instead of .skipReferences. Complex extended-type shapes (generic specialisations, opaque types, function types) continue to fall through to the previous .skipReferences behaviour.

The class scope is the most conservative reuse of existing logic — it already correctly handles method bodies, computed properties, and closure invocations on the right-hand side, while still skipping the things class mode skips (stored-property initializers, function signatures) to avoid false positives. This matches the maintainer's "first step" framing: the reporter's exact case (extension Foo { static let otherValue = Foo.value } — a stored-property initializer) remains exempt under this change, matching how the rule already treats top-level class declarations.

The IdentifierTypeSyntax visitor also gains a guard so that the extension declaration header itself (the extended-type identifier) is excluded from violations — without it, the new scope would rewrite extension Foo to extension Self.

Tests

  • New non-triggering example: extension of an unrelated type with a reference to a different type (confirms parent-name matching is correct).
  • New triggering examples: extension of a class with violations in method body + computed property, and extension of a nested type (Outer.Inner) confirming MemberTypeSyntax handling.
  • New correction: extension method body that auto-fixes ↓C.iSelf.i.
  • Existing non-triggering example at the top of the file (extension T { static let j = S.i + T.i; static let k = { T.j }() }) continues to pass: class-mode MemberBlockSyntax already skips stored-property initializer expressions, so neither T.i nor the closure body's T.j is reachable.

Self-corrections

The integration test (testSwiftLintAutoCorrects) lints SwiftLint's own source against all rules. With this change, four pre-existing violations in SwiftLint's own code are surfaced and auto-corrected — they're equivalent rewrites of the same shape (String.operatorsSelf.operators inside a private extension String, etc.) and are bundled here so the integration test passes:

  • Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferKeyPathRule.swift: as ExprSyntaxas Self
  • Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift: String.operatorsSelf.operators
  • Source/SwiftLintBuiltInRules/Rules/Style/ContrastedOpeningBraceRule.swift: \IfExprSyntax.elseBody\Self.elseBody
  • Source/SwiftLintFramework/Reporters/SummaryReporter.swift: Int.numberFormatterSelf.numberFormatter

Verified locally with swift run swiftlint-dev rules register && swift test. All tests pass.

Resolves #3993.


This contribution was developed with the help of Claude Code. The fix, regression tests, edge-case analysis, and the local test suite were reviewed manually before submitting.

@SwiftLintBot
Copy link
Copy Markdown

SwiftLintBot commented May 15, 2026

2245 Warnings
⚠️ This PR may need tests.
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Models/Extensions/DispatchQueue+Extension.swift:14:9: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Models/Extensions/DispatchQueue+Extension.swift:17:17: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Models/Time/Solar.swift:336:32: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Models/Time/Solar.swift:340:41: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Views/AerialView+Player.swift:72:25: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Views/AerialView+Player.swift:136:38: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Views/AerialView+Player.swift:143:43: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Aerial: /Aerial/Source/Views/AerialView+Player.swift:144:46: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Example/Source/DetailViewController.swift:209:35: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Source/Features/URLEncodedFormEncoder.swift:510:25: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Source/Features/URLEncodedFormEncoder.swift:520:9: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Source/Features/URLEncodedFormEncoder.swift:529:9: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Source/Features/URLEncodedFormEncoder.swift:786:25: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Tests/FileManager+AlamofireTests.swift:35:30: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Tests/FileManager+AlamofireTests.swift:43:17: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Tests/FileManager+AlamofireTests.swift:58:17: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Tests/FileManager+AlamofireTests.swift:72:26: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Alamofire: /Tests/Bundle+AlamofireTests.swift:31:18: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/App/BraveWidgets/WidgetBackground.swift:26:34: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/App/iOS/Delegates/SceneDelegate.swift:420:8: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/App/iOS/Delegates/SceneDelegate.swift:421:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/App/iOS/Delegates/SceneDelegate.swift:429:8: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/App/iOS/Delegates/SceneDelegate.swift:430:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Tests/DataTests/DomainTests.swift:477:20: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveVPN/Models/BraveVPNProductInfo.swift:57:9: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveVPN/Models/BraveVPNProductInfo.swift:59:9: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveVPN/Components/Subscription/BraveVPN+Promotion.swift:52:34: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveVPN/Components/Subscription/BraveVPN+ReceiptResponse.swift:84:38: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveVPN/Components/Region/BraveVPN+Region.swift:111:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Playlist/PlaylistManager.swift:998:13: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Web/Chromium/CWVWebViewExtensions.swift:21:38: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Web/Chromium/CWVWebViewExtensions.swift:26:37: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Web/Chromium/CWVWebViewExtensions.swift:35:25: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Web/Chromium/CWVWebViewExtensions.swift:46:16: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/PreviewContent/MockStores.swift:111:17: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/String+Extensions.swift:24:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/String+Extensions.swift:25:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/String+Extensions.swift:27:24: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/String+Extensions.swift:30:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/BraveWalletExtensions.swift:145:35: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/BraveWalletExtensions.swift:156:35: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/BraveWalletExtensions.swift:573:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Shared/Extensions/UIDeviceExtensions.swift:59:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Shared/Extensions/UIDeviceExtensions.swift:63:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistScriptHandler.swift:265:11: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Playlist.swift:377:11: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Playlist.swift:379:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Origin.swift:18:5: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/Browser/TabManager.swift:1701:16: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/Browser/TabManager.swift:1708:16: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Frontend/Browser/TabManager.swift:1716:17: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Extensions/UIPasteboardExtensions.swift:16:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Extensions/UIPasteboardExtensions.swift:29:5: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:474:8: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:484:36: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:485:51: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:488:51: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:489:52: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:490:49: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:491:54: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:492:45: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:493:52: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:496:49: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:497:50: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:515:37: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:516:50: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:517:49: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:520:46: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:523:46: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:524:51: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:525:44: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:526:46: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:527:56: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:530:33: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:531:45: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:532:39: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:533:38: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:535:5: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:545:9: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:558:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:558:59: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:564:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:572:11: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:578:11: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:581:5: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:589:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:600:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:612:8: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:613:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:627:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Brave/Migration/Migration.swift:639:5: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveShared/Extensions/JSONSerializationExtensions.swift:12:23: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveShared/Extensions/JSONSerializationExtensions.swift:14:18: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveShared/Extensions/UIApplicationExtensions.swift:33:9: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/AIChat/Components/Markdown/AttributedString+Markdown.swift:14:18: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/AIChat/Components/Markdown/AttributedString+Markdown.swift:26:16: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/AIChat/Components/Markdown/AttributedString+Markdown.swift:51:46: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/AIChat/Components/Markdown/AttributedString+Markdown.swift:117:45: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/AIChat/Components/Popover/PopoverView.swift:35:33: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveUI/Extensions/UIFontExtensions.swift:23:16: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveUI/SwiftUI/BravePopup.swift:31:33: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveUI/SwiftUI/OSAvailabilityModifier.swift:15:48: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/CertificateUtilities/BraveCertificateUtils.swift:226:7: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/CertificateUtilities/BraveCertificateUtils.swift:227:47: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveShields/Domain+Extensions.swift:81:21: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/BraveShields/Domain+Extensions.swift:94:21: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/SessionWindow.swift:77:10: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/SessionWindow.swift:99:30: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/SessionWindow.swift:108:19: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/SessionWindow.swift:148:33: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/SessionTab.swift:130:30: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/SessionTab.swift:254:14: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/SessionTab.swift:285:23: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:385:21: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:400:34: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:414:85: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:423:5: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:428:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:450:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:465:22: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Domain.swift:562:12: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in Brave: /ios/brave-ios/Sources/Data/models/Favorite.swift:294:76: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in DuckDuckGo: /SharedPackages/BrowserServicesKit/Sources/BrowserServicesKit/Statistics/Atb.swift:105:38: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in DuckDuckGo: /SharedPackages/BrowserServicesKit/Sources/BrowserServicesKit/Autofill/AutofillUserScript+Email.swift:105:39: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in DuckDuckGo: /SharedPackages/BrowserServicesKit/Sources/BrowserServicesKit/SecureVault/AutofillKeyStoreProvider.swift:377:59: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in DuckDuckGo: /SharedPackages/BrowserServicesKit/Sources/BrowserServicesKit/SecureVault/AutofillKeyStoreProvider.swift:381:49: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in DuckDuckGo: /SharedPackages/BrowserServicesKit/Sources/BrowserServicesKit/Autofill/AutofillUserScript+SecureVault.swift:1119:23: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ This PR introduced a violation in DuckDuckGo: /SharedPackages/BrowserServicesKit/Sources/BrowserServicesKit/SecureVault/AutofillDatabaseProvider.swift:1709:35: Warning: Use Self to refer to the surrounding type name (prefer_self_in_static_references)
⚠️ Danger found 2245 violations with this PR. Due to GitHub's max issue comment size, the number shown has been truncated to 129.
19 Messages
📖 Building this branch resulted in a binary size of 27643.09 KiB vs 27639.02 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.81 s vs 0.81 s on main (0% slower).
📖 Linting Alamofire with this PR took 1.12 s vs 1.13 s on main (0% faster).
📖 Linting Brave with this PR took 7.4 s vs 7.41 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 30.18 s vs 30.2 s on main (0% faster).
📖 Linting Firefox with this PR took 12.71 s vs 12.78 s on main (0% faster).
📖 Linting Kickstarter with this PR took 8.66 s vs 8.68 s on main (0% faster).
📖 Linting Moya with this PR took 0.46 s vs 0.44 s on main (4% slower).
📖 Linting NetNewsWire with this PR took 2.92 s vs 2.87 s on main (1% slower).
📖 Linting Nimble with this PR took 0.68 s vs 0.69 s on main (1% faster).
📖 Linting PocketCasts with this PR took 8.35 s vs 8.22 s on main (1% slower).
📖 Linting Quick with this PR took 0.47 s vs 0.44 s on main (6% slower).
📖 Linting Realm with this PR took 3.01 s vs 3.05 s on main (1% faster).
📖 Linting Sourcery with this PR took 1.94 s vs 1.9 s on main (2% slower).
📖 Linting Swift with this PR took 4.86 s vs 4.84 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.34 s vs 0.35 s on main (2% faster).
📖 Linting VLC with this PR took 1.28 s vs 1.25 s on main (2% slower).
📖 Linting Wire with this PR took 19.35 s vs 19.4 s on main (0% faster).
📖 Linting WordPress with this PR took 13.08 s vs 13.08 s on main (0% slower).

Generated by 🚫 Danger

Copy link
Copy Markdown
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That's the right direction with a few remarks ...

}
}
extension Outer.Inner {
func f() -> Int { ↓Inner.i }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inner.i doesn't compile. It should be Outer.Inner.i.

Comment thread CHANGELOG.md Outdated
Comment on lines +15 to +19
* Surface violations of `prefer_self_in_static_references` inside
extensions of types whose name is a plain identifier or a member type
(e.g. `extension Foo` or `extension Foo.Bar`). Stored-property
initializers and function signatures remain exempt — matching the
rule's existing behaviour for top-level class declarations.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be shorter. Something like "Treat extensions like classes in ... rule.".

extension C {
func f() -> Int { Self.i }
}
"""),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an Outer.Inner.i correction example as well.

itsybitsybootsy and others added 2 commits May 22, 2026 12:23
…sions

The rule previously pushed `.skipReferences` for every `ExtensionDeclSyntax`,
which silenced the rule inside the entire extension body. Per the maintainer's
note on realm#3993, treating an extension as a class scope is a reasonable first
step that surfaces violations in member-access contexts (method bodies,
computed properties, closure-body references on the right-hand side) without
introducing false positives.

When the extended type is a plain identifier (`extension Foo`) or a member
type (`extension Foo.Bar`), push `.likeClass(name: extendedTypeName)` so
that references to that name inside the extension body are flagged and
corrected to `Self`. Complex extended-type shapes (generic specialisations,
opaque types, function types) continue to fall through to the existing
`.skipReferences` behaviour. The extension declaration header itself (the
extended-type identifier) is excluded from violations to avoid rewriting
`extension Foo` to `extension Self`.

Resolves realm#3993.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@itsybitsybootsy itsybitsybootsy force-pushed the fix/prefer-self-in-extensions branch from d89e068 to 95f0f1b Compare May 22, 2026 18:31
@itsybitsybootsy
Copy link
Copy Markdown
Author

comment #1 wasn't just a rename. the rule never matched member types on extensions, only single identifiers, so extension Foo.Bar flagged nothing and Outer.Inner.i slipped through. added structural matching for member types, so it now flags and fixes them to Self. that turned up one case in our own Configuration+Remote.swift, fixed in the diff. shortened the changelog, added the Outer.Inner.i correction.

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.

False negatives in prefer_self_in_static_references since 0.46.2

3 participants