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 — [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

@Will-6543 Will-6543 commented Aug 2, 2026 •
edited by adangel
Loading

Uh oh!

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

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

  • Fix [java] New Rule: Enforce local variable declarations to be at the start of a block #6882
  • Refs [java] Fix #6882: New Rule: Enforce that local variabe declarations at the start of a block are sorted #6924

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.

All reactions
Will-6543 added 12 commits August 2, 2026 11:36
@Will-6543
Initial tests and framework added
6809ce7
@Will-6543
Added new testcases and fixed old ones
7e70883
@Will-6543
Added logic to identify out of place declarations
96483b4
@Will-6543
Added additional example case and updated description
ef55f3d
@Will-6543
Added testcases from issue and added expected linenumbers to others
7ef0c42
@Will-6543
change behaviour to not blame innocent lines
3f2adea
@Will-6543
allow variable declarations inside switch statement cases
d1fa2cc
@Will-6543
fixed edge cases and restructured some logic
b3a93d7
@Will-6543
fixed naming convention issue
2a3b8c9
@Will-6543
removed leftover statement from debugging
99562c4
@Will-6543
updated rule description in line with guidelines
b037711
@Will-6543
updated violation message to be more neutral in line with guidelines
e693d6c
UncleOwen
UncleOwen requested changes Aug 7, 2026
View reviewed changes

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

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.

All reactions
Comment thread ...rceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableDeclarationShouldBeAtStartOfBlock.xml Resolved

Uh oh!

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

Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Resolved

Uh oh!

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

Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Outdated
Comment on lines +54 to +57
if (!declarationContainsInitialization || castedChild.hasInitializer()) {
firstFlaggedVarID = castedChild.getVarId();
break;
}

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

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.

All reactions
@Will-6543
Implemented suggested changes
0abe8f1
@Will-6543

Will-6543 commented Aug 8, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Will-6543
fixed checkstyle violation
b680a4d
@UncleOwen

UncleOwen commented Aug 8, 2026

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 8, 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 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)

All reactions

Sorry, something went wrong.

Uh oh!

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

UncleOwen
UncleOwen requested changes Aug 9, 2026
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Resolved

Uh oh!

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

Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

@Will-6543
Simplified loops and updated rule description
165fbb9
@UncleOwen UncleOwen added the a:new-rule Proposal to add a new built-in rule label Aug 9, 2026
@UncleOwen

UncleOwen commented Aug 9, 2026

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@Will-6543
Will-6543 force-pushed the issue-6882 branch from 8eaf70c to 165fbb9 Compare August 9, 2026 15:57
@Will-6543
changed position of rule reference
20de899
@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 reviewed Aug 15, 2026
View reviewed changes
Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

adangel
adangel reviewed Aug 15, 2026
View reviewed changes
Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

adangel
adangel reviewed Aug 15, 2026
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Resolved

Uh oh!

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

@adangel adangel mentioned this pull request Aug 15, 2026
[java] Fix #6882: New Rule: Enforce that local variabe declarations at the start of a block are sorted #6924
Closed
3 of 4 tasks
@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.

All reactions

Sorry, something went wrong.

Uh oh!

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

Will-6543 added 2 commits August 24, 2026 15:19
@Will-6543
Introduce sorting and version sensitivity to the rule
15d5db7
@Will-6543
added and updated tentative version numbers
15a1397
@Will-6543

Will-6543 commented Aug 26, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

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

adangel and others added 4 commits October 7, 2026 19:57
@adangel @UncleOwen @Will-6543
[java] Fix pmd#6737: Use next annotatable sibling for supressing top …
6254a4f
…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]>
@Will-6543
[java] Fix pmd#6900: Modifies isNullCheck to accept negated expressio…
f71a964
…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
@Will-6543
Removed preview version unit test due to tests crashing
d4176cf
@Will-6543
Merge branch 'main' into issue-6882
ae4fa50
@adangel adangel self-assigned this Oct 9, 2026
adangel added 10 commits October 9, 2026 08:26
@adangel
Merge branch 'main' into pr-6923
267d8ea
@adangel
Update versions to 7.29.0
a60ae91
@adangel
Refactor LocalVariableDeclarationShouldBeAtStartOfBlockRule:
224ad28
- replace asCtxt and use RuleContext
- simplify language version check
@adangel
Improve docs for LocalVariableDeclarationShouldBeAtStartOfBlockRule
c43cd7d
@adangel
Add example as test case
1c22abb
@adangel
Fix handling of requireBeforeThisSuper
e472edc
@adangel
var should be ignored
60a4e81
@adangel
Array types should be handled consistently
be0d896
@adangel
Remove unnecessary test case for fields
2ae9e97
@adangel
Update release notes (pmd#6882, pmd#6923)
9f2adfc
adangel
adangel reviewed Oct 9, 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.

I'll push my changes now....

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

Comment thread ...ceforge/pmd/lang/java/rule/codestyle/LocalVariableDeclarationShouldBeAtStartOfBlockRule.java Resolved

Uh oh!

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

Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Resolved

Uh oh!

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

Comment thread ...rceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableDeclarationShouldBeAtStartOfBlock.xml
</test-code>

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

@adangel adangel Oct 9, 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.

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.

All reactions
Comment thread ...rceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableDeclarationShouldBeAtStartOfBlock.xml Resolved

Uh oh!

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

Comment thread ...rceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableDeclarationShouldBeAtStartOfBlock.xml Resolved

Uh oh!

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

Comment thread ...rceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableDeclarationShouldBeAtStartOfBlock.xml Resolved

Uh oh!

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

@adangel
Simplify boolean return
165b259
@adangel adangel added this to the 7.29.0 milestone Oct 9, 2026
adangel
adangel reviewed Oct 18, 2026
View reviewed changes
Comment thread docs/pages/release_notes.md Resolved

Uh oh!

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

adangel added 2 commits October 18, 2026 17:10
@adangel
Merge branch 'main' into pr-6923
e83c66f
@adangel
Fix release notes
88c3765
@adangel
adangel merged commit 52fc535 into pmd:main Oct 18, 2026
2 checks passed

Uh oh!

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

@zbynek zbynek mentioned this pull request Oct 25, 2026
[java] Local Variables At Top #2850
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel left review comments
@UncleOwen UncleOwen UncleOwen requested changes

Uh oh!

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

Assignees

@adangel adangel

Labels

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

Projects

None yet

Milestone

7.29.0

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

Uh oh!

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

3 participants

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