Repository navigation
[java] Fix #1287: GuardLogStatement false positive with a guard clause - #6920
Conversation
…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]>
|
Compared to main: (comment created at 2026-08-14 18:43:50+00:00 for 9e217e0) |
|
Ping - open since 30 July, no review yet. Every 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. |
|
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. |
… Regression Tester
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.
|
Branch updated with
|
|
@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 The report on Both of your review points are in |
lukasgraef
left a comment
There was a problem hiding this comment.
Thanks!
I will fix the two small findings while merging.
Fixes #1287
GuardLogStatementonly recognizes the wrapping style of guard, so the guard-clause style is reportedeven though the log call is unreachable when the level is disabled:
hasGuard()looks for the guard among the ancestors of the log call: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
ifcounts as a guard only when it has noelsebranch, its condition is a negationcontaining 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 anegative control where the
ifbody does not exit and the call therefore stays unguarded: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=GuardLogStatementTestontemurin-25.
AI-assisted (LLM used for drafting); the change and both runs above are mine.