False positive for S1854 "Unused assignments should be removed"

  • Operating system: Linux
  • IDE name and flavor/env:
    Using sonarlint as Java library via gradle:
    {module = “org.sonarsource.sonarlint.core:sonarlint-core”, version = “9.8.0.76914” }
    { module = “org.sonarsource.java:sonar-java-plugin”, version = “7.31.0.34839” }

Getting a false positive for the following code:

package com;

public class BCid51 {
  void HJid232(EJid229 YHid199, Yid24.RIid217<MBid37> QJid241) {
    for (XFid148 RJid242 : YHid199.WIid222()) {
      int DHid178;
      if (Fid5.BGid151() == CGid152.DGid153) {
        MBid37 IHid183 = RJid242.OFid139();
        IHid183.AGid150();
      }
    }
  }
}

Message is for line 8:

Remove this useless assignment to local variable IHid183

That’s obviously wrong as the object is used in the next line.

1 Like

Hi David,

My assumption is: We have 2 problems:

  1. The project does not have a valid classpath and some of the types are not resolved properly.
  2. The rule S1854 raises false positives when some types are not resolved. We should remove this noise, I created this ticket SONARJAVA-4960.

Probably, by fixing the classpath used by the analysis, you should be able to remove those false positives.
And if it’s not related to a classpath problem, you should be able to provide a small reproducer using only known types.

Thanks for your feedback,
Alban

Hi Alban,

I have the same false positive issue with rule S1854. I am writing java code on Windows 11 in IntelliJ. I use IntelliJ version 2024.1(.4) with SonarLint plugin version 10.9.0(.79423)

I don’t think there are problems with the classpath (when I ctrl + click on the type of the parameter instance from the reproducible example below, IntelliJ takes me to the correct definition of the class). A smaller reproducible example can be found below:
Random file:

public String doSomething(AClass.BClass<String> instance) {
    String c = "Hi"; // Rule S1854
    return instance.b + c;
}

File AClass.java:

public class AClass {
    public class BClass<T> {
        public T b;
    }
}

Please let me know if you would prefer if I create a new topic for this.

Edit: A couple of days later I reopen the same file and see that the sonarlint warnings have disappeared.

Best regards,
Simon

Same situation with an enhanced switch setting a local variable:

// all the switch statement is signaled as "useless assignment"
final DocumentStatus status = switch(event) {
  case SimpleStatusChangedEvent ignored → DocumentStatus.DOC01;
  case NeedClientRecheckEvent ignored -> DocumentStatus.DOC02;
};
document.setStatus(status);
// ...
documentRepository.save(document);

If it can be of any help, for me those are the specs:

IntelliJ IDEA 2025.3
Build #IU-253.28294.334, built on December 5, 2025
Source revision: f50d587f27abb
Runtime version: 21.0.8+9-b1163.69 amd64 (JCEF 137.0.17)
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Toolkit: sun.awt.windows.WToolkit
Windows 11.0
GC: ZGC Minor Cycles, ZGC Minor Pauses, ZGC Major Cycles, ZGC Major Pauses
Memory: 2048M
Cores: 12
Registry:
vcs.log.index.enable=true
ide.experimental.ui=true
llm.jcp.analytics.endpoint.url=https://api.jetbrains.cloud/analytics/events/push
Non-Bundled Plugins:
com.intellij.notebooks.core (253.28294.337)
com.intellij.debugger.collections.visualizer (253.28294.336)
Subversion (253.28294.351)
org.editorconfig.editorconfigjetbrains (253.28294.335)
com.intellij.javaee (253.28294.336)
fr.pralexio.perplexityintegration (1.5)
com.intellij.spring (253.28294.351)
com.haulmont.jpab (253.28294.251)
intellij.jupyter (253.28294.337)
com.intellij.ml.llm (253.28294.337)
org.mapstruct.intellij (1.9.1)
com.intellij.mcpServer (253.28294.336)
org.sonarlint.idea (11.6.0.83783)
org.jetbrains.security.package-checker (253.28294.335)
Kotlin: 253.28294.334-IJ

Hi Marco,

Thanks for sharing this example and the detailed environment specifications.

I tried to reproduce the issue using the enhanced switch snippet you provided, but I was unable to trigger the S1854 false positive on my side.

To help us investigate this further, could you please clarify a few points:

  • Type Resolution: Are the types used in your switch (e.g., DocumentStatus, SimpleStatusChangedEvent) fully resolved in your IDE?

  • Java Version: Could you confirm the specific Java language level configured for this project module?

  • Reproducer: If possible, could you provide a small sample project (e.g. a GitHub repository or a zip file) that demonstrates the issue? This would allow us to analyze the configuration and classpath context that triggers the false positive.

Thanks for your help!

Romain

Hi everyone,

We have addressed the cause of these false positives for rule S1854. The fix has been implemented and will be available in the next release.

Best regards,

Romain

1 Like