Skip to content

[java] GuardLogStatement: False positive when guard is not a direct parent #4282

@Frettman

Description

@Frettman

Affects PMD Version:
6.53.0+
7.0.0

Rule: GuardLogStatement

Description:

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:

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Foo {
    private static final Logger LOG = LogManager.getLogger(Foo.class);
    public void test(String arg1) {
        if (LOG.isWarnEnabled()) {
          if (someCondition) {
            String msg = "Message 1 with one argument: ";
            LOG.warn(msg + arg1);
          } else {
            String msg = "Message 2 with two arguments: {}, {}.";
            LOG.warn(msg + arg1);
          }
        }
    }
}

Expected outcome:

PMD should look further around it to detect a guard method.

Running PMD through: Maven | Eclipse Plug-in

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions