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

Missing Unused Expression Warning #11998

Closed
jamesward opened this issue May 14, 2020 · 5 comments
Closed

Missing Unused Expression Warning #11998

jamesward opened this issue May 14, 2020 · 5 comments

Comments

@jamesward
Copy link

reproduction steps

using Scala 2.13.2,

val isAThing = {
  "blah" == "asdf" &&
  "foo" == "bar"
  "baz" == "baz"
}

With these compiler options:

  "-unchecked",
  "-deprecation",
  "-explaintypes",
  "-feature",
  "-Wconf:any:error",
  "-Wdead-code",
  "-Wunused",
  "-Wvalue-discard",

problem

The compiler does not catch that the first two expressions are unused.

@som-snytt
Copy link

This isn't under a flag. It doesn't do anything with Object#equals.

scala> def f = { 1 + 1 ; 2 }
                   ^
       warning: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
def f: Int

scala> def f = { 1 == 1 ; 2 }
                   ^
       warning: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
def f: Int

scala> def f = { "" == "" ; 2 }
def f: Int

Here, "pure" means "looks like a constant", IIRC. It could special case equals for String, or string equality for constants could be constant-folded, etc.

The heuristic could be improved, and maybe add a strict option that would be more like detecting value discard.

-Wunused only means a symbol that was introduced but not referenced. (For imports, a selector was never used to look up a symbol.)

@SethTisue
Copy link
Member

shouldn't -Wvalue-discard be catching it though?

@som-snytt
Copy link

-Wvalue-discard warns that value discard conversion was applied. That's not in the middle of a block, in "statement position", but only at the end, in "result position", when Unit is expected.

The heuristic could be improved, and maybe add a strict option that would be more like detecting value discard.

@SethTisue
Copy link
Member

PR scala/scala#9893

@som-snytt
Copy link

-Wnonunit-statement in the linked PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants