Skip to content

Commit

Permalink
Added WhileTrue and InterpolationToString
Browse files Browse the repository at this point in the history
  • Loading branch information
t1b00 committed Aug 26, 2024
1 parent 814c77e commit f486d09
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
20 changes: 20 additions & 0 deletions input/src/main/scala/fix/InterpolationToString.scala
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 16 additions & 0 deletions input/src/main/scala/fix/WhileTrue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
rule = WhileTrue
*/
package fix

object WhileTrue {

while (true) { // assert: WhileTrue
println("sam")
}

while (System.currentTimeMillis > 0) { // scalafix: ok;
println("sam")
}

}
4 changes: 3 additions & 1 deletion rules/src/main/resources/META-INF/services/scalafix.v1.Rule
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ fix.UnnecessaryConversion
fix.UnreachableCatch
fix.UnusedMethodParameter
fix.VarCouldBeVal
fix.VariableShadowing
fix.VariableShadowing
fix.WhileTrue
fix.InterpolationToString
32 changes: 32 additions & 0 deletions rules/src/main/scala/fix/InterpolationToString.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
28 changes: 28 additions & 0 deletions rules/src/main/scala/fix/WhileTrue.scala
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit f486d09

Please sign in to comment.