Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
pattern matching (e.g. if case, switch case, for case, catch).
[GandaLF2006](https://github.com/GandaLF2006)

* Avoid false positives in `prefer_self_in_static_references` when a nested type
shadows its enclosing type name.
[theamodhshetty](https://github.com/theamodhshetty)
[#5917](https://github.com/realm/SwiftLint/issues/5917)

## 0.63.2: High-Speed Extraction

### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private extension PreferSelfInStaticReferencesRule {
private var variableDeclScopes = Stack<VariableDeclBehavior>()

override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
parentDeclScopes.push(.likeClass(name: node.name.text))
pushParentDeclScope(.likeClass(name: node.name.text), memberBlock: node.memberBlock)
return .skipChildren
}

Expand All @@ -56,7 +56,7 @@ private extension PreferSelfInStaticReferencesRule {
}

override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
parentDeclScopes.push(.likeClass(name: node.name.text))
pushParentDeclScope(.likeClass(name: node.name.text), memberBlock: node.memberBlock)
return .visitChildren
}

Expand All @@ -74,7 +74,7 @@ private extension PreferSelfInStaticReferencesRule {
}

override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
parentDeclScopes.push(.likeStruct(node.name.text))
pushParentDeclScope(.likeStruct(node.name.text), memberBlock: node.memberBlock)
return .visitChildren
}

Expand Down Expand Up @@ -162,7 +162,7 @@ private extension PreferSelfInStaticReferencesRule {
}

override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
parentDeclScopes.push(.likeStruct(node.name.text))
pushParentDeclScope(.likeStruct(node.name.text), memberBlock: node.memberBlock)
return .visitChildren
}

Expand Down Expand Up @@ -213,7 +213,8 @@ private extension PreferSelfInStaticReferencesRule {
}

private func addViolation(on node: TokenSyntax) {
if let parentName = parentDeclScopes.peek()?.parentName, node.tokenKind == .identifier(parentName) {
if let parentName = parentDeclScopes.peek()?.parentName,
node.tokenKind == .identifier(parentName) {
violations.append(
at: node.positionAfterSkippingLeadingTrivia,
correction: .init(
Expand All @@ -224,5 +225,25 @@ private extension PreferSelfInStaticReferencesRule {
)
}
}

private func pushParentDeclScope(_ behavior: ParentDeclBehavior, memberBlock: MemberBlockSyntax) {
let hasShadowingNestedType =
if let name = behavior.parentName {
containsSameNamedNestedType(named: name, in: memberBlock)
} else {
false
}
parentDeclScopes.push(hasShadowingNestedType ? .skipReferences : behavior)
}

private func containsSameNamedNestedType(named name: String, in memberBlock: MemberBlockSyntax) -> Bool {
memberBlock.members.contains { member in
if member.decl.isProtocol((any DeclGroupSyntax).self) || member.decl.is(TypeAliasDeclSyntax.self) {
return member.decl.asProtocol((any NamedDeclSyntax).self)?.name.text == name
}

return false
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ enum PreferSelfInStaticReferencesRuleExamples {
}
}
""", excludeFromDocumentation: true),
Example("""
struct S1 {
struct S1 {}
var s = S1()
Comment thread
SimplyDanny marked this conversation as resolved.
}
""", excludeFromDocumentation: true),
Example("""
struct S1 {
var s = S1()
struct S1 {}
}
""", excludeFromDocumentation: true),
]

static let triggeringExamples = [
Expand Down