Skip to content

[java] InvalidLogMessageFormat detection failing when some method uses a placeholder-looking String parameter  #3250

@kris-scheibe

Description

@kris-scheibe

Affects PMD Version: 6.34, also 6.35.0-SNAPSHOT

Description:
InvalidLogMessageFormat detects other methods in the call-chain if they use something that looks like a placeholder (e.g. "%s") as a String parameter.
In this test-case the parameter to "someOtherMethod" isn't used as a parameter to the log-call, even if it looks like it contains placeholders, so it should be ignored

Code Sample demonstrating the issue:

    <test-code>
        <description>[java] InvalidLogMessageFormat detection failing when another %-parameter is used</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.String.format;

class TestInvalidLogMessageFormat {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestInvalidLogMessageFormat.class);
    public void testPMD() {
        LOGGER.info(SomeOtherClass.someOtherMethod("Here we have a few %s!"));
        LOGGER.info(format("Skipping file %s", getName()));
    }

    private String getName() { return "the-name"; }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>[java] InvalidLogMessageFormat detection failing when another %-parameter is used in a variable</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.String.format;

class TestInvalidLogMessageFormat {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestInvalidLogMessageFormat.class);
    public void testPMD() {
        String message = SomeOtherClass.someOtherMethod("Here we have a few %s!");
        LOGGER.info(message);
    }

    private String getName() { return "the-name"; }
}
        ]]></code>
    </test-code>
Test Failure: [java] InvalidLogMessageFormat detection failing when another %-parameter is used
 -> Expected 0 problem(s), 1 problem(s) found.
 -> Expected messages: []
 -> Expected line numbers: []

n/a:8:	InvalidLogMessageFormat:	Missing arguments, expected 1 argument but have 0

--------------------------------------------------------------

Test Failure: [java] InvalidLogMessageFormat detection failing when another %-parameter is used in a variable
 -> Expected 0 problem(s), 1 problem(s) found.
 -> Expected messages: []
 -> Expected line numbers: []

n/a:9:	InvalidLogMessageFormat:	Missing arguments, expected 1 argument but have 0

Running PMD through: Other

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