Skip to content

Commit 3140216

Browse files
committed
-Wtostring-interpolated for f
1 parent e8522dd commit 3140216

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ private sealed trait WarningSettings:
167167
private val WenumCommentDiscard = BooleanSetting(WarningSetting, "Wenum-comment-discard", "Warn when a comment ambiguously assigned to multiple enum cases is discarded.")
168168
private val WimplausiblePatterns = BooleanSetting(WarningSetting, "Wimplausible-patterns", "Warn if comparison with a pattern value looks like it might always fail.")
169169
private val WunstableInlineAccessors = BooleanSetting(WarningSetting, "WunstableInlineAccessors", "Warn an inline methods has references to non-stable binary APIs.")
170+
private val WtoStringInterpolated = BooleanSetting(WarningSetting, "Wtostring-interpolated", "Warn a standard interpolator used toString on a reference type.")
170171
private val Wunused: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(
171172
WarningSetting,
172173
name = "Wunused",

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
6464

6565
/** For N part strings and N-1 args to interpolate, normalize parts and check arg types.
6666
*
67-
* Returns normalized part strings and args, where args correcpond to conversions in tail of parts.
67+
* Returns normalized part strings and args, where args correspond to conversions in tail of parts.
6868
*/
6969
def checked: (List[String], List[Tree]) =
7070
val amended = ListBuffer.empty[String]
@@ -83,12 +83,15 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
8383
val cv = Conversion(n)
8484
cv.accepts(argType(n-1, defn.AnyType))
8585
convert += cv
86+
cv.lintToString(argTypes(n-1))
87+
8688
def errorLeading(op: Conversion) = op.errorAt(Spec)(s"conversions must follow a splice; ${Conversion.literalHelp}")
8789
def accept(op: Conversion): Unit =
8890
if !op.isLeading then errorLeading(op)
8991
op.accepts(argType(n-1, op.acceptableVariants*))
9092
amended += part
9193
convert += op
94+
op.lintToString(argTypes(n-1))
9295

9396
// after the first part, a leading specifier is required for the interpolated arg; %s is supplied if needed
9497
if n == 0 then amended += part
@@ -217,6 +220,10 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
217220
}
218221
case _ => true
219222

223+
def lintToString(arg: Type): Unit =
224+
if ctx.settings.WtoStringInterpolated.value && kind == StringXn && !(arg =:= defn.StringType) && !isPrimitiveValueType(arg)
225+
then warningAt(CC)("interpolation uses toString")
226+
220227
// what arg type if any does the conversion accept
221228
def acceptableVariants: List[Type] =
222229
kind match

0 commit comments

Comments
 (0)