-
Notifications
You must be signed in to change notification settings - Fork 186
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
Remove Importee.Rename if it does not shadow anything #923
Remove Importee.Rename if it does not shadow anything #923
Conversation
fix(doc.tree, unusedTermsPosition.toSet, unusedImportsPosition.toSet) | ||
} | ||
} | ||
|
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.
Algorithm:
- traverses the tree using depth-first traversal
- for each enclosing scope (source, package, object, class, def, block, ...)
- any used import wildcards AND unused import renames are tracked and passed down into nested scopes following them
- any other unused import gets removed
- at the end of each scope
- any unused import rename (from outer our current scope) whose package is the same as any wildcard import in scope (outer or current) gets unimported
- any remaining unused import rename belonging exclusively to the current scope (not the outer) gets removed
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.
👍 would be good to include this description in the comments
77d72f3
to
5ad04a2
Compare
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.
Thank you @marcelocenerine! Great job with the thorough test suite and evaluation 👍
I am a bit concerned about the increased complexity from this change. I am wondering if we can achieve the same goal while keeping a stateless tree traverser. I took a stab at this issue here master...olafurpg:fix_rename_unused_import2
In short, we add a new guard for the "unimport rename" case to only unimport renames when they're not part of an importer with a used wildcard.
val isUsedWildcard = importer.importees.exists {
case i: Importee.Wildcard => !isUnusedImport(importPosition(i))
case _ => false
}
importer.importees.collect {
case i @ Importee.Rename(_, to)
if isUsedWildcard &&
isUnusedImport(importPosition(i)) => // unimport rename
This change caused several imports to get removed from the test suite. Consider the following case
bash-3.2$ cat err.scala
import scala.util._
import scala.util.{Success => S}
object A {
Success(1)
}
bash-3.2$ scalac -Ywarn-unused err.scala
err.scala:2: warning: Unused import
import scala.util.{Success => S}
^
one warning found
I think we are save to remove the rename because Success
is anyways brought in through the wildcard import.
fix(doc.tree, unusedTermsPosition.toSet, unusedImportsPosition.toSet) | ||
} | ||
} | ||
|
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.
👍 would be good to include this description in the comments
Indeed it adds some complexity to handle the case where the rename and wildcard are in different |
I think the simple solution fixes also #915 In the diff here 38db60c#diff-5a5912bc5da72f0e5da63e0919c8d2d0 for example it removes |
5ad04a2
to
141bd62
Compare
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.
Can we add back the test suite you wrote in the original PR? It was very thorough and I think it can be helpful to catch regressions in the future. Otherwise, LGTM 👍
I'm sorry for the slow response!
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.
Can we add back the test suite you wrote in the original PR? It was very thorough and I think it can be helpful to catch regressions in the future. Otherwise, LGTM 👍
I'm sorry for the slow response!
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.
Can we add back the test suite you wrote in the original PR? It was very thorough and I think it can be helpful to catch regressions in the future. Otherwise, LGTM 👍
I'm sorry for the slow response!
Hey @olafurpg, many of those test cases were more relevant to the original algorithm. I added most of them back, except the ones related to scoping. The new implementation doesn't care about scopes |
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.
LGTM 👍 Thank you @marcelocenerine !
Thanks, @marcelocenerine! |
Fixes #915
/cc @hepin1989
UPDATE: I tested the fix on the akka repo after reverting the changes to scala files in this commit: akka/akka@e9fb3a0. These are the only diffs: