From c4ff5692579cc487f0f239b53620f949e6c7b7a8 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Sat, 28 Sep 2024 10:35:27 +0900 Subject: [PATCH] fix warnings --- rules/src/main/scala/fix/CirceCodec.scala | 4 ++-- .../scala/fix/DirectoryAndPackageName.scala | 2 +- rules/src/main/scala/fix/DiscardValue.scala | 2 +- .../scala/fix/DuplicateWildcardImport.scala | 2 +- rules/src/main/scala/fix/MapToForeach.scala | 2 +- rules/src/main/scala/fix/ObjectFinal.scala | 2 +- rules/src/main/scala/fix/ObjectSelfType.scala | 21 +++++++++------- rules/src/main/scala/fix/RemovePureEff.scala | 2 +- rules/src/main/scala/fix/ReplaceFill.scala | 24 +++++++++++-------- .../main/scala/fix/SameParamOverloading.scala | 2 +- .../src/main/scala/fix/SimplifyForYield.scala | 2 +- .../scala/fix/SyntacticOrganizeImports.scala | 2 +- .../scala/fix/UnnecessarySortRewrite.scala | 2 +- 13 files changed, 38 insertions(+), 31 deletions(-) diff --git a/rules/src/main/scala/fix/CirceCodec.scala b/rules/src/main/scala/fix/CirceCodec.scala index 141d23f..c8390ba 100644 --- a/rules/src/main/scala/fix/CirceCodec.scala +++ b/rules/src/main/scala/fix/CirceCodec.scala @@ -82,7 +82,7 @@ class CirceCodec extends SyntacticRule("CirceCodec") { Patch.removeTokens(annotation.tokens), objectOpt match { case Some(obj) => - Patch.addRight(obj.templ.stats.last, instance) + Patch.addRight(obj.templ.body.stats.last, instance) case None => Patch.addRight(clazz, s"\n\nobject ${className} {${instance}\n}") } @@ -95,7 +95,7 @@ class CirceCodec extends SyntacticRule("CirceCodec") { }.flatten if (result.nonEmpty) { - val pkg = src.collect { case p: Pkg => p.stats.head }.head + val pkg = src.collect { case p: Pkg => p.body.stats.head }.head val annotationImport = src.collect { case i @ Import( diff --git a/rules/src/main/scala/fix/DirectoryAndPackageName.scala b/rules/src/main/scala/fix/DirectoryAndPackageName.scala index c513a7a..cf3f68e 100644 --- a/rules/src/main/scala/fix/DirectoryAndPackageName.scala +++ b/rules/src/main/scala/fix/DirectoryAndPackageName.scala @@ -123,7 +123,7 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn override def isLinter = true private def getPackages(t: Tree): List[Term.Ref] = { - t.collect { case p: Pkg => p.ref :: p.stats.flatMap(getPackages) }.flatten.distinct + t.collect { case p: Pkg => p.ref :: p.body.stats.flatMap(getPackages) }.flatten.distinct } override def fix(implicit doc: SyntacticDocument): Patch = { diff --git a/rules/src/main/scala/fix/DiscardValue.scala b/rules/src/main/scala/fix/DiscardValue.scala index ab9833a..15641cc 100644 --- a/rules/src/main/scala/fix/DiscardValue.scala +++ b/rules/src/main/scala/fix/DiscardValue.scala @@ -64,7 +64,7 @@ object DiscardValue { case t: Term.Block => t.stats case t: Template => - t.stats + t.body.stats } } diff --git a/rules/src/main/scala/fix/DuplicateWildcardImport.scala b/rules/src/main/scala/fix/DuplicateWildcardImport.scala index d833d00..ae6f956 100644 --- a/rules/src/main/scala/fix/DuplicateWildcardImport.scala +++ b/rules/src/main/scala/fix/DuplicateWildcardImport.scala @@ -49,7 +49,7 @@ class DuplicateWildcardImport(conf: DuplicateWildcardImportConfig) extends Synta } t.stats.collect { case p: Pkg => - p.stats + p.body.stats }.flatten.collect { case i: Import => i -> i.importers }.collect { case (i, x :: Nil) => diff --git a/rules/src/main/scala/fix/MapToForeach.scala b/rules/src/main/scala/fix/MapToForeach.scala index 4ee28ea..b9aec53 100644 --- a/rules/src/main/scala/fix/MapToForeach.scala +++ b/rules/src/main/scala/fix/MapToForeach.scala @@ -12,7 +12,7 @@ class MapToForeach extends SyntacticRule("MapToForeach") { override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree.collect { case t: Template => - t.stats.collect { + t.body.stats.collect { case Term.Apply.Initial( Term.Select(_, method @ Term.Name("map")), _ :: Nil diff --git a/rules/src/main/scala/fix/ObjectFinal.scala b/rules/src/main/scala/fix/ObjectFinal.scala index a11c106..8a8ff2e 100644 --- a/rules/src/main/scala/fix/ObjectFinal.scala +++ b/rules/src/main/scala/fix/ObjectFinal.scala @@ -101,7 +101,7 @@ class ObjectFinal extends SyntacticRule("ObjectFinal") { override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree.collect { case o: Defn.Object => - o.templ.stats.collect { + o.templ.body.stats.collect { case Defn.Val( ObjectFinal.FinalMod(m), List(Pat.Var(_: Term.Name)), diff --git a/rules/src/main/scala/fix/ObjectSelfType.scala b/rules/src/main/scala/fix/ObjectSelfType.scala index f6772d1..e0c440e 100644 --- a/rules/src/main/scala/fix/ObjectSelfType.scala +++ b/rules/src/main/scala/fix/ObjectSelfType.scala @@ -7,22 +7,25 @@ import scalafix.Patch import scalafix.lint.LintSeverity import scalafix.v1.SyntacticDocument import scalafix.v1.SyntacticRule +import scalafix.v1.XtensionOptionPatch import scalafix.v1.XtensionSeqPatch class ObjectSelfType extends SyntacticRule("ObjectSelfType") { override def isLinter = true override def fix(implicit doc: SyntacticDocument): Patch = { - doc.tree.collect { - case obj: Defn.Object if obj.templ.self.decltpe.isDefined => - Patch.lint( - Diagnostic( - id = "", - position = obj.templ.self.pos, - message = "objects must not have a self type", - severity = LintSeverity.Warning, + doc.tree.collect { case obj: Defn.Object => + obj.templ.body.selfOpt.collect { + case self if self.decltpe.isDefined => + Patch.lint( + Diagnostic( + id = "", + position = self.pos, + message = "objects must not have a self type", + severity = LintSeverity.Warning, + ) ) - ) + }.asPatch }.asPatch } } diff --git a/rules/src/main/scala/fix/RemovePureEff.scala b/rules/src/main/scala/fix/RemovePureEff.scala index 9a42ae1..9d99750 100644 --- a/rules/src/main/scala/fix/RemovePureEff.scala +++ b/rules/src/main/scala/fix/RemovePureEff.scala @@ -13,7 +13,7 @@ import scalafix.v1.XtensionSeqPatch class RemovePureEff extends SyntacticRule("RemovePureEff") { override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree.collect { case x1: Term.ForYield => - x1.enums + x1.enumsBlock.enums .drop(1) .collect { case x @ Enumerator.Generator( diff --git a/rules/src/main/scala/fix/ReplaceFill.scala b/rules/src/main/scala/fix/ReplaceFill.scala index 1f85df1..bc62b69 100644 --- a/rules/src/main/scala/fix/ReplaceFill.scala +++ b/rules/src/main/scala/fix/ReplaceFill.scala @@ -58,11 +58,13 @@ class ReplaceFill extends SyntacticRule("ReplaceFill") { case Name(n) if n == param.name.value => () }.isEmpty => Patch.replaceTree(t, s"${tpe}.fill(${size}){$body}") - case t @ Term.ForYield( - List( - Enumerator.Generator( - Pat.Var(n @ Term.Name(_)), - ToUntil(size, tpe) + case t @ Term.ForYield.After_4_9_9( + Term.EnumeratorsBlock( + List( + Enumerator.Generator( + Pat.Var(n @ Term.Name(_)), + ToUntil(size, tpe) + ) ) ), body @@ -70,11 +72,13 @@ class ReplaceFill extends SyntacticRule("ReplaceFill") { case Name(x) if x == n.value => () }.isEmpty => Patch.replaceTree(t, s"${tpe}.fill(${size}){$body}") - case t @ Term.ForYield( - List( - Enumerator.Generator( - Pat.Wildcard(), - ToUntil(size, tpe) + case t @ Term.ForYield.After_4_9_9( + Term.EnumeratorsBlock( + List( + Enumerator.Generator( + Pat.Wildcard(), + ToUntil(size, tpe) + ) ) ), body diff --git a/rules/src/main/scala/fix/SameParamOverloading.scala b/rules/src/main/scala/fix/SameParamOverloading.scala index 3cd1927..2eb4799 100644 --- a/rules/src/main/scala/fix/SameParamOverloading.scala +++ b/rules/src/main/scala/fix/SameParamOverloading.scala @@ -48,7 +48,7 @@ class SameParamOverloading extends SyntacticRule("SameParamOverloading") { import SameParamOverloading.* override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree.collect { case t: Template => - val overloadMethods = t.stats.collect { + val overloadMethods = t.body.stats.collect { case a: Defn.Def => a.name -> Method(a) case a: Decl.Def => diff --git a/rules/src/main/scala/fix/SimplifyForYield.scala b/rules/src/main/scala/fix/SimplifyForYield.scala index 2e2effa..ef7c12b 100644 --- a/rules/src/main/scala/fix/SimplifyForYield.scala +++ b/rules/src/main/scala/fix/SimplifyForYield.scala @@ -15,7 +15,7 @@ class SimplifyForYield extends SyntacticRule("SimplifyForYield") { override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree.collect { case x1: Term.ForYield => - val generatorAndRhs = x1.enums match { + val generatorAndRhs = x1.enumsBlock.enums match { case List(x2: Generator) => x2.pat match { case x3: Pat.Var => diff --git a/rules/src/main/scala/fix/SyntacticOrganizeImports.scala b/rules/src/main/scala/fix/SyntacticOrganizeImports.scala index d623a29..ad38c9a 100644 --- a/rules/src/main/scala/fix/SyntacticOrganizeImports.scala +++ b/rules/src/main/scala/fix/SyntacticOrganizeImports.scala @@ -37,7 +37,7 @@ class SyntacticOrganizeImports extends SyntacticRule("SyntacticOrganizeImports") override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree.collect { case p: Pkg => // find top-level imports - val imports = collectWhile(p.stats.dropWhile(!_.is[Import])) { case i: Import => i }.sortBy(_.pos.startLine) + val imports = collectWhile(p.body.stats.dropWhile(!_.is[Import])) { case i: Import => i }.sortBy(_.pos.startLine) val importers = imports.map(_.importers).collect { case i :: Nil => i } if (imports.nonEmpty && (imports.lengthCompare(importers.size) == 0)) { val start = imports.map(_.pos.startLine).min diff --git a/rules/src/main/scala/fix/UnnecessarySortRewrite.scala b/rules/src/main/scala/fix/UnnecessarySortRewrite.scala index 245cc90..235a747 100644 --- a/rules/src/main/scala/fix/UnnecessarySortRewrite.scala +++ b/rules/src/main/scala/fix/UnnecessarySortRewrite.scala @@ -88,7 +88,7 @@ class UnnecessarySortRewrite(config: UnnecessarySortRewriteConfig) extends Synta if (result.nonEmpty) { val patch = result.map(_._1).asPatch - val pkg = src.collect { case p: Pkg => p.stats.head }.head + val pkg = src.collect { case p: Pkg => p.body.stats.head }.head if (result.exists(_._2)) { Seq( Patch.addLeft(pkg, "\nimport scala.collection.compat._\n"),