scala/scala-async

False warnings with scalac-option `-Wnonunit-statement`

cornerman opened this issue · 2 comments

The scala-async macro does not work well together with the rather new scalac option -Wnonunit-statement (scala/scala#9893).

Example:

  def test(): Future[Boolean] =
    async {
      await(Future.successful(true))
    }

This will yield a warning:

unused value of type Boolean (add `: Unit` to discard silently)
[warn]     async {

Anything we can do about it?

This seems to be related how markForAsyncTransform work in the scala compiler. The above expression desugars into the following:

{
  final class stateMachine$async extends _root_.scala.async.FutureStateMachine {
    def <init>(): stateMachine$async = {
      stateMachine$async.super.<init>(scala.concurrent.ExecutionContext.Implicits.global);
      ()
    };
    override def apply(tr$async: scala.util.Try[AnyRef]): Unit = {
      scala.Predef.locally[Boolean](scala.async.Async.await[Boolean](scala.concurrent.Future.successful[Boolean](true)));
      ()
    }
  };
  (new stateMachine$async().start[Nothing](): scala.concurrent.Future[Boolean])
}

There we have an unused non-unit statement with the locally block which returns an unused Boolean.

Fixed on the transform side, which is more general.