-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added WhileTrue and InterpolationToString
- Loading branch information
Showing
5 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |