Skip to content

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

Open
@johnduffell

Description

@johnduffell
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

Activity

self-assigned this
on Jun 13, 2025
som-snytt

som-snytt commented on Jun 13, 2025

@som-snytt
Contributor

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.

linked a pull request that will close this issue on Jun 14, 2025
added
area:lintingLinting warnings enabled with -W or -Xlint
and removed
stat:needs triageEvery issue needs to have an "area" and "itype" label
on Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:lintingLinting warnings enabled with -W or -Xlintitype:bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @som-snytt@johnduffell@Gedochao

    Issue actions

      -Wtostring-interpolated warns on an if expression of type String · Issue #23361 · scala/scala3