Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnusedImports false positive for annotation used in another annotation attributes (with type resolution) #3246

Closed
vladimirfx opened this issue Nov 24, 2020 · 4 comments

Comments

@vladimirfx
Copy link

Expected Behavior

When an annotation used in another annotation attributes that annotation import should not marked as unused.

Steps to Reproduce

import somepkg.AnnotationA
import somepkg.AnnotationB

class SomeClass {
    @AnnotationB(attribute = AnnotationA)
    val someProp: Type
}

In such code import somepkg.AnnotationA is reported as unused.

Context

Your Environment

  • Version of detekt used: 1.15.0-RC1
  • Version of Gradle used (if applicable): 6.7.1
@cortinico
Copy link
Member

Have you enabled type resolution? This rule has a mixed behavior (see #2994) based on wether you have Type Resolution enabled or not: https://detekt.github.io/detekt/type-resolution.html

I'm adding a test with your snippet and it seems to work fine:
#3255

@vladimirfx
Copy link
Author

Yes I’ve use type resolution. Hm it seems I missing some important detail...
In your test case there is no attribute in AnnotationB. How it compiles?

@cortinico
Copy link
Member

Hm it seems I missing some important detail...
In your test case there is no attribute in AnnotationB. How it compiles?

Yup you're right. I've updated the test with a correct version. Unfortunately we don't have support for compiling snippets when there is more than one file involved.

To be fair, the snippet you posted initially was also invalid. You have to instantiate an annotation when passing it as parameter to another annotation: @AnnotationB(attribute = AnnotationA()).
Would you be able to provide a different snippet where we can reproduce?

@schalkms
Copy link
Member

Closing this, since tests have been added for the initial code snippet. If new false-positives for this rule appear, please feel free to open a new issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants