S2637 lombok.NonNull and lombok.Setter annotations false positive use case

Hi,

A class field is reported to not be initialized while it is.

Versions

  • Eclipse IDE
    Version: 2020-12 (4.18.0)
    Build id: 20201210-1552
  • SonarLint
    5.6.0.25634
  • Lombok
    1.18.18

Sample Code

import lombok.NonNull;
import lombok.Setter;

public class S2637FalsePositiveSampleCode {
    @Setter
    @NonNull
    private String foo;

    public S2637FalsePositiveSampleCode(String foo) {
        setFoo(foo);
    }
}

Lombok generates the method setFoo. Since the field foo is annotated with @NonNull, lombok adds a null check in the setFoo method.

However, an info is raised :

“foo” is marked “lombok.NonNull” but is not initialized in this constructor.

Hello @Stephan_Beuze, thanks for reporting this false positive.

I agree that there is a problem here. While investigating the code, I realized that even the vanilla Java code (similar to what Lombok will generate) is also subject to this false positive.

I therefore created a ticket to track this issue: SONARJAVA-3695.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.