From 275305a3d291cca49163903b5b6fe1d496b507a6 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sun, 20 Mar 2016 20:10:58 -0700 Subject: SI-9314 Ignore "${}" As an Easter egg, let "${} $x" forego the check on `x`. In other words, empty expression interpolation looks too degenerate to check. --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 8 +++++--- test/files/neg/t7848-interp-warn.scala | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 3f0d0e655d..1b55618691 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -105,7 +105,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper // that are turned private by typedBlock private final val SYNTHETIC_PRIVATE = TRANS_FLAG - private final val InterpolatorCodeRegex = """\$\{.*?\}""".r + private final val InterpolatorCodeRegex = """\$\{(.*?)\}""".r private final val InterpolatorIdentRegex = """\$[$\w]+""".r // note that \w doesn't include $ abstract class Typer(context0: Context) extends TyperDiagnostics with Adaptation with Tag with PatternTyper with TyperContextErrors { @@ -5211,11 +5211,13 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper def maybeWarn(s: String): Unit = { def warn(message: String) = context.warning(lit.pos, s"possible missing interpolator: $message") def suspiciousSym(name: TermName) = context.lookupSymbol(name, _ => true).symbol - def suspiciousExpr = InterpolatorCodeRegex findFirstIn s + val suspiciousExpr = InterpolatorCodeRegex findFirstMatchIn s def suspiciousIdents = InterpolatorIdentRegex findAllIn s map (s => suspiciousSym(TermName(s drop 1))) if (suspiciousExpr.nonEmpty) - warn("detected an interpolated expression") // "${...}" + suspiciousExpr filter (!_.group(1).trim.isEmpty) foreach (_ => + warn("detected an interpolated expression") // "${...}" + ) else suspiciousIdents find isPlausible foreach (sym => warn(s"detected interpolated identifier `$$${sym.name}`")) // "$id" } diff --git a/test/files/neg/t7848-interp-warn.scala b/test/files/neg/t7848-interp-warn.scala index 6a289b2b71..635dd48c27 100644 --- a/test/files/neg/t7848-interp-warn.scala +++ b/test/files/neg/t7848-interp-warn.scala @@ -25,4 +25,6 @@ object Test { def i = s"Try using '${ "$bar" }' instead." // was: no warn on space test def j = s"Try using '${ "something like $bar" }' instead." // warn def k = f"Try using '$bar' instead." // no warn on other std interps + def p = "Template ${} {}" // no warn on unlikely or empty expressions + def q = "${}$bar" // disables subsequent checks! } -- cgit v1.2.3