False-positive java:S1128 unnecessary import

Template for a good bug report, formatted with Markdown:

  • versions used: SonarLint plugin (4.11.0.20867) with Intellij

(IntelliJ IDEA 2020.2.2 (Ultimate Edition)
Build #IU-202.7319.50, built on September 15, 2020
Runtime version: 11.0.8+10-b944.31 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 10.15.7)

  • error observed (wrap logs/code around triple quote ``` for proper formatting)
Remove this unused import 'com.cartrust.comregistraiton.organiozation.model.DocumentState.UPLOADED'
  • steps to reproduce

It seem related to the enum value “UPLOADED” if I refactor the value to “UPLOADED_YEAH” the error disappears. Of course it was used after the refactoring too.

Other enum values from the same enum are not shown as errors:

import static com.cartrust.registration.organization.model.DocumentState.MISSING;
import static com.cartrust.registration.organization.model.DocumentState.UPLOADED;
import static com.cartrust.registration.organization.model.DocumentState.WAITING_FOR_NOTIFICATION;
import static com.cartrust.registration.organization.model.DocumentState.WAITING_FOR_UPLOAD;

Here’s the usage in the same file:

  @EventHandler
  public void on(DocumentAddedEvent event) {
    val aggregateId = event.getOwnerOrganizationId();

    organizationViewRepository.findById(aggregateId).ifPresent(
        organizationView -> {
          organizationViewRepository.save(organizationView.toBuilder()
                .businessRegistrationDocumentId(event.getDocumentId())
                .businessRegistrationState(UPLOADED)
                .build());         
        }
    );
  }
  • potential workaround

Rename enum values “UPLOADED” to something else.

P.S.: use the #bug:fault sub-category if you’re hitting a specific crash/error , or the #bug:fp sub-category for rules-related behaviour

Hello @tim-comline,

Sorry for coming back to you so late after you posted this. Unfortunately, without a self-contained reproducer code, it will be hard for me to reproduce the issue on my side, as I can only assumes on what are the methods used in the code snippet. Can you simplify your code snippet and provide a code which which explicitly reproduce the issue without having to figure some methods?

Also, I guess that you are using lombok (val?), and we have always been facing issue handling it correctly in our analysis. It might be a track to explore.

Thanks,
Michael

We have a similar problem: false positives on statically imported constants. The following class is simplified and raises an issue on the statically imported OK constant:

package test;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;

class MyHttpEndpoint {

    Object handleRequest(Object request) {
        // ... some request handling code that doesn't matter now

        return respond(OK);
    }

    private Object respond(Object status) {
        return status;
    }
}

Warning message:

Remove this unused import ‘io.netty.handler.codec.http.HttpResponseStatus.OK’.

This is on SonarCloud, so latest released version.

Is this enough to be able to reproduce / fix the problem?
Thank you in advance.

Hi @Michael, I just realized I didn’t tag you in the comment above. Could you take a look at it to see if it’s enough to reproduce the problem?