Skip to content

[java] UnusedAssignment false positive with call chains #2796

@oowekyala

Description

@oowekyala

Affects PMD Version: 6.26.0

Description:

Call chains where side-effects occur in arguments are not handled by UnusedAssignment. This is a known issue, described here

// For the record this has problems with call chains with side effects, like
// a.foo(a = 2).bar(a = 3);

It's hard to make this right on master, because of the grammar for expressions. But this is easily fixed in #2794

Code Sample demonstrating the issue:

class Test {
    int a() {
        int a = 10;
        try {
            // both method calls may throw, so a = 2 and a = 4 reach after the try,
            // but not a = 10
            "".substring(a = 2)
              .substring(a = 4);
        } catch (RuntimeException e) {
        }
        return a;
    }
}

We get

line 3:	The initializer for variable 'a' is never used (overwritten on line 7)
line 7:	The value assigned to variable 'a' is never used (overwritten on line 8)

but only the first warning is relevant, the other one is a FP

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions