Repository navigation
[java] Fix #6882: New Rule: Enforce that local variables are declared at the start of their scope without initialization - #6923
Conversation
UncleOwen
left a comment
There was a problem hiding this comment.
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.
| if (!declarationContainsInitialization || castedChild.hasInitializer()) { | ||
| firstFlaggedVarID = castedChild.getVarId(); | ||
| break; | ||
| } |
There was a problem hiding this comment.
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.
The main reasons were that it:
However I am happy to combine both into one if that is preferable. |
|
Thanks, I agree with both your reasons. Let's keep the rules separate. |
|
Compared to main: (comment created at 2026-10-18 15:33:22+00:00 for 88c3765) |
|
See #6961 for what is going on with the rules doc. |
Actually, I would prefer to add a sorting option to this rule as well.
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. This could be a future property, to also enforce the block formatting in that way. |
|
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. |
…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
| </test-code> | ||
|
|
||
| <test-code> | ||
| <description>When a declaration has multiple violations they should all be flagged</description> |
There was a problem hiding this comment.
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.
Describe the PR
Creates a new rule
LocalVariableDeclarationShouldBeAtStartOfBlockwhich enforces a two things: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
variableNameis 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?
./mvnw clean verifypasses (checked automatically by github actions)