Skip to content

Discussion: throwing away Futures into void locations #57769

Open
@MichaelRFairhurst

Description

@MichaelRFairhurst

This could be part of void_checks or unawaited_futures.

Given, the following function which takes a void function:

void foo(void Function() bar) {
  ...
}

and the following call site:

  foo(() async {
    ...
  });

We can confidently say that the resulting future will never be awaited. We can also say, this is a value that probably shouldn't be thrown away into a void location.

There's therefore an argument that this should be flagged by either void_checks or unawaited_futures.

It applies to other cases too:

final List<void> voidList = <Future>[...];
// or even what about.. ?
final List<Object> objectList = <Future>[...];

though these probably don't have to be covered right away.

As an argument against this, I expect the following type of code to be common:

stream.listen((x) async {
  ...
});

in the case where (x) async {...} needs to do async work, there's no better way to write this.

And yes it may have race conditions, but if the code needs synchronization, it looks wrong to me for it to not have synchronization explicitly:

stream.listen((x) async {
  await lock.aquire();
  ...
  lock.release();
});

But the lint may still catch cases where a the misuse an API, and hopefully there's a correct way to use the API that they can find after they see the lint.

[...].forEach((x) async {
  // this won't be awaited! What if these need to execute serially?
  ....
});
// there's no forEachAsync, however, there are other ways of doing what you want:
await [...].fold(Future<void>.value(null), (future, next) async {
  await future;
  ....
});

Counterpoint: the lack of await forEach() tells you that the code won't be serial, and so maybe that's another clue that this type of error isn't common.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagetype-enhancementA request for a change that isn't a bugtype-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions