Null assignments are flagged for @NotNull annotations with a check group

I am using SonarLint for IntelliJ IDEA, version

Given the following class,

import javax.validation.constraints.NotNull;

public class Test {
    interface TestCheck {}

    @NotNull(groups = TestCheck.class)
    public String testString;

    public void setTestString() {
        testString = null;

SonarLint flags the line testString = null; with S2637, because the field has a @NotNull annotation.

I think this is a rather more clear-cut case than, e. g., this issue: False Positive on S2583 when a @NotNull field is allowed to be null

Given that the validation in question only applies to certain validation groups, it should be clear that the @NotNull annotation is not intended to mean that the field can never be null - only that it cannot be null in certain contexts. Therefore, SonarLint should not flag @NotNull annotations with the groups parameter set.

Hello @chaosflaws

I just stumbled into this old topic when looking into a ticket we recently created: SONARJAVA-3803.

It turns out it is closely related to the problem you reported, it should get rid of your false-positive aswell. I made sure to add your example to make sure we are correctly supporting it.


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