"Non-primitive fields should not be 'volatile'" spurious bug

Please provide

  • Operating system: macOS Ventura 13.3.1
  • SonarLint plugin version:
  • 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(

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.

Hi CSSPRS, thanks for reporting!

You are right, declaring a field with a reference type volatile is not a problem in case the type is a record with all-primitive fields. Also, it is not a problem for immutable types in general.

A current limitation of our analyzer is that it considers primitive types only, as well as enums and a predefined set of some immutable API types. There is no deeper, generic analysis for immutable reference types implemented yet.

We already have a ticket for this issue.

Best regards,