You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] GuardLogStatement: False positive when guard is not a direct parent #4282
I have an additional if between the guard method and the actual call to a log method (see example below). In the else I'm directly calling a (simple getter) method to provide an argument to the log method, which triggers the need for a guard. This raises a violation: Logger calls should be surrounded by log level guards.
The rule does not recognize that there's a guard around it, just not as direct parent in the syntax tree.
Code Sample demonstrating the issue:
importorg.apache.logging.log4j.Logger;
importorg.apache.logging.log4j.LogManager;
publicclassFoo {
privatestaticfinalLoggerLOG = LogManager.getLogger(Foo.class);
publicvoidtest(Stringarg1) {
if (LOG.isWarnEnabled()) {
if (someCondition) {
Stringmsg = "Message 1 with one argument: ";
LOG.warn(msg + arg1);
} else {
Stringmsg = "Message 2 with two arguments: {}, {}.";
LOG.warn(msg + arg1);
}
}
}
}
Expected outcome:
PMD should look further around it to detect a guard method.
changed the title [-][java] GuardLogStatement only detects guard methods immediately around it.[/-][+][java] GuardLogStatement only detects guard methods immediately around it[/+]on Jan 19, 2023
changed the title [-][java] GuardLogStatement only detects guard methods immediately around it[/-][+][java] GuardLogStatement: False positive when guard is not a direct parent[/+]on Dec 21, 2025
Activity
[-][java] GuardLogStatement only detects guard methods immediately around it.[/-][+][java] GuardLogStatement only detects guard methods immediately around it[/+]SheikhJebran-Philips commentedon Jul 9, 2024
facing the same issue
krallus commentedon Oct 3, 2024
Interestingly, this issue does not occur for me in PMD 7.2, 7.3, or 7.4. However, it is a problem in PMD versions 7.5 and 7.6.
[-][java] GuardLogStatement only detects guard methods immediately around it[/-][+][java] GuardLogStatement: False positive when guard is not a direct parent[/+][doc] Update release notes (#4282, #6296)
[java] Fix pmd#4282: GuardLogStatement only detects guard methods imm…
RM-37163: CPD 7.21.0 upgrade (#8)