Skip to content

Commit

Permalink
Handle chained MemberAccessExprSyntax in variable declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
tonell-m committed Jan 5, 2024
1 parent fd046b6 commit 8771e14
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct RedundantTypeAnnotationRule: SwiftSyntaxCorrectableRule, OptInRule {
Example("var one: A<T> = B()"),
Example("var one: A = B<T>()"),
Example("var one: A<T> = B<T>()"),
Example("let a = A.b.c.d"),
Example("let a: B = A.b.c.d"),
Example("""
enum Direction {
case up
Expand Down Expand Up @@ -73,6 +75,7 @@ struct RedundantTypeAnnotationRule: SwiftSyntaxCorrectableRule, OptInRule {
Example("if var set↓: Set<Int> = Set<Int>.init([]) { return }"),
Example("var set↓: Set = Set<Int>([]), otherSet: Set<Int>"),
Example("var num↓: Int = Int.random(0..<10)"),
Example("let a↓: A = A.b.c.d"),
Example("""
class ViewController: UIViewController {
func someMethod() {
Expand Down Expand Up @@ -123,6 +126,8 @@ struct RedundantTypeAnnotationRule: SwiftSyntaxCorrectableRule, OptInRule {
Example("if var set = Set<Int>.init([]) { return }"),
Example("var set↓: Set = Set<Int>([]), otherSet: Set<Int>"):
Example("var set = Set<Int>([]), otherSet: Set<Int>"),
Example("let a↓: A = A.b.c.d"):
Example("let a = A.b.c.d"),
Example("""
class ViewController: UIViewController {
func someMethod() {
Expand Down Expand Up @@ -304,6 +309,12 @@ private extension TypeAnnotationSyntax {
return genericSpecialization.expression.trimmedDescription == type.typeName
}

if base.is(MemberAccessExprSyntax.self) {
// In the case of chained MemberAccessExprSyntax (e.g. let a: A = A.b.c), call this function recursively
// with the base sequence as root node (in this case A.b).
return isMemberAccessViolation(node: base, type: type)
}

return base.trimmedDescription == type.trimmedDescription
}
}
Expand Down

0 comments on commit 8771e14

Please sign in to comment.