Skip to content

Commit

Permalink
fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Sep 28, 2024
1 parent fe39969 commit 95b97f9
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions rules/src/main/scala/fix/CirceCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/DirectoryAndPackageName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/DiscardValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object DiscardValue {
case t: Term.Block =>
t.stats
case t: Template =>
t.stats
t.body.stats
}
}

Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/DuplicateWildcardImport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/MapToForeach.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/ObjectFinal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
21 changes: 12 additions & 9 deletions rules/src/main/scala/fix/ObjectSelfType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ import scalafix.lint.LintSeverity
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionSeqPatch
import scalafix.v1.XtensionOptionPatch

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
}
}
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/RemovePureEff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 14 additions & 10 deletions rules/src/main/scala/fix/ReplaceFill.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,27 @@ 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
) if body.collect {
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
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/SameParamOverloading.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/SimplifyForYield.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/SyntacticOrganizeImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/UnnecessarySortRewrite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 95b97f9

Please sign in to comment.