diff --git a/scalafix-core/src/main/scala/scalafix/internal/patch/ImportPatchOps.scala b/scalafix-core/src/main/scala/scalafix/internal/patch/ImportPatchOps.scala index 55c8bd8b0b..28000106ce 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/patch/ImportPatchOps.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/patch/ImportPatchOps.scala @@ -4,6 +4,7 @@ import scala.annotation.tailrec import scala.collection.mutable import scala.meta._ +import scala.meta.trees.Origin import scalafix.XtensionOptionPatch import scalafix.XtensionSeqPatch @@ -200,7 +201,11 @@ object ImportPatchOps { allImports.filter(_.importers.forall(isRemovedImporter)) def remove(toRemove: Tree): Patch = { - if (toRemove.pos == Position.None) return Patch.empty + val isSameInput = (toRemove.origin, ctx.tree.origin) match { + case (a: Origin.Parsed, b: Origin.Parsed) => a.input == b.input + case _ => false + } + if (!isSameInput) return Patch.empty // Imagine "import a.b, c.d, e.f, g.h" where a.b, c.d and g.h are unused. // All unused imports are responible to delete their leading comma but // c.d is additionally responsible for deleting its trailling comma.