# Unnecessary imports should be removed

**URL:** <https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246>\
**Category:** SonarQube Server / Community Build\
**Tags:** sonarqube\
**Created:** [July 24, 2023, 2:58pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246 "2023-07-24T14:58:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cindy-rahbani](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/cindy-rahbani/32/45824_2.png) [@cindy-rahbani](https://community.sonarsource.com/u/cindy-rahbani)\
**Post date:** [July 24, 2023, 2:58pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/1 "2023-07-24T14:58:31Z")

</div>

Hello,

I’m facing an issue with the following rule not detecting vulnerable lines of code.

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;

No @ are added before the line.  
The quality profile used contains the rule in question.  
We are using sonarqube version 9.6-community.

Do you have any idea?

Thanks,  
Cindy

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [July 25, 2023, 1:18pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/2 "2023-07-25T13:18:52Z")

</div>

Hi Cindy,

Your version is past EOL. You should upgrade to either the latest version or the current LTS at your earliest convenience. Your upgrade path is:

> 9.6 → 9.9.1 → 10.1 (last step optional)

You may find these resources helpful:

- [Upgrade Guide](https://docs.sonarqube.org/latest/setup/upgrading/)

Regarding your question, it’s not clear what’s wrong with the line of code you provided. If you still have problems after your upgrade, please come back to us and make sure to provide detail on what’s missing, and what language you’re working in.

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![cindy-rahbani](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/cindy-rahbani/32/45824_2.png) [@cindy-rahbani](https://community.sonarsource.com/u/cindy-rahbani)\
**Post date:** [July 28, 2023, 1:35pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/3 "2023-07-28T13:35:16Z")

</div>

Hello Ann,

Actually what is wrong is that we have an enabled rule: “Unnecessary imports should be removed”  
And this import : import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;  
is not used, so the rule should have picked it up and a bug / code smell …

Do you have any idea why it ignored it?

Will be upgrading soon to the latest LTS version.

Thanks,  
Cindy

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [July 28, 2023, 1:55pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/4 "2023-07-28T13:55:38Z")

</div>

Hi Cindy,

Please let us know if this is still replicable after your upgrade.

&nbsp;  
Thx,  
Ann

---

<div class="post-metadata">

**Author:** ![cindy-rahbani](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/cindy-rahbani/32/45824_2.png) [@cindy-rahbani](https://community.sonarsource.com/u/cindy-rahbani)\
**Post date:** [July 31, 2023, 3:19pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/5 "2023-07-31T15:19:07Z")

</div>

Hello Ann,

The issue persists after upgrading to the latest LTS version.  
Can you please assist?

Thanks,  
Cindy

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [July 31, 2023, 4:54pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/6 "2023-07-31T16:54:00Z")

</div>

Hi Cindy,

Thanks for the followup.

To clarify, we’re talking about Java?

And could you provide a screenshot of the context for this false positive, or a compact reproducer file?

&nbsp;  
Thx,  
Ann

---

<div class="post-metadata">

**Author:** ![cindy-rahbani](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/cindy-rahbani/32/45824_2.png) [@cindy-rahbani](https://community.sonarsource.com/u/cindy-rahbani)\
**Post date:** [August 1, 2023, 8:49am UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/7 "2023-08-01T08:49:45Z")

</div>

Hi again,

Yes we are talking about java.  
The rule “Unnecessary imports should be removed” the same project is working for java classes but not for java classes in spring services modules.

Here is an example where the same import is detected as duplicated but not detected as unused.

 ![MicrosoftTeams-image (16)](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/e/6e50e13b5462d872d7585c602377c045f735e804.png)

Thanks,  
Cindy

---

<div class="post-metadata">

**Author:** ![cindy-rahbani](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/cindy-rahbani/32/45824_2.png) [@cindy-rahbani](https://community.sonarsource.com/u/cindy-rahbani)\
**Post date:** [August 1, 2023, 8:51am UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/8 "2023-08-01T08:51:53Z")

</div>

import java.io.ObjectOutput should have been detected as unused.  
But it’s not.

Any idea?

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [August 1, 2023, 1:30pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/10 "2023-08-01T13:30:47Z")

</div>

Hi,

Thanks for the clarifications. I’ve flagged this for the language experts.

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [August 3, 2023, 3:03pm UTC](https://community.sonarsource.com/t/unnecessary-imports-should-be-removed/96246/12 "2023-08-03T15:03:58Z")

</div>

Hello @cindy-rahbani,

Thanks for your message. I think there are a few different issues here: Duplicated import is reported, but unused is not, unused import isn’t reported. So let’s not mix them and try to solve them one by one.

Firstly, I will need the minimal reproducer to be able to reproduce the issue. Could you, please, provide me with a small sample project, where it happens, so I can investigate it further?

Regards,  
Margarita
