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] New rule: UnnecessaryBlock by UncleOwen · Pull Request #6639 · pmd/pmd

Conversation

@UncleOwen

@UncleOwen UncleOwen commented May 2, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Describe the PR

New rule that flags unnecessary blocks. A block is necessary if it is the body of a method/control structure etc., or if it is used to restrict the scope of a variable. All other blocks are deemed unnecessary.

Related issues

  • Fix [java] New rule: UnnecessaryBlock #6622
  • This rule would have caught the issues described in [java] SimplifyBooleanReturns: False negative when if-return is wrapped inside unnecessary anonymous block #6519 and [java] UselessOverridingMethod: False negative when super call is wrapped in nested blocks #6491

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.

zbynek reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@UncleOwen
New rule: UnnecessaryBlock
624ec75
@pmd-actions-helper

pmd-actions-helper Bot commented May 2, 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 120 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-08 14:03:19+00:00 for 8bdd308)

All reactions

Sorry, something went wrong.

Uh oh!

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

oowekyala
oowekyala reviewed May 4, 2026
View reviewed changes
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryBlockRule.java Resolved

Uh oh!

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

@UncleOwen @oowekyala
Apply suggestion from @oowekyala
b416d35
Co-authored-by: Clément Fournier <[email protected]>
@zbynek

zbynek commented May 5, 2026

Copy link
Copy Markdown
Contributor

Sometimes copy-paste errors can create

if (foo) {{
   int i =1;
   System.exit(i);
}
}

which might make sense to also catch (i.e. if parent size is 1, report it without checking for variable definitions).

The Spring regressions show another usecase where blocks are used to limit scope of anonymous classes -- should that be acceptable?

All reactions

Sorry, something went wrong.

Uh oh!

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

@UncleOwen
UncleOwen marked this pull request as draft May 6, 2026 10:58
@UncleOwen

UncleOwen commented May 6, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Thanks. Adding checks for the {{ ... }} case is definitely a good idea. Although there might be overlap with DoubleBraceInitialization.

I did look at the spring framework regression reports... and completely missed the anonymous inner local classes. You're completely right that that shouldn't be a violation.

All reactions

Sorry, something went wrong.

Uh oh!

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

UncleOwen added 3 commits May 6, 2026 13:35
@UncleOwen
Providing a scope for a local class is another reason for using a block
97684ae
@UncleOwen
Refactor
937b7b4
@UncleOwen
Also violate if one ASTBlock is the only child of another ASTBlock.
0dc7d60
This is likely caused by a typo.
@UncleOwen UncleOwen added the a:new-rule Proposal to add a new built-in rule label May 6, 2026
@UncleOwen
UncleOwen marked this pull request as ready for review May 6, 2026 19:16
UncleOwen added 2 commits May 6, 2026 21:18
@UncleOwen
Stick to the naming convention:
fe9e9ef
If it's not a violation, the method should not be called "bad", but "good"
@UncleOwen
Add import so that this would compile in the real world.
25e5db1
adangel
adangel requested changes May 8, 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.

Thanks!

The functionality looks good, we need to improve the docs.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryBlockRule.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 pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryBlockTest.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 ...ava/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryBlock.xml Resolved

Uh oh!

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

@adangel adangel added the needs:user-input Maintainers are waiting for feedback from author label May 8, 2026
UncleOwen and others added 4 commits May 8, 2026 15:23
@UncleOwen @adangel
Update pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/code…
f62041e
…style/UnnecessaryBlockRule.java

Co-authored-by: Andreas Dangel <[email protected]>
@UncleOwen @adangel
Update pmd-java/src/main/resources/category/java/codestyle.xml
b24ced8
Co-authored-by: Andreas Dangel <[email protected]>
@UncleOwen
JUnit5 test shouldn't be public.
fb030ea
@UncleOwen
update description
8bdd308
@adangel adangel mentioned this pull request May 14, 2026
[java] SimplifyBooleanReturns: False negative when if-return is wrapped inside unnecessary anonymous block #6519
Closed
@adangel adangel added this to the 7.25.0 milestone May 14, 2026
@adangel adangel removed the needs:user-input Maintainers are waiting for feedback from author label May 14, 2026
adangel
adangel approved these changes May 14, 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.

Thanks!

Sorry, something went wrong.

Uh oh!

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

All reactions
@adangel
adangel merged commit c90747d into pmd:main May 14, 2026
11 checks passed

Uh oh!

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

adangel added a commit that referenced this pull request May 14, 2026
@adangel
[doc] Update release notes (#6622, #6639)
13f4cae
adangel added a commit that referenced this pull request May 14, 2026
@adangel
[java] New rule: UnnecessaryBlock (#6639)
532aae9
@UncleOwen
UncleOwen deleted the issue-6622-UnnecessaryBlock branch May 14, 2026 18:21
@UncleOwen UncleOwen mentioned this pull request May 15, 2026
[java] UselessOverridingMethod: False negative when super call is wrapped in nested blocks #6491
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@oowekyala oowekyala oowekyala left review comments
@adangel adangel adangel approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

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

Projects

None yet

Milestone

7.25.0

Development

Successfully merging this pull request may close these issues.

[java] New rule: UnnecessaryBlock

Uh oh!

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

4 participants

@UncleOwen @zbynek @adangel @oowekyala
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.