Skip to content

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

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 scala/scala#11074
Open

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

johnduffell opened this issue Jun 13, 2025 · 1 comment · May be fixed by scala/scala#11074
Assignees

Comments

@johnduffell
Copy link

Reproduction steps

2.13.16
With scalacOptions including

    "-Wtostring-interpolated",

and optionally (to make it an error)

    "-Wconf:cat=w-flag-tostring-interpolated:e",

compile this code:

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

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

Output

there is a warning/error 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 3.7.1-RC1
Feature introduced in scala/scala#10776

@som-snytt
Copy link

Funnily, the type of the expression is Any, probably because the expected type for interpolated exprs is Any, and if/else uses the expected type (there's no need to lub, given that the branches conform to the expected type).

It wouldn't be terrible if the lint must check arms of an if, or maybe also cases of a match; just a bit ugly.

@som-snytt som-snytt self-assigned this Jun 13, 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 a pull request may close this issue.

2 participants