GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [kotlin] Fix #6732: Add LocalVariableShadowsParameter rule by stokpop · Pull Request #6733 · pmd/pmd

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jborgers reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@stokpop
[kotlin] add LocalVariableShadowsParameter rule
ad98b2d
@pmd-actions-helper

pmd-actions-helper Bot commented May 28, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen

UncleOwen commented May 30, 2026

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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".

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen

UncleOwen commented Jun 3, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel approved these changes Jun 4, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...va/net/sourceforge/pmd/lang/kotlin/rule/bestpractices/LocalVariableShadowsParameterRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/net/sourceforge/pmd/lang/kotlin/rule/bestpractices/LocalVariableShadowsParameterRule.java
Comment on lines +75 to +77
if (!KotlinAstUtil.isWithin(propDecl, KtFunctionDeclaration.class, func)) {
continue;
}

@adangel adangel Jun 4, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-kotlin/src/main/resources/category/kotlin/bestpractices.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@adangel
Update version from 7.25.0 to 7.26.0
605a7a2
Co-authored-by: Andreas Dangel <[email protected]>
@adangel adangel added this to the 7.26.0 milestone Jun 4, 2026
@adangel
adangel merged commit d7376c3 into pmd:main Jun 4, 2026
11 checks passed

Uh oh!

There was an error while loading. Please reload this page.

adangel added a commit that referenced this pull request Jun 4, 2026
@adangel
[kotlin] Fix #6732: Add LocalVariableShadowsParameter rule (#6733)
9da766f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.26.0

Development

Successfully merging this pull request may close these issues.

[kotlin] New Rule: LocalVariableShadowsParameter

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@stokpop @UncleOwen @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.