Kotlin S1128 - Unnecessary imports - False Positive

Hi everyone,

since last week we have a lot of additional code smells about unnecessary imports. But actually all of them are false positive. We already use detekt & ktlint locally and on our CI. They find none of the issues. Also IntelliJ IDEA “optimize imports” doesn’t remove anything.

So it looks like the unnecessary imports rules is currently broken. One thing I noticed is that constants used from companion objects are mostly causing the issues. E.g. import com.example.MyClass is reported even though MyClass.MY_CONSTANT is used inside the class. Also we see this issue when using methods from companion objects.

1 Like

Hi Thomas!

Thanks for the report, I’ve been able to reproduce the issue and created a ticket. You are right, something is not quite working as expected here.

We will ship a fix for the issue with the next release. As a workaround, you can disable the rule until then, if the false positives are too noisy.

You wrote that usages of constants from companion objects are mostly causing the issues. Have you noticed any other situations where FPs are triggered? Am I correct in assuming that the source code for which you have noticed this behavior is not publicly accessible?

1 Like

Thanks for you fast response.

I haven’t found time to check all of the issues. I randomly checked 10+ issues and all of the reported imports were classes with fields or methods from their companion objects being accessed.

Looks like this is the main cause. I will post an update if I find other cases.

Edit: And I am sorry, the repo is private. :frowning:

1 Like