Make unawaited accept a nullable argument.

The associated `unawaited_futures` lint triggers for expressions
of type `Future?` as well, so we should allow the workaround to
handle such expressions.

This change makes a static function more permissive, and is not expected
to be breaking in any way.
(It does mean that you need Dart 2.15.0 to use the function with
nullable arguments. I've changed the annotation to say `@Since("2.15")`.
That means anyone using a 2.15 SDK should assume that they need a 2.15
SDK to use it, and someone using a 2.14 SDK will not see the argument
as nullable. If anyone uses `@Since` annotations for anything at all.)

Change-Id: Ib2da4b353104cc88a834208a6ebd788ae55b4544
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214406
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
This commit is contained in:
Lasse R.H. Nielsen 2021-09-27 11:58:16 +00:00 committed by commit-bot@chromium.org
parent bb1640eec2
commit bdbd3c0602
4 changed files with 16 additions and 4 deletions

View file

@ -268,6 +268,11 @@
### Core libraries
#### `dart:async`
- Make the `unawaited` function's argument nullable, to allow calls like
`unawaited(foo?.bar())` too.
#### `dart:cli`
- The experimental `waitFor` functionality, and the library containing only that

View file

@ -780,7 +780,8 @@ abstract class Future<T> {
///
/// Not all futures need to be awaited.
/// The Dart linter has an optional ["unawaited futures" lint](https://dart-lang.github.io/linter/lints/unawaited_futures.html)
/// which enforces that futures (expressions with a static type of [Future])
/// which enforces that potential futures
/// (expressions with a static type of [Future] or `Future?`)
/// in asynchronous functions are handled *somehow*.
/// If a particular future value doesn't need to be awaited,
/// you can call `unawaited(...)` with it, which will avoid the lint,
@ -797,8 +798,8 @@ abstract class Future<T> {
/// are *expected* to complete with a value.
/// You can use [FutureExtensions.ignore] if you also don't want to know
/// about errors from this future.
@Since("2.14")
void unawaited(Future<void> future) {}
@Since("2.15")
void unawaited(Future<void>? future) {}
/// Convenience methods on futures.
///

View file

@ -14,13 +14,16 @@ void main() {
void testUnawaited() {
// Exists where expected.
prefix.unawaited.expectStaticType<Exactly<void Function(Future<Object?>)>>();
prefix.unawaited.expectStaticType<Exactly<void Function(Future<Object?>?)>>();
var future = Future<int>.value(42);
captureStaticType(unawaited(future), <T>(value) {
Expect.equals(typeOf<void>(), T);
});
Future<Never>? noFuture = null;
unawaited(noFuture); // Doesn't throw on null.
asyncStart();
// Unawaited futures still throw.
{

View file

@ -23,6 +23,9 @@ void testUnawaited() {
Expect.equals(typeOf<void>(), T);
});
Future<Never> noFuture = null;
unawaited(noFuture); // Doesn't throw on `null`.
asyncStart();
// Unawaited futures still throw.
{