Skip to content

Commit dee5e7e

Browse files
committed
Boolean format is null test for non-Boolean
1 parent bc6cf47 commit dee5e7e

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/FormatChecker.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
219219
def acceptableVariants: List[Type] =
220220
kind match
221221
case StringXn => if hasFlag('#') then FormattableType :: Nil else defn.AnyType :: Nil
222-
case BooleanXn => defn.BooleanType :: defn.NullType :: Nil
222+
case BooleanXn => defn.BooleanType :: defn.NullType :: defn.AnyType :: Nil // warn if not boolean
223223
case HashXn => defn.AnyType :: Nil
224224
case CharacterXn => defn.CharType :: defn.ByteType :: defn.ShortType :: defn.IntType :: Nil
225225
case IntegralXn => defn.IntType :: defn.LongType :: defn.ByteType :: defn.ShortType :: BigIntType :: Nil

tests/neg/f-interpolator-neg.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
7 | new StringContext("", "").f() // error
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
| too few arguments for interpolated string
17-
-- Error: tests/neg/f-interpolator-neg.scala:11:7 ----------------------------------------------------------------------
18-
11 | f"$s%b" // error
19-
| ^
20-
| Found: (s : String), Required: Boolean, Null
17+
-- Warning: tests/neg/f-interpolator-neg.scala:11:9 --------------------------------------------------------------------
18+
11 | f"$s%b" // warn only
19+
| ^
20+
| Boolean format is null test for non-Boolean
2121
-- Error: tests/neg/f-interpolator-neg.scala:12:7 ----------------------------------------------------------------------
2222
12 | f"$s%c" // error
2323
| ^

tests/neg/f-interpolator-neg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object Test {
88
}
99

1010
def interpolationMismatches(s : String, f : Double, b : Boolean) = {
11-
f"$s%b" // error
11+
f"$s%b" // warn only
1212
f"$s%c" // error
1313
f"$f%c" // error
1414
f"$s%x" // error
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//> using options -Wtostring-interpolated
2+
//> abusing options -Wconf:cat=w-flag-tostring-interpolated:e -Wtostring-interpolated
3+
4+
case class C(x: Int)
5+
6+
trait T {
7+
def c = C(42)
8+
def f = f"$c" // warn
9+
def s = s"$c" // warn
10+
def r = raw"$c" // warn
11+
12+
def format = f"${c.x}%d in $c or $c%s" // warn using c.toString // warn
13+
14+
def bool = f"$c%b" // warn just a null check
15+
16+
def oops = s"${null} slipped thru my fingers" // warn
17+
18+
def ok = s"${c.toString}"
19+
20+
def sb = new StringBuilder().append("hello")
21+
def greeting = s"$sb, world" // warn
22+
}
23+
24+
class Mitigations {
25+
26+
val s = "hello, world"
27+
val i = 42
28+
def shown = println("shown")
29+
30+
def ok = s"$s is ok"
31+
def jersey = s"number $i"
32+
def unitized = s"unfortunately $shown" // maybe tell them about unintended ()?
33+
34+
def nopct = f"$s is ok"
35+
def nofmt = f"number $i"
36+
}

0 commit comments

Comments
 (0)