GeezerVison is showing github.com — [java] Fix #6882: New Rule: Enforce that local variables are declared at the start of their scope without initialization by Will-6543 · Pull Request #6923 · pmd/pmd
Conversation
Will-6543
commented
Aug 2, 2026
•
edited by adangel
Loading
Uh oh!
There was an error while loading. Please reload this page.
Describe the PR
Creates a new rule LocalVariableDeclarationShouldBeAtStartOfBlock which enforces a two things:
- 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
- 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)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+54
to
+57
|
|
if (!declarationContainsInitialization || castedChild.hasInitializer()) {
|
|
|
firstFlaggedVarID = castedChild.getVarId();
|
|
|
break;
|
|
|
}
|
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.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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:
- Felt like this rule would then be flagging too many different issues.
- Allows the sorting rule to apply to fields as well.
However I am happy to combine both into one if that is preferable.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks, I agree with both your reasons. Let's keep the rules separate.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
See #6961 for what is going on with the rules doc.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
-
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.
-
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?
-
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…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
- replace asCtxt and use RuleContext
- simplify language version check
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.
I'll push my changes now....
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
|
</test-code>
|
|
|
|
|
|
<test-code>
|
|
|
<description>When a declaration has multiple violations they should all be flagged</description>
|
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.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
adangel
adangel left review comments
Uh oh!
There was an error while loading. Please reload this page.
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.
|