Skip to content

[java] ConfusingArgumentToVarargsMethod FP when types are unresolved #5070

@SaschaRiemer

Description

@SaschaRiemer

Affects PMD Version:
7.2.0

Rule:
ConfusingArgumentToVarargsMethod
https://docs.pmd-code.org/latest/pmd_rules_java_errorprone.html#confusingargumenttovarargsmethod

Description:
Under certain circumstances (could not find a clear pattern yet, might be caused by the varargs parameter being a field in a different class), the ConfusingArgumentToVarargsMethod rule triggers even though it should not. This can happen for different varargs methods, when called with a single parameter of the same type, such as:
MessageFormat,format(PATTERN, ARG)
List.of(SomeEnum.CONSTANT)

Code Sample demonstrating the issue:
Two classes, in separate files
Messages.java:

public class Messages {
    public static String PATTERN = "Hello {0}";
    public static String SUBJECT = "World";
}

PmdExample.java

public class PmdExample {
    public void run() {
        MessageFormat.format(Messages.PATTERN, Messages.SUBJECT);
    }
}

Expected outcome:
PMD reports a violation at line PmdExample.java:3, but that's wrong. That's a false positive.

Running PMD through: CLI
I was unable to reproduce this example with gradle.
In a more complex codebase, this also happens with gradle, but I could not get that working with a simple example.

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