All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalafix.internal.rule.RemoveUnusedImports.scala Maven / Gradle / Ivy

package scalafix.internal.rule

import scala.meta._
import scalafix.Patch
import scalafix.SemanticdbIndex
import scalafix.rule.RuleCtx
import scalafix.rule.SemanticRule

case class RemoveUnusedImports(index: SemanticdbIndex)
    extends SemanticRule(
      index,
      "RemoveUnusedImports"
    ) {

  override def description: String =
    "Rewrite that removes unused imports reported by the compiler under -Xwarn-unused-import."

  private val unusedImports = index.messages.toIterator.collect {
    case Message(pos, _, "Unused import") =>
      pos
  }.toSet
  private def isUnused(importee: Importee) = {
    val pos = importee match {
      case Importee.Rename(name, _) => name.pos
      case _ => importee.pos
    }
    unusedImports.contains(pos)
  }
  override def fix(ctx: RuleCtx): Patch =
    ctx.tree.collect {
      case i: Importee if isUnused(i) => ctx.removeImportee(i).atomic
    }.asPatch
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy