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([