Skip to content

Warning for local variable shadowing #6228

Closed

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

Activity

  1. added
    rule-request
    on Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    rule-request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions