Skip to content

[java] New rule: VariableDeclarationUsageDistance#6640

Merged
adangel merged 8 commits into
pmd:mainfrom
zbynek:distance
May 14, 2026
Merged

[java] New rule: VariableDeclarationUsageDistance#6640
adangel merged 8 commits into
pmd:mainfrom
zbynek:distance

Conversation

@zbynek
Copy link
Copy Markdown
Contributor

@zbynek zbynek commented May 3, 2026

Describe the PR

Similar to PrematureDeclaration, but instead of checking whether the declaration can be moved after a return/throw, it checks whether it can be moved by a significant number of statements.

Violation of this rule suggests that a method is doing many unrelated things.

Relates to Google style guide paragraph 4.8.2.2 https://checkstyle.sourceforge.io/styleguides/google-java-style-20250426/javaguide.html#s4.8.2.2-variables-limited-scope

Equivalent to Checkstyle's VariableDeclarationUsageDistance

Related issues

  • Fix #

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-actions-helper
Copy link
Copy Markdown
Contributor

pmd-actions-helper Bot commented May 3, 2026

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 833 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-05-09 15:00:43+00:00 for af55b15)

@zbynek zbynek marked this pull request as ready for review May 3, 2026 18:34
Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Outdated
Co-authored-by: UncleOwen <[email protected]>
@zbynek zbynek changed the title [java] new rule: VariableDeclarationDistance [java] new rule: VariableDeclarationUsageDistance May 5, 2026
@zbynek zbynek added the a:new-rule Proposal to add a new built-in rule label May 6, 2026
@adangel adangel changed the title [java] new rule: VariableDeclarationUsageDistance [java] New rule: VariableDeclarationUsageDistance May 8, 2026
Copy link
Copy Markdown
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

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

}
}
if (!referencedEarly) {
asCtx(data).addViolation(node, id.getName());
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.

Should we report in the message the max_distance value as well? For the rules that are related to metrics, we had a feature request to always report the actual value and the configured limit.

I think, we can't report the actual distance from declaration to first usage, can we? Would this be too expensive?

E.g. something like "Declaration of ''{0}'' is ''{1}'' statements away from its first usage. It can be moved closer. The max distance allowed is ''{2}''".

Comment thread pmd-java/src/main/resources/category/java/codestyle.xml
public class Bar {
public int lengthSumOf(String[] strings) {
int sum = 0; // premature
final int offset = 42; // this is final: not a problem
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.

Can you explain the reasoning behind why final local vars are excluded? I think, we should put this into the rule description.

Atm I don't see a reason, why offset shouldn't be moved right before the return statement...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I guess the main reason is that sometimes you want to make a "snapshot" of some object's state before that object is changed and use that snapshot after. Checkstyle rule has a setting for this, by default distances for final variables are ignored there (I didn't find any additional reasoning in sevntu-checkstyle/sevntu.checkstyle#21 where that rule was introduced).

@adangel adangel added the needs:user-input Maintainers are waiting for feedback from author label May 8, 2026
zbynek and others added 2 commits May 8, 2026 21:30
Co-authored-by: Andreas Dangel <[email protected]>
@zbynek zbynek requested a review from adangel May 13, 2026 22:50
@zbynek zbynek removed the needs:user-input Maintainers are waiting for feedback from author label May 13, 2026
@adangel adangel added this to the 7.25.0 milestone May 14, 2026
Copy link
Copy Markdown
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

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

Thanks!

I'll do the minor cleanups myself.

Comment thread docs/pages/release_notes.md
Comment thread docs/pages/release_notes.md
adangel added a commit that referenced this pull request May 14, 2026
@adangel adangel merged commit f0bbe27 into pmd:main May 14, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants