Skip to content

Warning for local variable shadowing #6228

@aidan-waite

Description

@aidan-waite

New Issue Checklist

Feature or Enhancement Proposal

Variable shadowing is an anti-pattern because it makes code harder to understand. It is a compiler error in many languages. I think it would be great to warn against it in SwiftLint. Here is an example:

var b: String? = "thank"

test(a:"you")

func test(a: String?) {
    if let b = a {
        print("b:\(b)")
        let b = "wow"
        print("b:\(b)")
    }
}
  • The line if let b = a { shadows the above variable b
  • The line let b = "wow" redeclares b as a new variable

In my opinion both of these should be covered by SwiftLint.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    rule-requestRequests for a new rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions