Skip to content

Commit 12673fa

Browse files
committed
-Wtostring-interpolated for f
1 parent 719be84 commit 12673fa

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

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

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
6565

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

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

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

0 commit comments

Comments
 (0)