| CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Affects PMD Version: 7.14.0 (in fact since 7.5.0)
Rule: https://docs.pmd-code.org/pmd-doc-7.14.0/pmd_rules_java_bestpractices.html#guardlogstatement
Description:
After fixing the issue #3283 in PMD 6, PMD is able to recognize that a string is a compile-time constant expression and thus does not need to be guarded in log method calls.
While this detection also worked with PMD 6.45.0 until PMD 7.4.0 independent of the position of the compile-time constant expression in the method call, PMD 7.14.0 reports a violation (false positive) if the compile-time constant expression is not the first parameter, e.g. if a marker is the first parameter, or if the compile-time constant expression follows later on (more realistic use case than line 13 below: a specialized logger implementation is used that expects the message being built of long compile-time constant strings with line wrapping as third parameter).
Code Sample demonstrating the issue:
import org.slf4j.*;
public class MyLogger {
private static final Logger LOGGER = LoggerFactory.getLogger(MyLogger.class);
public static void main(String[] args) {
LOGGER.debug("Part1" + " Part2"); // ok
final Marker marker = MarkerFactory.getMarker("myMarker");
LOGGER.debug(marker, "Part3 " + "Part4"); // false-positive GuardLogStatement
LOGGER.debug(marker, "Message={}", "Part3 " +
"Part4"); // false-positive GuardLogStatement
}
}Expected outcome:
PMD reports a violation at line 11 as well as line 13, but that's wrong. That's a false positive.
Running PMD through: Gradle