Skip to content

[java] UnusedPrivateMethod false positives with lombok.val #5369

@Pankraz76

Description

@Pankraz76

Affects PMD Version: 7.10.0

Rule: UnusedPrivateMethod

Description:
When using Lombok val, the type of the local variable is lombok.val instead of the inferred correct type.
When such a variable is used in a method call, we don't find the correct method declaration due to the different types and report the called method as unused.

Workaround: Don't use val, use final var instead. Note: lombok val is a final local variable, while Java's var, is a non-final local variable.

Code Sample demonstrating the issue:

package sample;

import lombok.val;

class Foo {
  public void method() {
     val test = "a";

     privateMethod(test);
  }

  private void privateMethod(String a) {
    System.out.println(a);
  }
}

Complete reproducer project: issue-5369-unusedprivatemethod.zip

Expected outcome:

PMD reports a violation at line 12, but that's wrong. That's a false positive.

Running PMD through: Maven

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematicin:type-resolutionAffects the type resolution code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions