Skip to content

Commit

Permalink
Avoid boxing primitives with Any2StringAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jun 18, 2019
1 parent 7319678 commit f36f999
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
16 changes: 8 additions & 8 deletions output/src/main/scala/fix/scala213/Any2StringAdd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ abstract class Any2StringAdd {
//
def unit1 = unit.toString + s
def bool1 = bool.toString + s
def byte1 = byte.toString + s
def byte1 = "" + byte + s
def byte2 = byte + byte
def short1 = short.toString + s
def short1 = "" + short + s
def short2 = short + short
def char1 = char.toString + s
def char1 = "" + char + s
def char2 = char + char
def int1 = int.toString + s
def int1 = "" + int + s
def int2 = int + int
def long1 = long.toString + s
def long1 = "" + long + s
def long2 = long + long
def float1 = float.toString + s
def float1 = "" + float + s
def float2 = float + float
def double1 = double.toString + s
def double1 = "" + double + s
def double2 = double + double

// With infix operators, make sure to use parens
def parens1 = (Nil ++ Nil).toString + s
def parens2 = (int + int).toString + s
def parens2 = "" + (int + int) + s
def parens3 = {Nil ++ Nil}.toString + s
}
12 changes: 9 additions & 3 deletions rewrites/src/main/scala/fix/scala213/Any2StringAdd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import scalafix.v1._
import scala.meta._

object Any2StringAdd {
val plusString = SymbolMatcher.exact(
"scala/Predef.any2stringadd#`+`().",
val any2stringaddPlusString = SymbolMatcher.exact("scala/Predef.any2stringadd#`+`().")
val primitivePlusString = SymbolMatcher.exact(
"scala/Byte#`+`().",
"scala/Short#`+`().",
"scala/Char#`+`().",
Expand All @@ -22,12 +22,18 @@ final class Any2StringAdd extends SemanticRule("fix.scala213.Any2StringAdd") {

override def fix(implicit doc: SemanticDocument): Patch = {
doc.tree.collect {
case plusString(Term.ApplyInfix(lhs, _, _, _)) => addToString(lhs)
case any2stringaddPlusString(Term.ApplyInfix(lhs, _, _, _)) => addToString(lhs)
case primitivePlusString(Term.ApplyInfix(lhs, _, _, _)) => blankStringPlus(lhs)
}.asPatch
}

private def addToString(term: Term) = term match {
case _: Term.Name | _: Term.Select | _: Term.Block => Patch.addRight(term, ".toString")
case _ => Patch.addLeft(term, "(") + Patch.addRight(term, ").toString")
}

private def blankStringPlus(term: Term) = term match {
case _: Term.Name | _: Term.Select | _: Term.Block => Patch.addLeft(term, """"" + """)
case _ => Patch.addLeft(term, """"" + (""") + Patch.addRight(term, ")")
}
}

0 comments on commit f36f999

Please sign in to comment.