Skip to content

Commit 5091e59

Browse files
authored
Merge pull request #532 from scala/backport-lts-3.3-23365
Backport "Check OrType in interpolated toString lint" to 3.3 LTS
2 parents 8e04775 + 0d64b0a commit 5091e59

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,20 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
228228
case _ => true
229229

230230
def lintToString(arg: Type): Unit =
231-
if ctx.settings.Whas.toStringInterpolated && kind == StringXn && !(arg.widen =:= defn.StringType) && !arg.isPrimitiveValueType
232-
then warningAt(CC)("interpolation uses toString")
231+
def checkIsStringify(tp: Type): Boolean = tp.widen match
232+
case OrType(tp1, tp2) =>
233+
checkIsStringify(tp1) || checkIsStringify(tp2)
234+
case tp =>
235+
!(tp =:= defn.StringType)
236+
&& {
237+
tp =:= defn.UnitType
238+
&& { warningAt(CC)("interpolated Unit value"); true }
239+
||
240+
!tp.isPrimitiveValueType
241+
&& { warningAt(CC)("interpolation uses toString"); true }
242+
}
243+
if ctx.settings.Whas.toStringInterpolated && kind == StringXn then
244+
checkIsStringify(arg): Unit
233245

234246
// what arg type if any does the conversion accept
235247
def acceptableVariants: List[Type] =

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,20 @@ class StringInterpolatorOpt extends MiniPhase:
109109
result
110110
end mkConcat
111111
def lintToString(t: Tree): Unit =
112-
val arg: Type = t.tpe
113-
if ctx.settings.Whas.toStringInterpolated && !(arg.widen =:= defn.StringType) && !arg.isPrimitiveValueType
114-
then report.warning("interpolation uses toString", t.srcPos)
112+
def checkIsStringify(tp: Type): Boolean = tp.widen match
113+
case OrType(tp1, tp2) =>
114+
checkIsStringify(tp1) || checkIsStringify(tp2)
115+
case tp =>
116+
!(tp =:= defn.StringType)
117+
&& {
118+
tp =:= defn.UnitType
119+
&& { report.warning("interpolated Unit value", t.srcPos); true }
120+
||
121+
!tp.isPrimitiveValueType
122+
&& { report.warning("interpolation uses toString", t.srcPos); true }
123+
}
124+
if ctx.settings.Whas.toStringInterpolated then
125+
checkIsStringify(t.tpe): Unit
115126
val sym = tree.symbol
116127
// Test names first to avoid loading scala.StringContext if not used, and common names first
117128
val isInterpolatedMethod =

0 commit comments

Comments
 (0)