Unresolved variable in anonymous class (S1854: Remove this useless assignment to local variable "!unknown!")

Hi,

for some anonymous classes the rule S1854 produces false positives. It appears as if name, type and usages weren’t resolved. Instances of this FP only occurr on local variables in overriden methods of anonymous classes. For example:

Runnable r = new Runnable() {
  
  @Override
  public void run() {
    SomeImportedType variable = ...; // variable is !unknown!
  }
}

I do not know how to investigate this issue. Converting the anonymous class to a nested class resolves the issue, classpath seems to be complete.
I have found an issue related to mine (unknown local variable); it concerned SonarLint for IntelliJ and is already fixed, however. We are using SonarLint for Eclipse, which is working fine.

We use SonarQube 8.9 LTS. The project is build with maven + tycho, we currently use sonar-maven-plugin:3.7.0.1746

Hello @oliver

We are using SonarLint for Eclipse, which is working fine.

I’m not sure to understand: where are you seeing this issue? In SonarLint for Eclipse? Are you using connected mode?
Or is it directly inside SonarQube (8.9)?

Hi @Quentin

this issue appears only in SonarQube. We are using SonarLint in connected mode, there everything is fine.

Thanks for the clarification.

However, I played a bit with your example, but I can not reproduce the issue.

Is variable actually used? In others words: is it a false positive or simply a strange-looking message? Ideally, could you provide a complete reproducer?

Hi Quentin,

currently, all instances of this phenomenon are in anonymous classes in method bodies. For example

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

public class SomeClass {

  private Collection<Object> objects;

  public void someMethod() {
    IStatusRunnable runnable = new IStatusRunnable() {

      @Override
      public IStatus run() {
        IStatus status = Status.OK_STATUS;

       for (Object obj : objects) {
         if (obj instanceof String) {
           status = Status.CANCEL_STATUS;
         }

        return status;
      }
    ...
  }
}

In all instances the variable is used and since SonarLint (in connected mode) is not reporting an issue here it seems to be related only to SonarQube or our scanner configuration. However, I have no idea how to investigate.

Finally coming back at this, we are currently putting a lot of effort to avoid reporting issues when things are unresolved.

The first observation is that I was still unable to reproduce the issue… Still, during the investigation, I manage to identify problems in the logic to determine what is unresolved or not (see SONARJAVA-4126).
It should improve things, but I’m unsure this will solve your specific problem.

This report is now quite old, a lot of changes happened since. I encourage anyone facing a similar issue to try the latest version of SonarQube/SonarCloud and to create a new topic with as many information as possible to help us identify the cause (versions, scanner used, …)

In addition, let me remind you that unresolved variables are not supposed to happen, and are a sign that something is going wrong during the analysis. Here are the typical reasons:

  • For SonarLint, you should probably build completely your project and try again.

  • For SonarQube/SonarCloud, you should have a look at the logs of the analysis for something like:

Unresolved imports/types have been detected during analysis.

It should help you to understand what dependencies/libraries are missing in the configuration.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.