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:
Proposed Rule Name:
LocalVariableShadowsParameterProposed 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
AttributeViewclasses for typed attribute access, andKotlinAstUtilhelpers for navigating the Kotlin AST.Code Sample:
Possible Properties:
Not in scope / possible follow-up:
Related: