Skip to content

[kotlin] New Rule: LocalVariableShadowsParameter #6732

@stokpop

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-ruleProposal to add a new built-in rule

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions