pmd / pmd

An extensible multilanguage static code analyzer.
https://pmd.github.io
Other
4.81k stars 1.49k forks source link

[java] False-positive UnusedAssignment with Lombok @NoArgsConstructor #3771

Closed paveljakov closed 1 year ago

paveljakov commented 2 years ago

Affects PMD Version: 6.42.0

Rule: UnusedAssignment

Please provide the rule name and a link to the rule documentation: https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#unusedassignment

Description:

False-positive unused assignment while using Lombok @NoArgsConstructor annotation. Lombok generates no-args constructor, so assignment is valid. Such code is usually encountered with JPA entities, where entity can have Lombok generated no-args and explicit copy-constructor constructors.

Code Sample demonstrating the issue:

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class TestClass {

  private Long version = 0L;

  public TestClass(final TestClass anesthesiaRequestVersion) {
    this.version = anesthesiaRequestVersion.version + 1;
  }

}

Expected outcome: No PMD violation detected.

PMD reports a violation at line private Long version = 0L;, but that's wrong. That's a false positive.

Running PMD through: Gradle

kna commented 1 year ago

This seems a similar problem to https://github.com/pmd/pmd/issues/2817. I faced the same issue and I run pmd after delombok.

adangel commented 1 year ago

Duplicate of #2871