Skip to content

[java] Fix #6882: New Rule: Enforce that local variables are declared at the start of their scope without initialization - #6923

Merged
adangel merged 35 commits into
pmd:mainfrom
Will-6543:issue-6882
Oct 18, 2026
Merged

adangel merged 35 commits into
pmd:mainfrom
Will-6543:issue-6882

Conversation

@Will-6543

@Will-6543 Will-6543 commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

Creates a new rule LocalVariableDeclarationShouldBeAtStartOfBlock which enforces a two things:

  1. Local variables are not declared on the same line that they are initialised - this avoids clutter in the declarations which if allowed would defeat the purpose of the rule
  2. All local variables are declared together at the start of their scope, not interleaved throughout

This does not include variables declared and initialised at once inside for loop, foreach loop and try-with-resources initialisers.

This rule does not enforce sorting of the declarations as suggested in the issue, I will implement that as a separate rule with a separate PR as it is too distinct from the core functionality of this rule.

The current message is "Local variable variableName is not declared without initialisation at start of block" though this is very much open to suggestions.

I have tried to use the American "initialized" throughout as opposed to "initialised" however some may have slipped through the cracks.

Related issues

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)

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

I also don't understand why you want the sorted check to be a different rule - given that it only makes sense in the context of this extremely unusual code convention.

Comment on lines +54 to +57
if (!declarationContainsInitialization || castedChild.hasInitializer()) {
firstFlaggedVarID = castedChild.getVarId();
break;
}

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.

Is there a reason you want only one violation per ASTLocalVariableDeclaration? I would have expected one violation per ASTVariableDeclarator - or a violation message that is worded differently.

@Will-6543

Copy link
Copy Markdown
Contributor Author

I also don't understand why you want the sorted check to be a different rule - given that it only makes sense in the context of this extremely unusual code convention.

The main reasons were that it:

  1. Felt like this rule would then be flagging too many different issues.
  2. Allows the sorting rule to apply to fields as well.

However I am happy to combine both into one if that is preferable.

@UncleOwen

Copy link
Copy Markdown
Member

Thanks, I agree with both your reasons. Let's keep the rules separate.

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 156191 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-10-18 15:33:22+00:00 for 88c3765)

@UncleOwen UncleOwen added the a:new-rule Proposal to add a new built-in rule label Aug 9, 2026
@UncleOwen

Copy link
Copy Markdown
Member

See #6961 for what is going on with the rules doc.

@adangel adangel changed the title [java] New Rule #6882: Enforce that local variables are declared at the start of their scope without initialization [java] Fix #6882: New Rule: Enforce that local variables are declared at the start of their scope without initialization Aug 15, 2026
@adangel

adangel commented Aug 15, 2026

Copy link
Copy Markdown
Member

I also don't understand why you want the sorted check to be a different rule - given that it only makes sense in the context of this extremely unusual code convention.

The main reasons were that it:

1. Felt like this rule would then be flagging too many different issues.

2. Allows the sorting rule to apply to fields as well.

However I am happy to combine both into one if that is preferable.

Actually, I would prefer to add a sorting option to this rule as well.

  1. Sorting only makes sense, if you define groups. This rule explicitly requires to group local variables into blocks - IMHO the sorting is a natural feature of this. You would not want to sort, without grouping first.

  2. If sorting is implemented, then this PR exactly matches the original requirements in [java] New Rule: Enforce local variable declarations to be at the start of a block #6882 and it can be closed. This means, this PR solves a real requirement.

    The other rule/PR ([java] Fix #6882: New Rule: Enforce that local variabe declarations at the start of a block are sorted #6924) can be seen completely separate from this and we can remove the relation to [java] New Rule: Enforce local variable declarations to be at the start of a block #6882 there ([java] Fix #6882: New Rule: Enforce that local variabe declarations at the start of a block are sorted #6924 won't fix [java] New Rule: Enforce local variable declarations to be at the start of a block #6882 then, it solves a different problem - scope creep?). Maybe the other rule is a massive generalization of this concrete rule?

  3. This rule explicitly concentrates only on local variables (not like the other rule also fields), which makes sorting a straightforward feature. The rule name includes the word "local variable", so the rule won't be extended in the future to other declarations, which would require to think about sorting (sorting inside a block, across the whole file)

By the way - (caution: scope creep again). This rule could also be extended to look at the indentation - there could be users, who want to align the identifiers of the variables, e.g.

int    someNumber;
String someName;

This could be a future property, to also enforce the block formatting in that way.

@Will-6543

Copy link
Copy Markdown
Contributor Author

The tests all pass locally. When the checks run here I get sent two email notifications, one with only documentation failing and one with documentation+tests failing.
Not really sure why the tests are unable to run here.

adangel and others added 4 commits October 7, 2026 19:57
…level (pmd#6902)

* [java] Fix pmd#6737: Use next annotatable sibling for supressing top level

* Fix spelling

---------

Co-authored-by: Sören Glimm <[email protected]>
…ns (pmd#6917)

* strip negations from around an expression before check

* resolved endless loop

* resolved code style issue

* added unit tests for nullCheck

* fixed bad tests

* fixes style violations and targets tests at non-overloaded method

* Added full negation testcase

* Added nested negation testcase

* added issue to release notes
@adangel adangel self-assigned this Oct 9, 2026

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

I'll push my changes now....

</test-code>

<test-code>
<description>When a declaration has multiple violations they should all be flagged</description>

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.

We could think about whether to report a combined violation, that has both reasons in the message. But I'd rather do that later and keep it verbose like now.

@adangel adangel added this to the 7.29.0 milestone Oct 9, 2026
Comment thread docs/pages/release_notes.md Resolved
@adangel
adangel merged commit 52fc535 into pmd:main Oct 18, 2026
2 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.

[java] New Rule: Enforce local variable declarations to be at the start of a block

3 participants