Skip to content

[java] New rule: UnnecessaryBlock#6639

Merged
adangel merged 11 commits into
pmd:mainfrom
UncleOwen:issue-6622-UnnecessaryBlock
May 14, 2026
Merged

[java] New rule: UnnecessaryBlock#6639
adangel merged 11 commits into
pmd:mainfrom
UncleOwen:issue-6622-UnnecessaryBlock

Conversation

@UncleOwen

@UncleOwen UncleOwen commented May 2, 2026

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

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)

@pmd-actions-helper

pmd-actions-helper Bot commented May 2, 2026

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)

Co-authored-by: Clément Fournier <clem.fournier@proton.me>
@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?

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

UncleOwen commented May 6, 2026

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.

@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
If it's not a violation, the method should not be called "bad", but "good"

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

Thanks!

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

Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Outdated
Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Outdated
@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
…style/UnnecessaryBlockRule.java

Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
Co-authored-by: Andreas Dangel <andreas.dangel@adangel.org>
@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 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.

Thanks!

@adangel adangel merged commit c90747d into pmd:main May 14, 2026
13 checks passed
adangel added a commit that referenced this pull request May 14, 2026
adangel added a commit that referenced this pull request May 14, 2026
@UncleOwen UncleOwen deleted the issue-6622-UnnecessaryBlock branch May 14, 2026 18:21
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: UnnecessaryBlock

4 participants