-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Value discard warning in one-limbed if #20450
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
Comments
note: the same behaviour can be reproduced outside of REPL. //> using options -Wnonunit-statement -Wvalue-discard
import scala.util.chaining.given
@main def main = {
val it = Iterator.continually(42)
it.hasNext.tap(if (_) it.next())
} |
Scala 2 also still gives a warning in this case, right? |
@nox213 why would you say that.
|
Thanks for double checking, I was confused |
In Scala 2, it warns for the code above when compiled with the -Wvalue-discard flag. Is this a different case from the example you provided? |
@nox213 yes, the expected type matters. From the previous example, compare:
In Scala 3, In Scala 2, |
Thanks for the explanation! |
I don't think this is a good reason to skip the warning in this case. The method The one-limbed if behaves in the exact same way – discards the output of its content by design – so it should also produce the exact same warning. FWIW, I have a use case in Laminar that relies on this warning for safety. The documentation was written pre-Scala-3.3.0 where Wvalue-discard was introduced, and explains the issue in Laminar terms, but here is the same issue minimized: def foo(sideEffect: => Unit): Unit = sideEffect
def foo(f: Int => Unit): Unit = f(1)
val callback = (v: Int) => println(v.toString)
// valid usages
foo(callback(0))
foo(callback)
// user mistake – does not execute callback. Should warn.
foo(if (true) callback) The last line currently throws a discard warning in Scala 3, and IMO it should stay that way. Both for consistency with |
Neither version of
The feature request might be to ensure that function values are eventually applied. |
... I don't understand how I would use this
You mean that Scala should detect that the val observer: Observer[Int] = ???
def foo(observer: Observer[Int]): Unit = observer.onNext(1)
// valid code
foo(observer)
// user mistake – does not execute observer. Should warn.
foo(if (true) observer) Function-specific lints could be useful for other things, but not as a fix to this proposed bugfix. My foo-s were just an example of me using value-discard warnings for their stated purpose.
Scala 3 added this new place where non-Unit values are discarded, similar to |
The linked ticket is about I would demote this ticket to a "feature request" for a way to quiet the warning, perhaps |
Compiler version
3.5
Minimized code
Output
It warns on one-legged or one-armed if.
Expectation
Scala 2 decides not to warn, because one-limbed if is intentionally a value-discarding Unit expression.
The revisted expectation is that it's easy to silence the warning, probably with a category:
It is a matter of taste or style or risk tolerance whether
if (b) e
is obviously discarding.The text was updated successfully, but these errors were encountered: