Skip to content

[java] InvalidLogMessageFormat rule : False positive for lambda parameter #2718

@julius-d

Description

@julius-d

Affects PMD Version:
PMD version: 6.26.0

Description:

In my code the PMD rule InvalidLogMessageFormat
found a bug, that is not a bug.

I was able to reproduce it with this code sample

Code Sample demonstrating the issue:

import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Foo {

  private static final Logger LOG = LoggerFactory.getLogger(Foo.class);

  public static class Event {

    private Throwable lastThrowable;

    public Throwable getLastThrowable() {
      return lastThrowable;
    }
  }

  public void bar(Event event) {
    Optional.of(event).ifPresent(e -> LOG.warn(
        "Failed on {}",
        1,
        e.getLastThrowable()));

  }
}

Steps to reproduce:

On my machine:

  1. mvn clean compile pmd:check
  2. See You have 1 PMD violation
  3. cat target/pmd.xml
  4. See
<file name="/xyz/Foo.java">
<violation beginline="21" endline="21" begincolumn="39" endcolumn="46" rule="InvalidLogMessageFormat" ruleset="Error Prone" package="xyz" class="Foo" method="bar" externalInfoUrl="https://pmd.github.io/pmd-6.26.0/pmd_rules_java_errorprone.html#invalidlogmessageformat" priority="5">
Too many arguments, expected 1 argument but have 2
</violation>
</file>

In this project:

  1. Add the snippet to src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/InvalidLogMessageFormat.xml
  2. Run mvn test
  3. See
[ERROR] Failures: 
[ERROR] net.sourceforge.pmd.lang.java.rule.errorprone.InvalidLogMessageFormatTest.null
...
[ERROR]   Run 45: InvalidLogMessageFormatTest>RuleTst.runTest:157 "My bug" resulted in wrong number of failures, expected:<0> but was:<1>

Running PMD through: Maven
maven-pmd-plugin:3.13.0

Apache Maven 3.6.3
Java version: 11.0.1, vendor: AdoptOpenJDK,
platform encoding: UTF-8
OS name: "mac os x"

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