False Positive with java:S2259 on Enum constants

Hello,

We’re using SonarQube Server v2025.1 (102418) and seeing this false positive in java:S2259 rule.

To make it easy to reproduce, I’m copying the code below.

public enum Status {

    SUCCESS("green"),
    FAILED("red"),
    PENDING("yellow");

    private final String color;

    Status(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }
}
public class StatusMapper {

    public String getColor(@CheckForNull Status status) {
        if (Status.SUCCESS == status || status == null) {
            return Status.SUCCESS.getColor(); <<<<<<< FP here
        }
        return status.getColor();
    }
}

Please let me know what you think.

Thanks.

I have noticed the exact same behaviour in our code base.

From my tests it is the “status == null” part that is causing the problem. As status can be null it is transferring that nullability to the enum constant in this analysis. That is obviously wrong as the enum constant is constant, the value of a variable can have no effect.

Hi there thanks for the report.
I added this thread to an existing ticket for this known problem, here.

1 Like