Skip to content

[kotlin] Fix #6732: Add LocalVariableShadowsParameter rule#6733

Merged
adangel merged 2 commits into
pmd:mainfrom
stokpop:feature/kotlin-local-variable-shadows-parameter
Jun 4, 2026
Merged

[kotlin] Fix #6732: Add LocalVariableShadowsParameter rule#6733
adangel merged 2 commits into
pmd:mainfrom
stokpop:feature/kotlin-local-variable-shadows-parameter

Conversation

@stokpop

@stokpop stokpop commented May 28, 2026

Copy link
Copy Markdown
Contributor

Adds the LocalVariableShadowsParameter rule to the Kotlin best practices ruleset. Detects local variables and lambda explicit parameters that shadow a parameter of any enclosing named function.

Closes #6732.

@pmd-actions-helper

pmd-actions-helper Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-06-04 15:45:58+00:00 for 605a7a2)

@UncleOwen

Copy link
Copy Markdown
Member

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?

@stokpop

stokpop commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

ok, an example would be:

fun process(items: List<String>) {
    val result = "default"   // method-scope variable

    for (item in items) {
        val result = item.trim()  // shadows method-scope 'result' -- block-scope shadowing
        println(result)
    }
}

Yes, we can add this to this rule.

  • Or make it a separate rule for better (prio/active) control and reporting lines, or too fine grained?
  • Or optionally make a property to enable/disable this type (e.g. "checkLocalShadowing").

Shall I add it to this rule?
If so, maybe name should then also change to "LocalVariableShadowing".

@UncleOwen

Copy link
Copy Markdown
Member

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 adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +75 to +77
if (!KotlinAstUtil.isWithin(propDecl, KtFunctionDeclaration.class, func)) {
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pmd-kotlin/src/main/resources/category/kotlin/bestpractices.xml Outdated
Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
@adangel adangel added this to the 7.26.0 milestone Jun 4, 2026
@adangel adangel merged commit d7376c3 into pmd:main Jun 4, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[kotlin] New Rule: LocalVariableShadowsParameter

3 participants