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

improve RemoveUnused rule #1114

Merged
merged 1 commit into from
Jun 16, 2020

Conversation

xuwei-k
Copy link
Contributor

@xuwei-k xuwei-k commented May 6, 2020

I think importees can be remove if forall(_.is[Importee.Unimport])

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
Copy link
Contributor

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.

Copy link
Collaborator

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.

Copy link
Collaborator

@bjaglin bjaglin left a 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
Copy link
Collaborator

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.

@bjaglin bjaglin merged commit 4da3d7f into scalacenter:master Jun 16, 2020
@xuwei-k xuwei-k deleted the importee-forall-unimport branch June 24, 2020 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants