Skip to content

Backport "Check OrType in interpolated toString lint" to 3.3 LTS #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,20 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
case _ => true

def lintToString(arg: Type): Unit =
if ctx.settings.Whas.toStringInterpolated && kind == StringXn && !(arg.widen =:= defn.StringType) && !arg.isPrimitiveValueType
then warningAt(CC)("interpolation uses toString")
def checkIsStringify(tp: Type): Boolean = tp.widen match
case OrType(tp1, tp2) =>
checkIsStringify(tp1) || checkIsStringify(tp2)
case tp =>
!(tp =:= defn.StringType)
&& {
tp =:= defn.UnitType
&& { warningAt(CC)("interpolated Unit value"); true }
||
!tp.isPrimitiveValueType
&& { warningAt(CC)("interpolation uses toString"); true }
}
if ctx.settings.Whas.toStringInterpolated && kind == StringXn then
checkIsStringify(arg): Unit

// what arg type if any does the conversion accept
def acceptableVariants: List[Type] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,20 @@ class StringInterpolatorOpt extends MiniPhase:
result
end mkConcat
def lintToString(t: Tree): Unit =
val arg: Type = t.tpe
if ctx.settings.Whas.toStringInterpolated && !(arg.widen =:= defn.StringType) && !arg.isPrimitiveValueType
then report.warning("interpolation uses toString", t.srcPos)
def checkIsStringify(tp: Type): Boolean = tp.widen match
case OrType(tp1, tp2) =>
checkIsStringify(tp1) || checkIsStringify(tp2)
case tp =>
!(tp =:= defn.StringType)
&& {
tp =:= defn.UnitType
&& { report.warning("interpolated Unit value", t.srcPos); true }
||
!tp.isPrimitiveValueType
&& { report.warning("interpolation uses toString", t.srcPos); true }
}
if ctx.settings.Whas.toStringInterpolated then
checkIsStringify(t.tpe): Unit
val sym = tree.symbol
// Test names first to avoid loading scala.StringContext if not used, and common names first
val isInterpolatedMethod =
Expand Down