Skip to content

Commit 9e80aa3

Browse files
authored
Merge pull request scala#10797 from som-snytt/issue/8761-missing-interp-dollar
Better split for missing-interpolator
2 parents 2c9b786 + 030255e commit 9e80aa3

File tree

8 files changed

+101
-102
lines changed

8 files changed

+101
-102
lines changed

src/compiler/scala/tools/nsc/ast/parser/Scanners.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ trait Scanners extends ScannersCommon {
15671567
case COMMA => "','"
15681568
case CASECLASS => "case class"
15691569
case CASEOBJECT => "case object"
1570-
case XMLSTART => "$XMLSTART$<"
1570+
case XMLSTART => s"$$XMLSTART$$<"
15711571
case _ =>
15721572
(token2name get token) match {
15731573
case Some(name) => "'" + name + "'"

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
116116
private final val SYNTHETIC_PRIVATE = TRANS_FLAG
117117

118118
private final val InterpolatorCodeRegex = """\$\{\s*(.*?)\s*\}""".r
119-
private final val InterpolatorIdentRegex = """\$[$\w]+""".r // note that \w doesn't include $
119+
private final val InterpolatorIdentRegex = """\$[\w]+""".r // note that \w doesn't include $
120120

121121
/** Check that type of given tree does not contain local or private
122122
* components.
@@ -6101,8 +6101,11 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
61016101
// short-circuit on leading ${}
61026102
if (!exprs.head.isEmpty && exprs.exists(warnableExpr))
61036103
warn("detected an interpolated expression") // "${...}"
6104-
} else
6105-
suspiciousIdents.find(id => isPlausible(id.substring(1))).foreach(id => warn(s"detected interpolated identifier `$id`"))
6104+
} else suspiciousIdents.toList match {
6105+
case Nil =>
6106+
case id :: Nil => if (isPlausible(id.substring(1))) warn(s"detected interpolated identifier `$id`")
6107+
case all => if (all.forall(id => isPlausible(id.substring(1)))) warn(all.mkString("detected interpolated identifiers `", "`, `", "`"))
6108+
}
61066109
}
61076110
lit match {
61086111
case Literal(Constant(s: String)) if mightBeMissingInterpolation => maybeWarn(s)

test/files/neg/forgot-interpolator.check

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
forgot-interpolator.scala:6: warning: possible missing interpolator: detected interpolated identifier `$bippy`
1+
forgot-interpolator.scala:6: warning: possible missing interpolator: detected interpolated identifiers `$bippy`, `$bippy`
22
def f = "Put the $bippy in the $bippy!" // warn 1
33
^
44
forgot-interpolator.scala:16: warning: possible missing interpolator: detected an interpolated expression
@@ -25,6 +25,48 @@ forgot-interpolator.scala:92: warning: possible missing interpolator: detected i
2525
forgot-interpolator.scala:126: warning: possible missing interpolator: detected an interpolated expression
2626
@deprecated("${myProperty}")
2727
^
28+
forgot-interpolator.scala:144: warning: possible missing interpolator: detected interpolated identifier `$foo`
29+
"An important $foo message!" // warn on ident in scope
30+
^
31+
forgot-interpolator.scala:148: warning: possible missing interpolator: detected an interpolated expression
32+
"A doubly important ${foo * 2} message!" // warn on some expr, see below
33+
^
34+
forgot-interpolator.scala:151: warning: possible missing interpolator: detected interpolated identifier `$bar`
35+
def i = s"Try using '${ "$bar" }' instead." // was: no warn on space test
36+
^
37+
forgot-interpolator.scala:152: warning: possible missing interpolator: detected interpolated identifier `$bar`
38+
def j = s"Try using '${ "something like $bar" }' instead." // warn
39+
^
40+
forgot-interpolator.scala:158: warning: possible missing interpolator: detected an interpolated expression
41+
def v = "${baz}${bar}" // warn on second expr
42+
^
43+
forgot-interpolator.scala:159: warning: possible missing interpolator: detected an interpolated expression
44+
def w = "${ op_* }" // warn, only cheap ident parsing
45+
^
46+
forgot-interpolator.scala:160: warning: possible missing interpolator: detected an interpolated expression
47+
def x = "${ bar }" // warn, a cheap ident in scope
48+
^
49+
forgot-interpolator.scala:162: warning: possible missing interpolator: detected an interpolated expression
50+
def z = "${ baz * 3}" // warn, no expr parsing
51+
^
52+
forgot-interpolator.scala:164: warning: possible missing interpolator: detected interpolated identifier `$this`
53+
def thisly = "$this"
54+
^
55+
forgot-interpolator.scala:165: warning: possible missing interpolator: detected an interpolated expression
56+
def exprly = "${this}"
57+
^
58+
forgot-interpolator.scala:170: warning: possible missing interpolator: detected interpolated identifier `$s`
59+
val t = "$s"
60+
^
61+
forgot-interpolator.scala:171: warning: possible missing interpolator: detected an interpolated expression
62+
val u = "a${s}b"
63+
^
64+
forgot-interpolator.scala:172: warning: possible missing interpolator: detected interpolated identifier `$s`
65+
val v = "a$s b"
66+
^
67+
forgot-interpolator.scala:177: warning: possible missing interpolator: detected interpolated identifiers `$foo`, `$bar`
68+
def s = "$foo$bar"
69+
^
2870
error: No warnings can be incurred under -Werror.
29-
9 warnings
71+
23 warnings
3072
1 error

test/files/neg/forgot-interpolator.scala

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,53 @@ object t10456 {
126126
@deprecated("${myProperty}")
127127
var myProperty: String = _
128128
}
129+
130+
package pancake { }
131+
132+
object Tester {
133+
type NonVal = Int
134+
135+
def ok = "Don't warn on $nosymbol interpolated."
136+
137+
def pass = "Don't warn on $pancake package names."
138+
139+
def types = "Or $NonVal type symbols either."
140+
141+
def bar = "bar"
142+
def f = {
143+
val foo = "bar"
144+
"An important $foo message!" // warn on ident in scope
145+
}
146+
def g = {
147+
val foo = "bar"
148+
"A doubly important ${foo * 2} message!" // warn on some expr, see below
149+
}
150+
def h = s"Try using '$$bar' instead." // no warn
151+
def i = s"Try using '${ "$bar" }' instead." // was: no warn on space test
152+
def j = s"Try using '${ "something like $bar" }' instead." // warn
153+
def k = f"Try using '$bar' instead." // no warn on other std interps
154+
def p = "Template ${} {}" // no warn on unlikely or empty expressions
155+
def q = "${}$bar" // disables subsequent checks! (a feature)
156+
def r = "${}${bar}" // disables subsequent checks! (a feature)
157+
158+
def v = "${baz}${bar}" // warn on second expr
159+
def w = "${ op_* }" // warn, only cheap ident parsing
160+
def x = "${ bar }" // warn, a cheap ident in scope
161+
def y = "${ baz }" // no warn, cheap ident not in scope
162+
def z = "${ baz * 3}" // warn, no expr parsing
163+
164+
def thisly = "$this"
165+
def exprly = "${this}"
166+
}
167+
168+
trait X {
169+
val s = "hello"
170+
val t = "$s"
171+
val u = "a${s}b"
172+
val v = "a$s b"
173+
}
174+
175+
trait DollarDollar {
176+
val foo, bar = 42
177+
def s = "$foo$bar"
178+
}

test/files/neg/t7848-interp-warn.check

Lines changed: 0 additions & 33 deletions
This file was deleted.

test/files/neg/t7848-interp-warn.scala

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/files/neg/t9127.check

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/files/neg/t9127.scala

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)