Skip to content

-Wtostring-interpolated warns on an if expression of type String #23361

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

Open
johnduffell opened this issue Jun 13, 2025 · 1 comment · May be fixed by #23365
Open

-Wtostring-interpolated warns on an if expression of type String #23361

johnduffell opened this issue Jun 13, 2025 · 1 comment · May be fixed by #23365
Assignees
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@johnduffell
Copy link
Contributor

Compiler version

3.7.1-RC1

Minimized code

With scalacOptions including

    "-Wtostring-interpolated",

compile this code:

val shouldCaps = true
val greeting = s"Hello ${if (shouldCaps) "WORLD" else "world"}"
println(greeting)

see https://scastie.scala-lang.org/Y9lVvpVjRUeL7P08bs8Heg

Output

there is a warning raised on line 2:
"interpolation uses toString"

Expectation

compiles without warnings, as the type of the expression is a String

Additional info

Also fails on 2.13.16
Feature introduced in #20578

@johnduffell johnduffell added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 13, 2025
@som-snytt som-snytt self-assigned this Jun 13, 2025
@som-snytt
Copy link
Contributor

som-snytt commented Jun 13, 2025

Thanks for the reports. As mentioned on the Scala 2 ticket, the type of the expression is Any (because if/else takes the expected type if there is one), but maybe expressions with arms can be special-cased. Edit: the tree type is a union of the arms.

@som-snytt som-snytt linked a pull request Jun 14, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants