Skip to content

[kotlin] New Rule: LocalVariableShadowsParameter #6732

Closed

Description

@stokpop

Proposed Rule Name: LocalVariableShadowsParameter

Proposed Category: Best Practices

Description:
Detects local variable declarations and lambda explicit parameters that shadow a parameter of any enclosing named function. Shadowing a parameter with a local variable of the same name may lead to confusion about which value is actually used.

This rule also serves as a demonstration of the new Kotlin AST features: custom AttributeView classes for typed attribute access, and KotlinAstUtil helpers for navigating the Kotlin AST.

Code Sample:

// violation - local variable 'result' shadows function parameter 'result'
fun compute(result: Int): Int {
    val result = 42
    return result
}

// no violation - different name
fun compute2(input: Int): Int {
    val result = input + 1
    return result
}

// violation - lambda explicit parameter shadows outer function parameter
fun process(result: Int): Int {
    val transform = { result: Int -> result + 1 }
    return transform(result)
}

Possible Properties:

  • No properties planned — the rule has no configurable threshold or pattern.

Not in scope / possible follow-up:

  • Local variable hiding a class field — this is a related but distinct case that requires type/classpath resolution and is better handled by a separate rule.

Related:

Activity

  1. added
    a:new-rule
    on Jun 4, 2026
  2. added this to the 7.26.0 milestone on Jun 4, 2026
  3. added a commit that references this issue on Jun 4, 2026
  4. added a commit that references this issue on Jun 4, 2026
    9da766f
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

    a:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions