Skip to content
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

Warnings from -Wvalue-discard silenced with @nowarn give an error with -Wfatal-warnings #17735

Closed
mleclercq opened this issue May 31, 2023 · 0 comments · Fixed by #18089
Closed
Assignees
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug

Comments

@mleclercq
Copy link

Compiler version

3.3.0 with compiler options -Wvalue-discard and -Xfatal-warnings

Minimized code

import scala.collection.mutable
import scala.annotation.nowarn

object Foo:

  def f(b: Boolean): String =
    val messageBuilder = mutable.StringBuilder()
    if b then
      // Here @nowarn is effective with or without -Wfatal-warnings
      // i.e. no warning without -Wfatal-warnings and no error with -Wfatal-warnings
      messageBuilder.append("helloworld").append("\n"): @nowarn("msg=discarded non-Unit value*")

    messageBuilder.result()

  def g(x: String => Unit) = ???
  def h: String =
    val messageBuilder = mutable.StringBuilder()
    g: s => 
      // here @nowarn is effective without -Wfatal-warnings (i.e. no warning)
      // But with -Wfatal-warnings we get an error
      messageBuilder.append("\n").append(s): @nowarn("msg=discarded non-Unit value*")
    messageBuilder.result()

Scastie: https://scastie.scala-lang.org/yHZ6xz51SsKqKpHaXaPJQQ

Output

[error] -- [E175] Potential Issue Error: /Users/matthieu/reco-engine-v3/foo/src/main/scala/Foo.scala:20:40 
[error] 21 |      messageBuilder.append("\n").append(s): @nowarn("msg=discarded non-Unit value*")
[error]    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |      discarded non-Unit value of type (messageBuilder : StringBuilder)
[error] one error found```

Expectation

When a warning is silenced with a @nowarn annotation, it should not raise an error when -Wfatal-warnings flag is used

Notes

I noticed that code like:

    if b then
      messageBuilder.append("helloworld")

Do not give a warning, while:

    if b then
      messageBuilder.append("helloworld").append("\n")

does give a warning.

Not sure if that's expected.

@mleclercq mleclercq added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 31, 2023
@jchyb jchyb added area:reporting Error reporting including formatting, implicit suggestions, etc and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 31, 2023
@KacperFKorban KacperFKorban self-assigned this Jun 28, 2023
KacperFKorban referenced this issue in dotty-staging/dotty Jun 28, 2023
…ng it into an error

closes lampepfl#17741
closes lampepfl#17735
KacperFKorban referenced this issue in dotty-staging/dotty Jun 29, 2023
…ng it into an error

closes lampepfl#17741
closes lampepfl#17735
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug
Projects
None yet
3 participants