Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc21f58

Browse files
committed
Allow interpolating primitive toString
1 parent cd15f58 commit dc21f58

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

src/compiler/scala/tools/reflect/FastStringInterpolator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ trait FastStringInterpolator extends FormatInterpolator {
102102
val emptyLit = treatedContents.isEmpty
103103
if (i < numLits - 1) {
104104
val arg = argsIndexed(i)
105-
if (linting && !(arg.tpe =:= definitions.StringTpe))
105+
if (linting && !(arg.tpe =:= definitions.StringTpe) && !definitions.isPrimitiveValueType(arg.tpe))
106106
runReporting.warning(arg.pos, "interpolation uses toString", WFlagTostringInterpolated, c.internal.enclosingOwner)
107107
concatArgs += arg
108108
}

src/compiler/scala/tools/reflect/FormatInterpolator.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ abstract class FormatInterpolator {
143143
val cv = Conversion(part0.pos, argc)
144144
cv.accepts(argType(n-1, AnyTpe))
145145
convert += cv
146+
cv.lintToString(argTypes(n-1))
146147
}
147148
def errorLeading(op: Conversion) = op.errorAt(Spec)(s"conversions must follow a splice; ${Conversion.literalHelp}")
148149
def accept(op: Conversion): Unit = {
149150
if (!op.isLeading) errorLeading(op)
150151
op.accepts(argType(n-1, op.acceptableVariants: _*))
151152
amended += part
153+
op.lintToString(argTypes(n-1))
152154
}
153155

154156
if (n == 0) amended += part
@@ -309,9 +311,11 @@ abstract class FormatInterpolator {
309311
arg == BigIntTpe || !cond(cc) {
310312
case 'o' | 'x' | 'X' if hasAnyFlag("+ (") => "+ (".filter(hasFlag).foreach(bad => badFlag(bad, s"only use '$bad' for BigInt conversions to o, x, X")) ; true
311313
}
312-
case StringXn => arg == StringTpe orElse (if (linting) warningAt(CC)("interpolation uses toString", WFlagTostringInterpolated))
313314
case _ => true
314315
}
316+
def lintToString(arg: Type): Unit =
317+
if (linting && kind == StringXn && !(arg =:= StringTpe) && !isPrimitiveValueType(arg))
318+
warningAt(CC)("interpolation uses toString", WFlagTostringInterpolated)
315319

316320
// what arg type if any does the conversion accept
317321
def acceptableVariants: List[Type] =
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
tostring-interpolated.scala:7: error: interpolation uses toString
22
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=w-flag-tostring-interpolated, site=T.f
3-
def f = f"$c"
3+
def f = f"$c" // warn
44
^
55
tostring-interpolated.scala:8: error: interpolation uses toString
66
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=w-flag-tostring-interpolated, site=T.s
7-
def s = s"$c"
7+
def s = s"$c" // warn
88
^
99
tostring-interpolated.scala:9: error: interpolation uses toString
1010
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=w-flag-tostring-interpolated, site=T.r
11-
def r = raw"$c"
11+
def r = raw"$c" // warn
1212
^
1313
tostring-interpolated.scala:11: error: interpolation uses toString
1414
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=w-flag-tostring-interpolated, site=T.format
15-
def format = f"${c.x}%d in $c or $c%s" // warn using c.toString
15+
def format = f"${c.x}%d in $c or $c%s" // warn using c.toString // warn
1616
^
1717
tostring-interpolated.scala:11: error: interpolation uses toString
1818
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=w-flag-tostring-interpolated, site=T.format
19-
def format = f"${c.x}%d in $c or $c%s" // warn using c.toString
19+
def format = f"${c.x}%d in $c or $c%s" // warn using c.toString // warn
2020
^
2121
tostring-interpolated.scala:13: warning: Boolean format is null test for non-Boolean
2222
def bool = f"$c%b" // warn just a null check
2323
^
2424
tostring-interpolated.scala:15: error: interpolation uses toString
2525
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=w-flag-tostring-interpolated, site=T.oops
26-
def oops = s"${null} slipped thru my fingers"
26+
def oops = s"${null} slipped thru my fingers" // warn
2727
^
2828
tostring-interpolated.scala:20: error: interpolation uses toString
2929
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=w-flag-tostring-interpolated, site=T.greeting
30-
def greeting = s"$sb, world"
30+
def greeting = s"$sb, world" // warn
3131
^
3232
1 warning
3333
7 errors

test/files/neg/tostring-interpolated.scala

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,32 @@ case class C(x: Int)
44

55
trait T {
66
def c = C(42)
7-
def f = f"$c"
8-
def s = s"$c"
9-
def r = raw"$c"
7+
def f = f"$c" // warn
8+
def s = s"$c" // warn
9+
def r = raw"$c" // warn
1010

11-
def format = f"${c.x}%d in $c or $c%s" // warn using c.toString
11+
def format = f"${c.x}%d in $c or $c%s" // warn using c.toString // warn
1212

1313
def bool = f"$c%b" // warn just a null check
1414

15-
def oops = s"${null} slipped thru my fingers"
15+
def oops = s"${null} slipped thru my fingers" // warn
1616

1717
def ok = s"${c.toString}"
1818

1919
def sb = new StringBuilder().append("hello")
20-
def greeting = s"$sb, world"
20+
def greeting = s"$sb, world" // warn
21+
}
22+
23+
class Mitigations {
24+
25+
val s = "hello, world"
26+
val i = 42
27+
def shown() = println("shown")
28+
29+
def ok = s"$s is ok"
30+
def jersey = s"number $i"
31+
def unitized = s"unfortunately $shown" // maybe tell them about unintended ()?
32+
33+
def nopct = f"$s is ok"
34+
def nofmt = f"number $i"
2135
}

0 commit comments

Comments
 (0)