-
Notifications
You must be signed in to change notification settings - Fork 185
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
improve RemoveUnused rule #1114
Conversation
importees can be remove if `forall(_.is[Importee.Unimport])`
@@ -67,6 +67,9 @@ class RemoveUnused(config: RemoveUnusedConfig) | |||
Patch.empty | |||
} else { | |||
doc.tree.collect { | |||
case Importer(_, importees) | |||
if importees.forall(_.is[Importee.Unimport]) => | |||
importees.map(Patch.removeImportee).asPatch |
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 wonder is it safe to remove any unimports without an accompanying _
? E.g.:
import p.{a, b => _}
import p.{c => c1, d => _}
I think it's also safe to remove b => _
and d => _
above?
The Scala language spec is a little bit vague about the semantics of unimports (see here).
If the target in an import selector is a wildcard, the import selector hides access to the source member. For instance, the import selector 𝑥 => _ “renames” 𝑥 to the wildcard symbol (which is unaccessible as a name in user programs), and thereby effectively prevents unqualified access to 𝑥. This is useful if there is a final wildcard in the same import selector list, which imports all members not mentioned in previous import selectors.
The last sentence says "this is useful ...", not "this is only useful ...". It makes me wonder whether a "standalone" unimport without an accompanying _
is meaningful at all.
A quick test shows that they seem to be meaningless:
package p {
object a
}
object X {
import p.a
import p.{a => _}
val x = a
}
object Y {
import p._
import p.{a => _}
val x = a
}
The above snippet compiles successfully, which is quite expected. Otherwise, it means that unimports are order-sensitive.
I'm asking this partly because I'm trying to answer this question for handling unimports in the OrganizeImports
rule.
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.
That looks like another scenario where the rule could be be more aggressive indeed. @xuwei-k any feedback on this? We could merge this as-is and handle your scenario in another PR @liancheng.
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, thanks!
@@ -67,6 +67,9 @@ class RemoveUnused(config: RemoveUnusedConfig) | |||
Patch.empty | |||
} else { | |||
doc.tree.collect { | |||
case Importer(_, importees) | |||
if importees.forall(_.is[Importee.Unimport]) => | |||
importees.map(Patch.removeImportee).asPatch |
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.
That looks like another scenario where the rule could be be more aggressive indeed. @xuwei-k any feedback on this? We could merge this as-is and handle your scenario in another PR @liancheng.
I think
importees
can be remove ifforall(_.is[Importee.Unimport])