False positive on java:S2259 with record

  • rule: java:S2259
  • Context: java 25 with jspecify
  • View with
    • SonarQube Community Build - v25.12.0.117093
    • SonarQube for IDE - 12.6.0.84973 (not connected)

When testing a condition on the nullability of a record attribute, we should have a warn as the record fields are immutable and set during the object instantiation:

package tech.lightframe.shift.core.cbs;

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.math.BigDecimal;

@NullMarked
public class Example {
    public record MyClass(
            @Nullable BigDecimal amount
    ) {
    }

    public BigDecimal checkValidity(MyClass obj) {
        if (obj.amount() != null) {
            return obj.amount().multiply(BigDecimal.ONE); // <= create a warn because consider obj.amount() could return null
            // But obj is immutable, obj.amount() always return the same value
        }
        return BigDecimal.TEN;
    }
}

It’s only an issue with sonar (create an issue in Sonar Qube), the compiler with `com.uber.nullaway` don’t have any issue with this syntax.

The same for pattern matched values, e.g.:

private boolean shouldAddPostfix(ServerWebExchange exchange) {
return exchange.getAttribute(REQUEST_ENRICHED_ATTRIBUTE) instanceof Boolean enriched && enriched;
}

enriched variable highlighted as rule java:S2259

Hello @kerny ,

Sorry for the delayed response. SonarQube Community Build uses a legacy implementation of java:S2259.

We have reworked this rule as javabugs:S2259, and it should no longer raise an issue for the case shown above.

This rule is available in the commercial editions of SonarQube and in SonarQube Cloud, including its free tier.

Would you be able to try it on SonarQube Cloud and confirm whether the false positive disappears?