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 = “” }
    { module = “org.sonarsource.java:sonar-java-plugin”, version = “” }

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();

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.

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,