Skip to content

[java] AvoidDeeplyNestedIfStmts: fix false negative with if-else - #6100

Merged
oowekyala merged 1 commit into
pmd:mainfrom
zbynek:nested-if
Oct 19, 2025
Merged

oowekyala merged 1 commit into
pmd:mainfrom
zbynek:nested-if

Conversation

@zbynek

@zbynek zbynek commented Sep 27, 2025

Copy link
Copy Markdown
Contributor

Describe the PR

There were two problems with how if-else statements were handled by this rule:

  1. when if-else was encountered, the depth was not supposed to increase, but in fact it decreased. Meaning that code containing 10 if-else pairs was allowed to have completely unrelated if statement (without else) 10+2 levels deep and the rule wouldn't trigger.
  2. I think the intention was to allow long if - else if - else if -.... else chains, but it alse allowed arbitrary nesting of ifs as long as they had else branches.

The PR fixes both by increasing the depth when entering if branch and decreasing it right after. This could be further refined by also increasing the depth for else branches that contain more statements than a single if.

Related issues

Fixes #1499

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 Sep 27, 2025 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 48 violations,
introduces 2245 new violations, 0 new errors and 0 new configuration errors,
removes 26 violations, 25 errors and 8 configuration errors.

Regression Tester Report

(comment created at 2025-09-27 23:21:23+00:00 for def6517)

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

This looks good to me. The regression report has lots of new positives but AFAICS they are legit.

@oowekyala oowekyala added this to the 7.18.0 milestone Oct 17, 2025
@oowekyala oowekyala self-assigned this Oct 17, 2025
@oowekyala
oowekyala merged commit 0254c7c into pmd:main Oct 19, 2025
oowekyala added a commit that referenced this pull request Oct 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] AvoidDeeplyNestedIfStmts violations can be unintentionally undetected

2 participants