From 16ef0aa0ff8a862c2015112fbfa4d5c42e2d139a Mon Sep 17 00:00:00 2001 From: Swastik Tripathi Date: Sat, 26 Sep 2026 09:09:18 +0530 Subject: [PATCH] Ignore module-selected types in `prefer_self_in_static_references` A type written with a module selector (`Foundation::TimeInterval`) names a type from that module, so it shouldn't be reported (or corrected to `Self`) just because its name matches the surrounding type. Fixes #6924. Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 6 ++++++ .../Style/PreferSelfInStaticReferencesRule.swift | 7 +++++-- .../PreferSelfInStaticReferencesRuleExamples.swift | 13 +++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50835855b4..6234857b27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,12 @@ [Hokila](https://github.com/Hokila) [#6897](https://github.com/realm/SwiftLint/issues/6897) +* Stop `prefer_self_in_static_references` from reporting types referenced with + a module selector such as `Foundation::TimeInterval`, which name a type from + that module rather than the surrounding type. + [SwastikTripathi](https://github.com/SwastikTripathi) + [#6924](https://github.com/realm/SwiftLint/issues/6924) + ## 0.65.1: Fresh Folded Fixtures ### Breaking diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift index ecb9aa0ac5..51400ae365 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift @@ -158,7 +158,8 @@ private extension PreferSelfInStaticReferencesRule { override func visitPost(_ node: DeclReferenceExprSyntax) { guard let parent = node.parent, !parent.is(GenericSpecializationExprSyntax.self), - node.keyPathInParent != \MemberAccessExprSyntax.declName else { + node.keyPathInParent != \MemberAccessExprSyntax.declName, + node.moduleSelector == nil else { return } if parent.is(FunctionCallExprSyntax.self), case .likeClass = parentDeclScopes.peek() { @@ -293,7 +294,9 @@ private extension PreferSelfInStaticReferencesRule { } override func visitPost(_ node: IdentifierTypeSyntax) { - guard let parent = node.parent else { + // A module selector (`Module::Name`) names a type from that module, + // which isn't necessarily the surrounding type. + guard let parent = node.parent, node.moduleSelector == nil else { return } // Don't flag identifiers that belong to the extension declaration diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRuleExamples.swift index 074dbc9487..dbbf6b3fe2 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRuleExamples.swift @@ -225,6 +225,19 @@ enum PreferSelfInStaticReferencesRuleExamples { } } """.asExample(excludeFromDocumentation: true), + """ + enum TimeInterval { + private static func defaultInterval() -> Foundation::TimeInterval { + .init() + } + } + """.asExample(excludeFromDocumentation: true), + """ + struct Date { + static let reference = Foundation::Date.distantPast + static let parsed: Foundation::Date? = nil + } + """.asExample(excludeFromDocumentation: true), ]) static let triggeringExamples = #examples([