diff --git a/.scalafix-scala2.conf b/.scalafix-scala2.conf index f9ca221c2..bfc871b2b 100644 --- a/.scalafix-scala2.conf +++ b/.scalafix-scala2.conf @@ -1,11 +1,13 @@ rules = [ ExplicitResultTypes, - OrganizeImports + OrganizeImports, + RemoveUnused ] ExplicitResultTypes { unsafeShortenNames = true } + OrganizeImports { groupedImports = Explode expandRelative = true @@ -17,3 +19,7 @@ OrganizeImports { "*" ] } + +RemoveUnused { + imports = false +} \ No newline at end of file diff --git a/.scalafix-scala3.conf b/.scalafix-scala3.conf index cfbb6de23..8114d5a78 100644 --- a/.scalafix-scala3.conf +++ b/.scalafix-scala3.conf @@ -1,11 +1,12 @@ rules = [ - OrganizeImports + OrganizeImports, + RemoveUnused ] OrganizeImports { groupedImports = Explode expandRelative = true - removeUnused = false + removeUnused = true groups = [ "re:javax?\\." "scala." @@ -13,3 +14,7 @@ OrganizeImports { "*" ] } + +RemoveUnused { + imports = false +} \ No newline at end of file diff --git a/scalafix-cli/src/main/scala/scalafix/internal/jgit/JGitDiff.scala b/scalafix-cli/src/main/scala/scalafix/internal/jgit/JGitDiff.scala index 9b01d6d99..2c42f156a 100644 --- a/scalafix-cli/src/main/scala/scalafix/internal/jgit/JGitDiff.scala +++ b/scalafix-cli/src/main/scala/scalafix/internal/jgit/JGitDiff.scala @@ -113,7 +113,7 @@ object JGitDiff { walk.dispose() Right(treeParser) } catch { - case missing: MissingObjectException => + case _: MissingObjectException => unknown(id.getName) } } diff --git a/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala b/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala index e59db1196..080c6ca2d 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala @@ -1,7 +1,6 @@ package scalafix.internal.patch import scala.annotation.tailrec -import scala.collection.compat._ import scala.collection.immutable.TreeMap import scala.collection.mutable import scala.collection.mutable.ListBuffer @@ -181,7 +180,7 @@ object EscapeHatch { if (isEmpty) (true, None) else { val escapesUpToPos = - escapeTree.rangeTo(EscapeOffset(position)).valuesIterator.flatten + escapeTree.to(EscapeOffset(position)).valuesIterator.flatten escapesUpToPos .collectFirst { case f @ EscapeFilter(_, _, _, Some(end)) @@ -328,7 +327,7 @@ object EscapeHatch { if (disabling.isEmpty) (true, None) else { val disables = - disabling.rangeTo(EscapeOffset(position)).valuesIterator.flatten + disabling.to(EscapeOffset(position)).valuesIterator.flatten loop(disables.toList, None) } } @@ -485,7 +484,8 @@ object EscapeHatch { } val disable = TreeMap.newBuilder[EscapeOffset, List[EscapeFilter]] - val unused = ListBuffer.from(onOffTracker.allUnused) + val unused = ListBuffer.empty[Position] + unused ++= onOffTracker.allUnused // for filters starting on the same offset, pick the one with the wider range // and mark the others as unused 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 f4248dcb2..55c8bd8b0 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/patch/ImportPatchOps.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/patch/ImportPatchOps.scala @@ -57,13 +57,6 @@ object ImportPatchOps { .collect { case i: Import => i } } - @tailrec private final def getLastTopLevelPkg(potPkg: Stat): Stat = - potPkg match { - case Pkg(_, head +: Nil) => getLastTopLevelPkg(head) - case Pkg(_, head +: _) => head - case _ => potPkg - } - @tailrec private final def getGlobalImports(ast: Tree): Seq[Import] = ast match { case Pkg(_, Seq(pkg: Pkg)) => getGlobalImports(pkg) diff --git a/scalafix-core/src/main/scala/scalafix/internal/patch/PatchInternals.scala b/scalafix-core/src/main/scala/scalafix/internal/patch/PatchInternals.scala index 735191a6f..1432e9d4e 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/patch/PatchInternals.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/patch/PatchInternals.scala @@ -36,7 +36,7 @@ object PatchInternals { ) case (_: Remove, add: Add) => add.copy(keepTok = false) case (add: Add, _: Remove) => add.copy(keepTok = false) - case (rem: Remove, rem2: Remove) => rem + case (rem: Remove, _: Remove) => rem case _ => throw Failure.TokenPatchMergeError(a, b) } diff --git a/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala b/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala index bdc10bb79..052dbaf50 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala @@ -45,7 +45,7 @@ object ReplaceSymbolOps { case (a @ Name(_), Symbol.Global(Symbol.None, SignatureName(b))) => ctx.replaceTree(a, b) -> Symbol.None // ref is shorter - case (a @ Name(_), sym @ Symbol.Global(owner, SignatureName(b))) => + case (a @ Name(_), sym @ Symbol.Global(_, SignatureName(b))) => if (isImport) { val qual = SymbolOps.toTermRef(sym) ctx.replaceTree(a, qual.syntax) -> Symbol.None @@ -106,7 +106,7 @@ object ReplaceSymbolOps { case Some(Importer(ref, `i` :: Nil)) => Patch.replaceTree(ref, SymbolOps.toTermRef(to.owner).syntax) + Patch.replaceTree(name, to.signature.name) - case Some(Importer(ref, _)) => + case Some(Importer(_, _)) => Patch.removeImportee(i) + Patch.addGlobalImport( Importer( @@ -125,7 +125,7 @@ object ReplaceSymbolOps { case Some(Identifier(parent, Symbol.Global(_, sig))) if sig.name != parent.value => Patch.empty // do nothing because it was a renamed symbol - case Some(parent) => + case Some(_) => val addImport = if (n.isDefinition) Patch.empty else ctx.addGlobalImport(to) diff --git a/scalafix-core/src/main/scala/scalafix/internal/util/Pretty.scala b/scalafix-core/src/main/scala/scalafix/internal/util/Pretty.scala index ac2c5131a..a0ca45f70 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/util/Pretty.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/util/Pretty.scala @@ -230,7 +230,7 @@ object Pretty { pretty(t.constant) case t: MacroExpansionTree => `(` + Doc.text("`after-expansion` : ") + pretty(t.tpe) + `)` - case r: OriginalTree => + case _: OriginalTree => `*` case r: OriginalSubTree => Doc.text("orig") + `(` + Doc.text(r.tree.syntax) + `)` diff --git a/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala b/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala index 7672108b3..f601ad3de 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala @@ -230,7 +230,7 @@ class PrettyType private ( toType(ret) ) } - case s.ClassSignature(Some(tparams), parents, self, Some(decls)) => + case s.ClassSignature(Some(tparams), parents, _, Some(decls)) => val declarations = decls.infos val isCaseClass = info.is(p.CASE) def objectDecls: List[Stat] = @@ -568,7 +568,7 @@ class PrettyType private ( Term.This(Name.Anonymous()), Type.Name(this.info(symbol).displayName) ) - case s.SuperType(prefix @ _, symbol) => + case s.SuperType(_ @_, symbol) => // TODO: print prefix https://github.com/scalacenter/scalafix/issues/758 Type.Select( Term.Super(Name.Anonymous(), Name.Anonymous()), diff --git a/scalafix-core/src/main/scala/scalafix/internal/util/SymbolOps.scala b/scalafix-core/src/main/scala/scalafix/internal/util/SymbolOps.scala index 3f047b9c0..41b40a5bb 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/util/SymbolOps.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/util/SymbolOps.scala @@ -49,7 +49,7 @@ object SymbolOps { } def toImporter(symbol: Symbol): Option[Importer] = { symbol match { - case Root(SignatureName(name)) => + case Root(SignatureName(_)) => None case Symbol.Global(qual, SignatureName(name)) => Some( diff --git a/scalafix-core/src/main/scala/scalafix/internal/v0/LegacyCodePrinter.scala b/scalafix-core/src/main/scala/scalafix/internal/v0/LegacyCodePrinter.scala index 42c884d4b..fb905a331 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/v0/LegacyCodePrinter.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/v0/LegacyCodePrinter.scala @@ -63,7 +63,7 @@ class LegacyCodePrinter(doc: SemanticDocument) { private def pprint(signature: s.Signature): Unit = { signature match { - case sig: s.ClassSignature => + case _: s.ClassSignature => // Not supported () @@ -85,7 +85,7 @@ class LegacyCodePrinter(doc: SemanticDocument) { } pprint(sig.returnType) - case sig: s.TypeSignature => + case _: s.TypeSignature => // Not supported () diff --git a/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala b/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala index a4c8ce1cc..adc13a344 100644 --- a/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala +++ b/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala @@ -98,7 +98,7 @@ object TreeOps { } } val result = tree match { - case n: Name => + case _: Name => tree.parent.flatMap(loop) case _ => loop(tree) } diff --git a/scalafix-reflect/src/main/scala-3/scalafix/internal/reflect/RuleCompiler.scala b/scalafix-reflect/src/main/scala-3/scalafix/internal/reflect/RuleCompiler.scala index 09c1cc712..232433eda 100644 --- a/scalafix-reflect/src/main/scala-3/scalafix/internal/reflect/RuleCompiler.scala +++ b/scalafix-reflect/src/main/scala-3/scalafix/internal/reflect/RuleCompiler.scala @@ -17,7 +17,6 @@ import dotty.tools.repl.AbstractFileClassLoader import metaconfig.ConfError import metaconfig.Configured import metaconfig.Input -import metaconfig.Position class RuleCompiler( classpath: String, targetDirectory: Option[File] = None @@ -43,8 +42,8 @@ class RuleCompiler( val run: Run = compiler.newRun(using ctx) val file: AbstractFile = input match { - case Input.File(path, _) => AbstractFile.getFile(input.path) - case Input.VirtualFile(path, _) => + case Input.File(_, _) => AbstractFile.getFile(input.path) + case Input.VirtualFile(_, _) => VirtualFile(input.path, input.text.getBytes()) case _ => throw RuntimeException("Invalid Input file") } diff --git a/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala b/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala index 2d5f420a6..3ce14eb4b 100644 --- a/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala +++ b/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala @@ -73,7 +73,7 @@ object RuleInstrumentation { } (dialects.Scala213, code).parse[Source] match { - case parsers.Parsed.Error(pos, msg, details) => + case parsers.Parsed.Error(pos, msg, _) => ConfError.parseError(pos.toMetaconfig, msg).notOk case parsers.Parsed.Success(ast) => val result = List.newBuilder[String] diff --git a/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala b/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala index a33054a8f..1f9b7bf5b 100644 --- a/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala +++ b/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala @@ -233,7 +233,7 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)( context: Context, tpe: Type ): Option[(Type, v1.Patch)] = tpe.finalResultType match { - case resultType @ RefinedType(parents, decls) + case RefinedType(_, decls) if config.rewriteStructuralTypesToNamedSubclass && decls.filterNot(_.isOverridingSymbol).nonEmpty => val body: Option[m.Term] = defn match { @@ -243,7 +243,7 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)( case _ => None } body match { - case Some(body @ m.Term.NewAnonymous(template)) + case Some(body @ m.Term.NewAnonymous(_)) if body.tokens.head.syntax == "new" => val nameSyntax = gsym.nameSyntax val suffixes = "" +: LazyList.from(1).map(_.toString()) @@ -303,7 +303,7 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)( case TypeRef(pre, sym, args) if gsymbolReplacements.contains(semanticdbSymbol(sym)) => loop(TypeRef(pre, gsymbolReplacements(semanticdbSymbol(sym)), args)) - case tp @ ThisType(sym) + case tp @ ThisType(_) if tp.toString() == s"${gsym.owner.nameString}.this.type" => new PrettyType("this.type") case ConstantType(Constant(c: Symbol)) if c.hasFlag(gf.JAVA_ENUM) => @@ -364,9 +364,9 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)( } else { TypeRef(loop(pre), sym, args.map(loop)) } - case ExistentialType(head :: Nil, underlying) => + case ExistentialType(head :: Nil, _) => head.info match { - case b @ TypeBounds(RefinedType(parents, _), hi) + case TypeBounds(RefinedType(parents, _), hi) if parents.length > 1 => // Remove the lower bound large `Type[_ >: A with B with C <: D // with Serializable]` so that it becomes only `Type[_ <: D]`. @@ -376,7 +376,7 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)( case _ => } tpe - case SingleType(ThisType(osym), sym) + case SingleType(ThisType(_), sym) if sym.isKindaTheSameAs(sym) && gsymbolReplacements.contains(semanticdbSymbol(sym)) => loop( diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala index 4d35ccba7..0a80ce071 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala @@ -208,7 +208,7 @@ final class DisableSyntax(config: DisableSyntaxConfig) t.pos ) ) - case t @ AbstractWithVals(vals) if config.noValInAbstract => + case _ @AbstractWithVals(vals) if config.noValInAbstract => vals.map { v => Diagnostic( "valInAbstract", @@ -225,7 +225,7 @@ final class DisableSyntax(config: DisableSyntaxConfig) t.pos ) ) - case t @ Defn.Def(mods, _, _, paramss, _, _) + case t @ Defn.Def(mods, _, _, _, _, _) if mods.exists(_.is[Mod.Implicit]) && hasNonImplicitParam(t) && config.noImplicitConversion => diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala index fc69acc87..86c67ed83 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala @@ -1,7 +1,5 @@ package scalafix.internal.rule -import scala.collection.compat._ - import scala.meta._ import scalafix.v1._ @@ -21,7 +19,7 @@ class NoAutoTupling extends SemanticRule("NoAutoTupling") { override def fix(implicit doc: SemanticDocument): Patch = { val unitAdaptations: Set[Position] = - doc.diagnostics.iterator.collect { + doc.diagnostics.collect { case message if message.message.startsWith( "Adaptation of argument list by inserting ()" @@ -32,7 +30,7 @@ class NoAutoTupling extends SemanticRule("NoAutoTupling") { }.toSet val tupleAdaptations: Set[Position] = - doc.diagnostics.iterator.collect { + doc.diagnostics.collect { case message if message.message.startsWith( "Adapting argument list by creating" diff --git a/scalafix-tests/expect/src/test/scala/scalafix/tests/rule/RuleSuite.scala b/scalafix-tests/expect/src/test/scala/scalafix/tests/rule/RuleSuite.scala index 43c6535dc..b9139132b 100644 --- a/scalafix-tests/expect/src/test/scala/scalafix/tests/rule/RuleSuite.scala +++ b/scalafix-tests/expect/src/test/scala/scalafix/tests/rule/RuleSuite.scala @@ -9,7 +9,7 @@ import scalafix.testkit._ object RuleSuite { def main(args: Array[String]): Unit = { if (Array("--save-expect").sameElements(args)) { - val suite = new AbstractSemanticRuleSuite( + new AbstractSemanticRuleSuite( TestkitProperties.loadFromResources(), isSaveExpect = true ) with AnyFunSuiteLike { diff --git a/scalafix-tests/integration/src/main/scala/scalafix/test/ExplicitSynthetic.scala b/scalafix-tests/integration/src/main/scala/scalafix/test/ExplicitSynthetic.scala index 2a7ae616d..2a72e1c59 100644 --- a/scalafix-tests/integration/src/main/scala/scalafix/test/ExplicitSynthetic.scala +++ b/scalafix-tests/integration/src/main/scala/scalafix/test/ExplicitSynthetic.scala @@ -1,10 +1,10 @@ package scalafix.test -import scalafix.v1.SemanticRule +import scala.meta._ + +import scalafix.v1._ class ExplicitSynthetic() extends SemanticRule("ExplicitSynthetic") { - import scalafix.v1._ - import scala.meta._ override def fix(implicit doc: SemanticDocument): Patch = { val patches = doc.tree.collect { diff --git a/scalafix-tests/integration/src/main/scala/test/TypeToTreeInput.scala b/scalafix-tests/integration/src/main/scala/test/TypeToTreeInput.scala index e36001fac..8c8b4df6f 100644 --- a/scalafix-tests/integration/src/main/scala/test/TypeToTreeInput.scala +++ b/scalafix-tests/integration/src/main/scala/test/TypeToTreeInput.scala @@ -1,3 +1,4 @@ +// scalafix:off RemoveUnused package test import java.util.Map diff --git a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/BaseCliSuite.scala b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/BaseCliSuite.scala index bf7a64c10..9bd2e5712 100644 --- a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/BaseCliSuite.scala +++ b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/BaseCliSuite.scala @@ -169,7 +169,6 @@ trait BaseCliSuite extends AnyFunSuite with DiffAssertions { files: String = removeImportsPath.toString() ): Unit = { test(name, SkipWindows) { - val fileIsFixed = expectedExit.isOk val cwd = Files.createTempDirectory("scalafix") val sourceDir = inputSourceroot.toRelative(AbsolutePath(BuildInfo.baseDirectory)).toNIO diff --git a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala index ef89d3615..309827dfd 100644 --- a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala +++ b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala @@ -154,7 +154,7 @@ class PrettyTypeFuzzSuite extends BasePrettyTypeSuite { try { checkPath(file) } catch { - case scala.meta.internal.classpath.MissingSymbolException(e) => + case scala.meta.internal.classpath.MissingSymbolException(_) => println(file) } } diff --git a/scalafix-tests/integration/src/test/scala/scalafix/tests/cli/ScalafixImplSuite.scala b/scalafix-tests/integration/src/test/scala/scalafix/tests/cli/ScalafixImplSuite.scala index 64bedd0d1..7b55bc110 100644 --- a/scalafix-tests/integration/src/test/scala/scalafix/tests/cli/ScalafixImplSuite.scala +++ b/scalafix-tests/integration/src/test/scala/scalafix/tests/cli/ScalafixImplSuite.scala @@ -192,7 +192,7 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions { semicolon.toString, excluded.toString ) ++ CompatSemanticdb.scalacOptions(src) - val compileSucceeded = scala.tools.nsc.Main.process(scalacOptions) + scala.tools.nsc.Main.process(scalacOptions) val buf = List.newBuilder[ScalafixDiagnostic] val callback = new ScalafixMainCallback { override def reportDiagnostic(diagnostic: ScalafixDiagnostic): Unit = { diff --git a/scalafix-tests/integration/src/test/scala/scalafix/tests/interfaces/ScalafixArgumentsSuite.scala b/scalafix-tests/integration/src/test/scala/scalafix/tests/interfaces/ScalafixArgumentsSuite.scala index 0eae51b18..58860c05f 100644 --- a/scalafix-tests/integration/src/test/scala/scalafix/tests/interfaces/ScalafixArgumentsSuite.scala +++ b/scalafix-tests/integration/src/test/scala/scalafix/tests/interfaces/ScalafixArgumentsSuite.scala @@ -230,7 +230,7 @@ class ScalafixArgumentsSuite extends AnyFunSuite with DiffAssertions { assert(maybeDiagnostic.isEmpty) val content = FileIO.slurp(AbsolutePath(main), StandardCharsets.UTF_8) assert(contentBeforeEvaluation == content) - val run = api + api .withRules( List( removeUnsuedRule().name.toString(),