Skip to content

[java] Fix #1287: GuardLogStatement false positive with a guard clause - #6920

Merged
lukasgraef merged 6 commits into
pmd:mainfrom
Eljees:fix/guardlogstatement-guard-clause-1287
Aug 14, 2026
Merged

lukasgraef merged 6 commits into
pmd:mainfrom
Eljees:fix/guardlogstatement-guard-clause-1287

Conversation

@Eljees

@Eljees Eljees commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #1287

GuardLogStatement only recognizes the wrapping style of guard, so the guard-clause style is reported
even though the log call is unreachable when the level is disabled:

private void log(Object o) {
    if (!LOG.isDebugEnabled()) {
        return;
    }
    LOG.debug("logging from " + o);   // reported
}

hasGuard() looks for the guard among the ancestors of the log call:

for (ASTIfStatement ifStatement : node.ancestors(ASTIfStatement.class).take(2)) {
    if (containsGuardMethod(ifStatement, logLevel)) {
        return true;
    }
}
return false;

With an early return the guard is not an ancestor — it is a preceding sibling in the same block, so it
is never seen.

This adds a second lookup for that shape. To avoid weakening the rule it is deliberately narrow: the
preceding if counts as a guard only when it has no else branch, its condition is a negation
containing the guard method for the same log level, and its then-branch always leaves the block
(return / throw / continue / break, or a block ending in one of those).

Before / after

Three cases added to GuardLogStatement.xml — the reported shape, the same without braces, and a
negative control where the if body does not exit and the call therefore stays unguarded:

before:  Test Failure: guard clause with early return, slf4j (#1287)
           -> Expected 0 problem(s), 1 problem(s) found.
         Test Failure: guard clause without braces, early return (#1287)
           -> Expected 0 problem(s), 1 problem(s) found.

after:   Tests run: 41, Failures: 0, Errors: 0, Skipped: 0

The control case passes both before and after, so the change narrows the rule only for guards that
really do skip the statement. Run with mvn -pl pmd-java test -Dtest=GuardLogStatementTest on
temurin-25.

AI-assisted (LLM used for drafting); the change and both runs above are mine.

…ause

hasGuard only looked for the guard among the ancestors of the log call, so the early-return style guard - a preceding sibling that leaves the block - was not recognized.

Signed-off-by: Eljees <[email protected]>
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 3 violations, 16 errors and 9 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-08-14 18:43:50+00:00 for 9e217e0)

@Eljees

Eljees commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Ping - open since 30 July, no review yet.

Every Build Pull Request job is green on 766c29a: compile, spelling, verify, unit tests on all three OSes, dogfood and documentation. The one red check is Regression Tester, and I would rather flag it than let it sit unexplained. This PR changes when GuardLogStatement reports, so some diff in the regression report is expected by construction - but whether the diff it produced is the intended one needs a reviewer who knows the rule.

Details and the before/after are in the description. Happy to rebase, or to change the approach if this is not how you would want it fixed.

@lukasgraef

Copy link
Copy Markdown
Member

Hi @Eljees,

the Regression tester check is shown as failing. Can you update your branch with main so that it runs again? The workflow seemed to have some problems a few days ago, but it should work again now.

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

Just had a first look.

Eljees and others added 2 commits August 9, 2026 09:08
Addresses the review on pmd#6920.

- hasEarlyExitGuard now walks up the statement ancestors instead of looking
  only at the immediate siblings of the log statement, so an early-exit guard
  also covers log statements nested deeper than the guard itself - for example
  inside a loop that follows it. This is the case reported in review.
- alwaysExits uses children(...).last() and lets the recursive instanceof
  handle the null case, as suggested.

Test cases added:

- log inside a loop after the guard (the reported case)
- log nested several blocks deep after the guard
- a continue guard inside a loop does not guard a log after the loop
- a guard in a sibling if-block does not guard a later log statement

The last two are negative cases: they pin that walking up the ancestors does
not start swallowing real violations. Both already passed before this change,
so they fail if the walk is ever made too permissive.
@Eljees

Eljees commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Branch updated with main (bca7cdf), so the Regression Tester should run again, and both review points are in 49135ea.

GuardLogStatementTest is 45 tests green locally. Before the rule change, with the new test cases already in place, exactly the two new positive cases failed and the other 43 passed - so the red/green is on the new behaviour only, not on a different test set.

@Eljees

Eljees commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

@lukasgraef the branch update did what you asked — the Regression Tester ran again and is no longer red. It reports neutral now rather than failing, and Build Pull Request / regressiontester passed in 4m along with the other 11 jobs.

The report on 49135ea: 0 new violations, 0 new errors, 0 new configuration errors; 3 violations, 16 errors and 9 configuration errors removed. Nothing new appears, which is the direction this fix should move the rule in.

Both of your review points are in 49135ea as well. Anything else you need before this can move?

@lukasgraef lukasgraef 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!
I will fix the two small findings while merging.

@lukasgraef lukasgraef added this to the 7.27.0 milestone Aug 14, 2026
@lukasgraef
lukasgraef merged commit 2789d74 into pmd:main Aug 14, 2026
13 checks passed
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] GuardLogStatement: False positive when using negative guard conditions

2 participants