-
-
Notifications
You must be signed in to change notification settings - Fork 773
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: fix false positive for unresolved imports #4882
Conversation
Codecov Report
@@ Coverage Diff @@
## main #4882 +/- ##
============================================
+ Coverage 84.81% 84.82% +0.01%
Complexity 3463 3463
============================================
Files 493 493
Lines 11357 11365 +8
Branches 2090 2093 +3
============================================
+ Hits 9632 9640 +8
Misses 675 675
Partials 1050 1050
Continue to review full report at Codecov.
|
Fix a false positive where UnusedImports would report extension function imports whose signature cannot be resolved by the BindingContext. This can happen for autogenerated classes, etc. (especially Android UI binding classes and the like). To do this we need to generate text matchers for all the references which cannot be resolved and check if there are any unresolved matches to them, similar to the approach without type resolution. Along the way, extract mappings inside `KtImportDirective.isNotUsed()` to lazy properties to avoid recalculating them for each import directive. Also make sure to map to a set for constant rather than linear lookup time.
Code looks good, but I have to mention that I'm not a super big fan of this 🤔 I believe it might introduce more false positives than how much benefit it provides. |
To be clear, I think this could introduce false negatives but not false positives that I can think of (meaning this change would make it possible for to detekt to miss issues but wouldn't make detekt report non-issues - that could be a positive/negative depending on your POV). But this issue has prevented my team from using this rule with type resolution since currently it marks hundreds of such imports as unused because detekt can't resolve them - we can still use I'm not sure how else to approach this issue; if we have an import Regardless, I do want to make sure the changes to extract the sets to |
I'm wondering how ktlint does this. I have this rule disabled from detekt because I use ktlint for this issue and I never had any false positive or false negative. |
I assume that ktlint does string usage matching, the same as detekt without type resolution (or this change for when type resolution is enabled but can't resolve an import). The source seems to suggest the same - reference identifier text minus backticks is added to |
We should decide what to do with this. My point of view: this is a workaround for other bug: We don't get a complete So, to me, we should merge this, a false-positive is way worst than a false-negative. And we should also focus on fixing that long stading bug to avoid this type of workarounds. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this issue will be resolved once we are using K2 compiler plugin.
* All [namedReferences] whose [KtReferenceExpression.fqNameOrNull] cannot be resolved | ||
* mapped to their text. String matches to such references shouldn't be marked as unused | ||
* imports since they could match the unknown value being imported. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 documentation.
Fix a false positive where UnusedImports would report extension function imports whose signature cannot be resolved by the BindingContext. This can happen for autogenerated classes, etc. (especially Android UI binding classes and the like).
To do this we need to generate text matchers for all the references which cannot be resolved and check if there are any unresolved matches to them, similar to the approach without type resolution.
Along the way, extract mappings inside
KtImportDirective.isNotUsed()
to lazy properties to avoid recalculating them for each import directive. Also make sure to map to a set for constant rather than linear lookup time.