# False-positive java:S1128 unnecessary import

**URL:** <https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164>\
**Category:** SonarQube Cloud\
**Tags:** false-positive, sonarqube-ide, java\
**Created:** [September 30, 2020, 9:48am UTC](https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164 "2020-09-30T09:48:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim-comline](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tim-comline](https://community.sonarsource.com/u/tim-comline)\
**Post date:** [September 30, 2020, 9:48am UTC](https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164/1 "2020-09-30T09:48:09Z")

</div>

Template for a good bug report, formatted with [Markdown](https://commonmark.org/help/):

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

```auto
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:

```auto
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:

```auto
  @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

---

<div class="post-metadata">

**Author:** ![Michael](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/michael/32/43_2.png) [@Michael](https://community.sonarsource.com/u/Michael)\
**Post date:** [November 24, 2020, 4:35pm UTC](https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164/4 "2020-11-24T16:35:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mlkammer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mlkammer/32/19391_2.png) [@mlkammer](https://community.sonarsource.com/u/mlkammer)\
**Post date:** [February 16, 2021, 10:24am UTC](https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164/6 "2021-02-16T10:24:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mlkammer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mlkammer/32/19391_2.png) [@mlkammer](https://community.sonarsource.com/u/mlkammer)\
**Post date:** [March 3, 2021, 8:12pm UTC](https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164/8 "2021-03-03T20:12:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [April 21, 2021, 12:37pm UTC](https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164/10 "2021-04-21T12:37:31Z")

</div>

Hello @mlkammer

Coming back to you with good news about this topic: we identified a way to greatly improve the precision of this rule (see [SONARJAVA-3777](https://jira.sonarsource.com/browse/SONARJAVA-3777)). Fixing this ticket will require a big refactoring of the implementation, I don’t think it is worth it to dig deeper into reproducing your issue at this point. And anyway, I’m quite confident this will make your issue go away.

Thanks for taking the time to report this false positive.  
Best,  
Quentin

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [April 28, 2021, 12:37pm UTC](https://community.sonarsource.com/t/false-positive-java-s1128-unnecessary-import/32164/12 "2021-04-28T12:37:46Z")

</div>

Closed on April 28, 2021, 12:37pm UTC

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