Skip to content

Commit 2a15075

Browse files
committed
Improve weird bool fmt message
1 parent 53df4a3 commit 2a15075

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import scala.annotation.tailrec
55
import scala.collection.mutable.ListBuffer
66
import scala.util.matching.Regex.Match
77

8-
import PartialFunction.cond
9-
108
import dotty.tools.dotc.ast.tpd.{Match => _, *}
119
import dotty.tools.dotc.core.Contexts.*
1210
import dotty.tools.dotc.core.Symbols.*
@@ -129,10 +127,8 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
129127
def intOf(g: SpecGroup): Option[Int] = group(g).map(_.toInt)
130128

131129
extension (inline value: Boolean)
132-
inline def or(inline body: => Unit): Boolean = value || { body ; false }
133-
inline def orElse(inline body: => Unit): Boolean = value || { body ; true }
134-
inline def and(inline body: => Unit): Boolean = value && { body ; true }
135-
inline def but(inline body: => Unit): Boolean = value && { body ; false }
130+
inline infix def or(inline body: => Unit): Boolean = value || { body; false }
131+
inline infix def and(inline body: => Unit): Boolean = value && { body; true }
136132

137133
enum Kind:
138134
case StringXn, HashXn, BooleanXn, CharacterXn, IntegralXn, FloatingPointXn, DateTimeXn, LiteralXn, ErrorXn
@@ -214,11 +210,18 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
214210
// is the specifier OK with the given arg
215211
def accepts(arg: Type): Boolean =
216212
kind match
217-
case BooleanXn => arg == defn.BooleanType orElse warningAt(CC)("Boolean format is null test for non-Boolean")
218-
case IntegralXn => arg == BigIntType || !cond(cc) {
219-
case 'o' | 'x' | 'X' if hasAnyFlag("+ (") => "+ (".filter(hasFlag).foreach(bad => badFlag(bad, s"only use '$bad' for BigInt conversions to o, x, X")); true
220-
}
213+
case BooleanXn if arg != defn.BooleanType =>
214+
warningAt(CC):
215+
"""non-Boolean value formats as "true" for non-null references and boxed primitives, otherwise "false""""
216+
true
217+
case IntegralXn if arg != BigIntType =>
218+
cc match
219+
case 'o' | 'x' | 'X' if hasAnyFlag("+ (") =>
220+
"+ (".filter(hasFlag).foreach: bad =>
221+
badFlag(bad, s"only use '$bad' for BigInt conversions to o, x, X")
222+
false
221223
case _ => true
224+
case _ => true
222225

223226
def lintToString(arg: Type): Unit =
224227
if ctx.settings.Whas.toStringInterpolated && kind == StringXn && !(arg.widen =:= defn.StringType) && !arg.isPrimitiveValueType

tests/neg/f-interpolator-neg.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
-- Warning: tests/neg/f-interpolator-neg.scala:11:9 --------------------------------------------------------------------
1818
11 | f"$s%b" // warn only
1919
| ^
20-
| Boolean format is null test for non-Boolean
20+
| non-Boolean value formats as "true" for non-null references and boxed primitives, otherwise "false"
2121
-- Error: tests/neg/f-interpolator-neg.scala:12:7 ----------------------------------------------------------------------
2222
12 | f"$s%c" // error
2323
| ^

0 commit comments

Comments
 (0)