Please provide
- Operating system: macOS Ventura 13.3.1
- SonarLint plugin version: 8.1.0.65508
- Programming language you’re coding in: Java
- Is connected mode used: Not connected
And a thorough description of the problem / question:
Consider the following code:
class Config {
record Values(
long heartbeatIntervalSeconds,
long timeoutMillis
) { }
private volatile Values values; // <- Generates java:S3077 bug
void update(
long heartbeatIntervalSeconds,
long timeoutMillis
)
{
this.values = new Values(
heartbeatIntervalSeconds
timeoutMillis
);
}
}
Even though Values
is an immutable record with all primitive/immutable values, SonarLint is raising a java:S3077 bug report that the usage is not thread safe. That is a spurious conclusion in this use case. Safe publication guarantees apply to object references. The issue arises only when object references refer to other objects that are mutable. That is not the case here. The rule needs to do a deeper analysis to determine if there is truly invalid use of volatile.