Do not trigger `prefer_self_in_static_references` rule on collection types (#5055)

This commit is contained in:
Danny Mösch 2023-06-12 22:40:45 +02:00 committed by GitHub
parent e29de7a99f
commit f12b8d66d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 14 deletions

View File

@ -32,6 +32,11 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#5009](https://github.com/realm/SwiftLint/issues/5009)
* Do not trigger `prefer_self_in_static_references` rule on collection types in
classes, but on initializers like `[C]()` in all types.
[SimplyDanny](https://github.com/SimplyDanny)
[#5042](https://github.com/realm/SwiftLint/issues/5042)
* Fix false positives on `redundant_objc_attribute` rule for enums
and private members.
[Martin Redington](https://github.com/mildm8nnered)
@ -43,7 +48,7 @@
[#5023](https://github.com/realm/SwiftLint/pull/5023)
* Fix false positives on `sorted_first_last` rule when `first`/`last` have
a predicate.
a predicate.
[woxtu](https://github.com/woxtu)
[#3023](https://github.com/realm/SwiftLint/issues/3023)

View File

@ -118,10 +118,7 @@ private class Visitor: ViolationsSyntaxVisitor {
}
override func visitPost(_ node: IdentifierExprSyntax) {
guard let parent = node.parent,
!parent.is(SpecializeExprSyntax.self),
!parent.is(DictionaryElementSyntax.self),
!parent.is(ArrayElementSyntax.self) else {
guard let parent = node.parent, !parent.is(SpecializeExprSyntax.self) else {
return
}
if parent.is(FunctionCallExprSyntax.self), case .likeClass = parentDeclScopes.peek() {
@ -166,6 +163,13 @@ private class Visitor: ViolationsSyntaxVisitor {
parentDeclScopes.pop()
}
override func visit(_ node: ReturnClauseSyntax) -> SyntaxVisitorContinueKind {
if case .likeStruct = parentDeclScopes.peek() {
return .visitChildren
}
return .skipChildren
}
override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
parentDeclScopes.push(.likeStruct(node.identifier.text))
return .visitChildren
@ -179,9 +183,8 @@ private class Visitor: ViolationsSyntaxVisitor {
guard let parent = node.parent else {
return
}
if case .likeClass = parentDeclScopes.peek(),
parent.is(GenericArgumentSyntax.self) || parent.is(ReturnClauseSyntax.self) {
// Type is a generic parameter or the return type of a function.
if case .likeClass = parentDeclScopes.peek(), parent.is(GenericArgumentSyntax.self) {
// Type is a generic parameter in a class.
return
}
if node.genericArguments == nil {
@ -202,8 +205,10 @@ private class Visitor: ViolationsSyntaxVisitor {
return .skipChildren
}
if let varDecl = node.parent?.parent?.parent?.as(VariableDeclSyntax.self) {
if varDecl.parent?.is(CodeBlockItemSyntax.self) == true || varDecl.bindings.onlyElement?.accessor != nil {
// Is either a local variable declaration or a computed property.
if varDecl.parent?.is(CodeBlockItemSyntax.self) == true // Local variable declaration
|| varDecl.bindings.onlyElement?.accessor != nil // Computed property
|| !node.type.is(SimpleTypeIdentifierSyntax.self) // Complex or collection type
{
return .visitChildren
}
}

View File

@ -29,10 +29,6 @@ enum PreferSelfInStaticReferencesRuleExamples {
static let k = { C.i }()
let h = C.i
@GreaterThan(C.j) var k: Int
func f() {
_ = [Int: C]()
_ = [C]()
}
}
""", excludeFromDocumentation: true),
Example("""
@ -103,6 +99,14 @@ enum PreferSelfInStaticReferencesRuleExamples {
func f() -> Int { C.i + h }
}
"""),
Example("""
class C {
func f() {
_ = [C]()
_ = [Int: C]()
}
}
"""),
Example("""
struct S {
let j: Int
@ -149,31 +153,37 @@ enum PreferSelfInStaticReferencesRuleExamples {
typealias A = C
let d: C? = nil
var c: C { C() }
let b: [C] = [C]()
init() {}
func f(e: C) -> C {
let f: C = C()
return f
}
func g(a: [C]) -> [C] { a }
}
final class D {
typealias A = D
let c: D? = nil
var d: D { D() }
let b: [D] = [D]()
init() {}
func f(e: D) -> D {
let f: D = D()
return f
}
func g(a: [D]) -> [D] { a }
}
struct S {
typealias A = S
// let s: S? = nil // Struct cannot contain itself
var t: S { S() }
let b: [S] = [S]()
init() {}
func f(e: S) -> S {
let f: S = S()
return f
}
func g(a: [S]) -> [S] { a }
}
""", excludeFromDocumentation: true)
]