Sonarqube doesn't report unused fields with lombok

Must-share information (formatted with Markdown):

  • Sonarqube Enterprise Edition Version 9.9.1 (build 69595)
  • Gradle Sonar Plugin 4.4.1.3373
  • Java 17
  • how is SonarQube deployed: Self Deployed
  • We face an issue that Sonarqube doesn’t report unused fields issue(rule java:S1068) for classes where Lombok used

I have 2 classes:
First class

public class ClassWithUnusedField {

    private final String result;

    public ClassWithUnusedField(String result) {
        this.result = result;
    }

    public String calculate() {
        return "success" + LocalDate.now();
    }

}

Second class:

@AllArgsConstructor
public class LombokClassWithUnusedField {

    private final String result;

    public String calculate() {
        return "success" + LocalDate.now();
    }

}

In result the issue will be reported only for the first class. However that’s obvious that it doesn’t used in both cases.
The rule java:S1068 in SonarQube says about exceptions:

The rule admits 3 exceptions:

  • Serialization id fields
  • Annotated fields
  • Fields from classes with native methods

However, I don’t have any of the mentioned cases above in my code. Please help how to fix that.
Reproducer project:
unused-field-issue-reproducer.zip (3.4 KB)

1 Like

Hello @posuhov,

Thank you for reaching out about this issue. The specification doesn’t mention that another exception is for classes annotated with Lombok annotations: see LombokFilter.

We don’t currently support Lombok annotations because they can cause undesired noise due to false positives; the drawback is allowing false negatives such as this case.

I will update the specification to mention that classes annotated with Lombok annotations are also an exception to this rule.

Cheers,
Angelo