Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new wart: NonPrimitiveInStringInterpolation #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.wartremover
package contrib.warts

import scala.annotation.tailrec

object NonPrimitiveInStringInterpolation extends WartTraverser {
def apply(u: WartUniverse): u.Traverser = {
import u.universe._

@tailrec
def checkArgumentsTypeIsPrimitive(args: List[Tree]): Boolean = {
args match {
case Nil => true

case h :: t =>
if (h.tpe <:< typeOf[String] || h.tpe <:< typeOf[Int] || h.tpe <:< typeOf[Double] ||
h.tpe <:< typeOf[Long] || h.tpe <:< typeOf[Boolean] || h.tpe <:< typeOf[Float])
checkArgumentsTypeIsPrimitive(t)
else
false
}
}

/**
* Collect arguments from nested `Apply(Select(_, TermName("$plus")), args)`
*
* @param tree AST
* @param acc Arguments accumulator
* @return None The input `tree` contains something
* that is NOT `Apply(Select(_, TermName("$plus")), args)`
* Some(args) The `tree` consists of only `Apply(Select(_, TermName("$plus")), args)` form
* and `args` is all arguments of the `tree`
*/
@tailrec
def collectArgsFormNestedApplySelectPlus(tree: Tree, acc: List[Tree]): Option[List[Tree]] =
tree match {
case Apply(Select(t @ Literal(_), TermName("$plus")), args) if t.tpe <:< typeOf[String] =>
Some(args ++ acc)

case Apply(Select(t, TermName("$plus")), args) =>
collectArgsFormNestedApplySelectPlus(t, args ++ acc)

case _ =>
None
}

val message = "This string interpolation contains non primitive value. Fix it."
new Traverser {
override def traverse(tree: Tree): Unit = {
tree match {
case t if hasWartAnnotation(u)(t) =>
case Apply(Select(Apply(Select(t, TermName("apply")), _), TermName("s")), args) if t.symbol.fullName == "scala.StringContext" && !checkArgumentsTypeIsPrimitive(args) =>
error(u)(tree.pos, message)

case Typed(t, TypeTree()) =>
/**
* From Scala 2.13, [[scala.tools.reflect.FastStringInterpolator]] is rewrite simple
* `StringContext.apply` tree into the strings appending form.
*/
collectArgsFormNestedApplySelectPlus(t, Nil) match {
case Some(args) if !checkArgumentsTypeIsPrimitive(args) =>
error(u)(tree.pos, message)
case _ =>
}
case _ =>
super.traverse(tree)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.wartremover
package contrib.warts

import org.scalatest.funsuite.AnyFunSuite
import org.wartremover.contrib.test.ResultAssertions
import org.wartremover.test.WartTestTraverser

class NonPrimitiveInStringInterpolationTest extends AnyFunSuite with ResultAssertions {
case class Dummy(
value: String)
val caseClass = Dummy("world")

test("""s"hello $str." is OK if the type of `str` is primitive""") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
val str = "world"
s"hello $str."
}
assert(result.errors.isEmpty)
}

test("""s"hello $number." is OK if the type of `number` is primitive""") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
val number = 1.0
s"hello $number."
}
assert(result.errors.isEmpty)
}

test("""s"hello $boolean." is OK if the type of `boolean` is primitive""") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
val boolean = false
s"hello $boolean."
}
assert(result.errors.isEmpty)
}

test("""s"hello $caseClass." is disable if the type of `number` is not primitive""") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
s"hello $caseClass."
}
assertError(result)("This string interpolation contains non primitive value. Fix it.")
}

test("""s"hello $caseClass.$caseClass." is disabled if `number` is repeated""") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
s"hello $caseClass.$caseClass"
}
assertError(result)("This string interpolation contains non primitive value. Fix it.")
}

test("`String.+` is OK even if the argument is not primitive") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
"world" + caseClass
}
assert(result.errors.isEmpty)
}

test("""s"hello ${Dummy("world")}" is disable""") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
s"hello ${Dummy("world")}."
}
assertError(result)("This string interpolation contains non primitive value. Fix it.")
}

test("""s"hello ${caseClass.value}" is OK if the type of `data.value` is primitive""") {
val result = WartTestTraverser(NonPrimitiveInStringInterpolation) {
s"hello ${caseClass.value}."
}
assert(result.errors.isEmpty)
}
}