Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

som-snytt
Copy link
Contributor

Fixes scala/bug#13107

Since interpolated expressions have an expected type Any, if and match expressions are Any-typed.

Before warning about them, check whether the "arms" or "branches" ("else" or "case") conform to String.

@scala-jenkins scala-jenkins added this to the 2.13.17 milestone Jun 13, 2025
@som-snytt
Copy link
Contributor Author

Position of diagnostic for f needs tweak? Currently it's at the CC conversion char, or where the CC would fall.

if (linting && !(arg.tpe =:= definitions.StringTpe))
def warn(msg: String) = runReporting.warning(arg.pos, msg, WFlagTostringInterpolated, c.internal.enclosingOwner)
def stringlyBranches = arg match {
case If(_, thenp, elsep) => thenp.tpe <:< definitions.StringTpe && elsep.tpe <:< definitions.StringTpe
Copy link
Member

@lrytz lrytz Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-unit primitives should be allowed

scala> def f(b: Boolean) = s"b: ${if (b) 1 else 0}"
                                  ^
       warning: interpolation uses toString
def f(b: Boolean): String

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also nesting..

scala> def f(x: Any) = s"r: ${ x match { case s: String => s; case _ => if (x == null) "n" else "s" } }"
                                 ^
       warning: interpolation uses toString

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I guess!

@SethTisue SethTisue marked this pull request as draft August 16, 2025 08:49
@lrytz lrytz modified the milestones: 2.13.17, 2.13.18 Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

-Wtostring-interpolated warns on an if expression of type String
3 participants