[kotlin] Fix #6732: Add LocalVariableShadowsParameter rule#6733
Conversation
|
Compared to main: (comment created at 2026-06-04 15:45:58+00:00 for 605a7a2) |
|
What do you think about adding other examples of shadowing to this rule? e.g. a block-scoped variable shadowing one defined at method scope? |
|
ok, an example would be: Yes, we can add this to this rule.
Shall I add it to this rule? |
|
I'll leave that up to you. I just wanted to mention the possibility. Both versions (one rule for all shadowing cases or separate rules) are fine. |
adangel
left a comment
There was a problem hiding this comment.
Thanks, looks good!
What do you think about adding other examples of shadowing to this rule? e.g. a block-scoped variable shadowing one defined at method scope?
Shall I add it to this rule?
If so, maybe name should then also change to "LocalVariableShadowing".
I'd say, we keep it simple for now. This can be added later if needed.
| if (!KotlinAstUtil.isWithin(propDecl, KtFunctionDeclaration.class, func)) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Just a note: In Java we have the concept of find boundaries, e.g. you can call node.descendants(KtPropertyDeclaration.class).crossFindBoundaries() to explicitly say: If there are nested classes, continue the search inside these and cross the find boundary.
By default I think, we do not cross the boundaries, so in theory, this if statement would not be necessary.
But it needs support in the AST and we probably don't have this feature in ANTLR. The AST nodes would need to implement net.sourceforge.pmd.lang.ast.Node#isFindBoundary, e.g. KtClassDeclaration would need to return true. But as we don't have AST nodes that we can change, I think adding the if statement is currently the only way to achieve this.
Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
Adds the
LocalVariableShadowsParameterrule to the Kotlin best practices ruleset. Detects local variables and lambda explicit parameters that shadow a parameter of any enclosing named function.Closes #6732.