From f486d093287167b17ecee8e968814041e676e540 Mon Sep 17 00:00:00 2001 From: Thibault Czarniak Date: Mon, 26 Aug 2024 12:59:48 +0200 Subject: [PATCH] Added WhileTrue and InterpolationToString --- .../scala/fix/InterpolationToString.scala | 20 ++++++++++++ input/src/main/scala/fix/WhileTrue.scala | 16 ++++++++++ .../META-INF/services/scalafix.v1.Rule | 4 ++- .../scala/fix/InterpolationToString.scala | 32 +++++++++++++++++++ rules/src/main/scala/fix/WhileTrue.scala | 28 ++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 input/src/main/scala/fix/InterpolationToString.scala create mode 100644 input/src/main/scala/fix/WhileTrue.scala create mode 100644 rules/src/main/scala/fix/InterpolationToString.scala create mode 100644 rules/src/main/scala/fix/WhileTrue.scala diff --git a/input/src/main/scala/fix/InterpolationToString.scala b/input/src/main/scala/fix/InterpolationToString.scala new file mode 100644 index 0000000..75abb7d --- /dev/null +++ b/input/src/main/scala/fix/InterpolationToString.scala @@ -0,0 +1,20 @@ +/* +rule = InterpolationToString + */ +package fix + +object InterpolationToString { + + val a = 1 + println(s" ${a.toString} b c ") // assert: InterpolationToString + println(s" ${2.toString} b ") // assert: InterpolationToString + + println(f"${2.toString} b") // assert: InterpolationToString + println(f"${a.toString} b") // assert: InterpolationToString + + println(s" $a b c ") // scalafix: ok; + println(f"$a b") // scalafix: ok; + + println(s"${a == 2} b") // scalafix: ok; + println(s"${a == 2} b") // scalafix: ok; +} diff --git a/input/src/main/scala/fix/WhileTrue.scala b/input/src/main/scala/fix/WhileTrue.scala new file mode 100644 index 0000000..d6f5297 --- /dev/null +++ b/input/src/main/scala/fix/WhileTrue.scala @@ -0,0 +1,16 @@ +/* +rule = WhileTrue + */ +package fix + +object WhileTrue { + + while (true) { // assert: WhileTrue + println("sam") + } + + while (System.currentTimeMillis > 0) { // scalafix: ok; + println("sam") + } + +} diff --git a/rules/src/main/resources/META-INF/services/scalafix.v1.Rule b/rules/src/main/resources/META-INF/services/scalafix.v1.Rule index 3f03264..6dc0806 100644 --- a/rules/src/main/resources/META-INF/services/scalafix.v1.Rule +++ b/rules/src/main/resources/META-INF/services/scalafix.v1.Rule @@ -45,4 +45,6 @@ fix.UnnecessaryConversion fix.UnreachableCatch fix.UnusedMethodParameter fix.VarCouldBeVal -fix.VariableShadowing \ No newline at end of file +fix.VariableShadowing +fix.WhileTrue +fix.InterpolationToString \ No newline at end of file diff --git a/rules/src/main/scala/fix/InterpolationToString.scala b/rules/src/main/scala/fix/InterpolationToString.scala new file mode 100644 index 0000000..f6be32f --- /dev/null +++ b/rules/src/main/scala/fix/InterpolationToString.scala @@ -0,0 +1,32 @@ +/* +rule = InterpolationToString + */ +package fix + +import scalafix.lint.LintSeverity +import scalafix.v1._ + +import scala.meta._ + +class InterpolationToString extends SemanticRule("InterpolationToString") { + + private def diag(pos: Position) = Diagnostic( + "", + "Checks for string interpolations that have .toString in their arguments", + pos, + "A call to .toString is not necessary, since arguments in format strings will be replaced by the value of a toString call on them.", + LintSeverity.Warning + ) + + override def fix(implicit doc: SemanticDocument): Patch = { + doc.tree.collect { + case Term.Interpolate(_, _, args) => + args.collect { + // Corresponds to ${x.toString()} or ${x.toString} arguments in string interpolations + case t @ Term.Block(List(Term.Apply.After_4_6_0(Term.Select(_, Term.Name("toString")), _) | Term.Select(_, Term.Name("toString")))) => Patch.lint(diag(t.pos)) + case _ => Patch.empty + } + case _ => List(Patch.empty) + }.flatten.asPatch + } +} diff --git a/rules/src/main/scala/fix/WhileTrue.scala b/rules/src/main/scala/fix/WhileTrue.scala new file mode 100644 index 0000000..7fb5161 --- /dev/null +++ b/rules/src/main/scala/fix/WhileTrue.scala @@ -0,0 +1,28 @@ +/* +rule = WhileTrue + */ +package fix + +import scalafix.lint.LintSeverity +import scalafix.v1._ + +import scala.meta._ + +class WhileTrue extends SemanticRule("WhileTrue") { + + private def diag(pos: Position) = Diagnostic( + "", + "Checks for code that uses a while(true) block.", + pos, + "A while true loop is unlikely to be meant for production.", + LintSeverity.Warning + ) + + override def fix(implicit doc: SemanticDocument): Patch = { + doc.tree.collect { + case w @ Term.While(Lit.Boolean(true), _) => Patch.lint(diag(w.pos)) + // This is only for compatibility with Scala version < 3. Do-while support has been dropped in Scala 3 + case d @ Term.Do(_, Lit.Boolean(true)) => Patch.lint(diag(d.pos)) + }.asPatch + } +}