dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

Analyzer reports a return-without-value in a `FutureOr<void>` function; does not report empty function #44480

Open srawlins opened 3 years ago

srawlins commented 3 years ago

See these two functions, each in null safe code:

import 'dart:async';

FutureOr<void> f1() {
  return; // error: return_without_value
}

FutureOr<void> f2() {} // clean

A bit odd. I assume the return_without_value report is erroneous.

srawlins commented 3 years ago

@scheglov if the analyzer should not be reporting that return statement in f1, I can fix in ReturnTypeVerifier; I'm poking around there now.

eernstg commented 3 years ago

It is a bit odd, but it does follow the specification, here: Only void, dynamic and Null allow for return;. We have some null safety related updates about returns here, but the return; rule is unchanged.

So it's currently not a bug, it is working as intended. If you wish to make it a bug then it should be handled as a language proposal.

Note that the type FutureOr<void> is inherently questionable: FutureOr is an untagged union (like Future<void> | void), but the void type admits any object whatsoever, including futures. This means that there is no safe way to determine which case we have for a value of type FutureOr<void>: If it is not a future then it's the case void (so we should ignore and discard the value), but if it is a Future<T> for some T then it could be intended to be used as a future, or it could be intended as a void value that the caller should discard.

For anyone who wishes to make a distinction between a value of no interest and a future (so the future "may or may not be there"), the type Future<T>? is a much better match: It can be detected whether the given object is null or a future.

srawlins commented 3 years ago

Thanks so much for the explanation, @eernstg !

srawlins commented 3 years ago

I've filed https://github.com/dart-lang/language/issues/1370

srawlins commented 3 months ago

Still not reported.