summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Typers.scala20
-rw-r--r--test/files/neg/forgot-interpolator.check4
-rw-r--r--test/files/neg/t7848-interp-warn.check12
-rw-r--r--test/files/neg/t7848-interp-warn.flags1
-rw-r--r--test/files/neg/t7848-interp-warn.scala13
5 files changed, 41 insertions, 9 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index 3ac0b398ee..80e9e16178 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -4878,17 +4878,23 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
}
tree.value match {
case Constant(s: String) =>
- def names = InterpolatorIdentRegex findAllIn s map (n => newTermName(n stripPrefix "$"))
- def suspicious = (
- (InterpolatorCodeRegex findFirstIn s).nonEmpty
- || (names exists (n => context.lookupSymbol(n, _ => true).symbol.exists))
- )
+ def names = InterpolatorIdentRegex findAllIn s map (n => TermName(n stripPrefix "$"))
+ def isSuspiciousExpr = (InterpolatorCodeRegex findFirstIn s).nonEmpty
+ //def isSuspiciousName = names exists symExists
+ def suspiciousName = names find symExists
+ def symExists(n: TermName) = context.lookupSymbol(n, _ => true).symbol.exists
val noWarn = (
isArgToImplicitNotFound
|| !(s contains ' ') // another heuristic - e.g. a string with only "$asInstanceOf"
)
- if (!noWarn && suspicious)
- unit.warning(tree.pos, "looks like an interpolated String; did you forget the interpolator?")
+ if (!noWarn) {
+ val suggest = "Did you forget the interpolator?"
+ if (isSuspiciousExpr)
+ unit.warning(tree.pos, s"That looks like an interpolated expression! $suggest")
+ else /* if (isSuspiciousName) */ suspiciousName foreach (n =>
+ unit.warning(tree.pos, s"`$$$n` looks like an interpolated identifier! $suggest")
+ )
+ }
case _ =>
}
}
diff --git a/test/files/neg/forgot-interpolator.check b/test/files/neg/forgot-interpolator.check
index f6de4d7b3a..1aad3b0003 100644
--- a/test/files/neg/forgot-interpolator.check
+++ b/test/files/neg/forgot-interpolator.check
@@ -1,7 +1,7 @@
-forgot-interpolator.scala:4: warning: looks like an interpolated String; did you forget the interpolator?
+forgot-interpolator.scala:4: warning: `$bippy` looks like an interpolated identifier! Did you forget the interpolator?
def f = "Put the $bippy in the $bippy!" // warn
^
-forgot-interpolator.scala:14: warning: looks like an interpolated String; did you forget the interpolator?
+forgot-interpolator.scala:14: warning: That looks like an interpolated expression! Did you forget the interpolator?
def f = """Put the ${println("bippy")} in the bippy!""" // warn
^
error: No warnings can be incurred under -Xfatal-warnings.
diff --git a/test/files/neg/t7848-interp-warn.check b/test/files/neg/t7848-interp-warn.check
new file mode 100644
index 0000000000..b2a8f59a63
--- /dev/null
+++ b/test/files/neg/t7848-interp-warn.check
@@ -0,0 +1,12 @@
+t7848-interp-warn.scala:7: warning: `$foo` looks like an interpolated identifier! Did you forget the interpolator?
+ "An important $foo message!"
+ ^
+t7848-interp-warn.scala:11: warning: That looks like an interpolated expression! Did you forget the interpolator?
+ "A doubly important ${foo * 2} message!"
+ ^
+t7848-interp-warn.scala:9: warning: That looks like an interpolated expression! Did you forget the interpolator?
+ def g = {
+ ^
+error: No warnings can be incurred under -Xfatal-warnings.
+three warnings found
+one error found
diff --git a/test/files/neg/t7848-interp-warn.flags b/test/files/neg/t7848-interp-warn.flags
new file mode 100644
index 0000000000..7949c2afa2
--- /dev/null
+++ b/test/files/neg/t7848-interp-warn.flags
@@ -0,0 +1 @@
+-Xlint -Xfatal-warnings
diff --git a/test/files/neg/t7848-interp-warn.scala b/test/files/neg/t7848-interp-warn.scala
new file mode 100644
index 0000000000..bb3eeff60c
--- /dev/null
+++ b/test/files/neg/t7848-interp-warn.scala
@@ -0,0 +1,13 @@
+
+package test
+
+object Test {
+ def f = {
+ val foo = "bar"
+ "An important $foo message!"
+ }
+ def g = {
+ val foo = "bar"
+ "A doubly important ${foo * 2} message!"
+ }
+}