Skip to content

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

Closed
@mleclercq

Description

@mleclercq

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.

Metadata

Metadata

Assignees

Labels

area:reportingError reporting including formatting, implicit suggestions, etcitype:bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions