Skip to content

No unused value warning inside of if statement #22214

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

Closed
OndrejSpanel opened this issue Dec 15, 2024 · 3 comments
Closed

No unused value warning inside of if statement #22214

OndrejSpanel opened this issue Dec 15, 2024 · 3 comments
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:enhancement stat:wontfix

Comments

@OndrejSpanel
Copy link
Member

Compiler version

3.5.2, 3.6.2

Minimized code

object Main {
  def fun(): String = ""
  def main(args: Array[String]): Unit = {
    fun() // warning here
    if (true) {
      fun() // no warning
    }
  }
}

Compile with -Wnonunit-statement

Output

C:\Dev\Sandbox\src\main\scala\Main.scala:4:8
unused value of type String
    fun()

Expectation

The warning should be given both on lines 4 and 6, but the discarded value on line 6 is silently ignored.

@OndrejSpanel OndrejSpanel added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 15, 2024
@som-snytt
Copy link
Contributor

I thought it was the other way around.

#20450

Oh that issue says -Wvalue-discard does warn at the line in question.

The two warnings are non "synonymous" because "value discard" is a discarded "expression", but "nonunit statement" checks for an expression in a syntactic statement.

In Scala 2, if (b) e is typechecked as if (b) e else (), which is a value that warns under "nonunit statement".

In Scala 3, e has expected type Unit, so it warns under value discard.

That much is "won't fix", but maybe the desired behavior is that the "nonunit statement" warning also enables "value discard", as it is the more expensive check and the user surely wants both.

@som-snytt som-snytt added itype:enhancement and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 16, 2024
@OndrejSpanel
Copy link
Member Author

Thanks for the explanation. Truth is I was not aware of -Wvalue-discard existence, I did not pay much attention to documentation, I just saw -Wnonunit-statement and though this is what I want.

@Gedochao Gedochao added area:linting Linting warnings enabled with -W or -Xlint stat:wontfix labels Dec 16, 2024
@Gedochao
Copy link
Contributor

That much is "won't fix", but maybe the desired behavior is that the "nonunit statement" warning also enables "value discard", as it is the more expensive check and the user surely wants both.

Agreed, closing this as a won't fix.
We might want to consider unifying -Wvalue-discard with -Wnonunit-statement, but that's a separate thing.
Feel free to raise it as a separate issue.

@Gedochao Gedochao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:enhancement stat:wontfix
Projects
None yet
Development

No branches or pull requests

3 participants