Linter: unawaited_futures misses cases

Created on 2 Jun 2020  路  8Comments  路  Source: dart-lang/linter

Dart VM version: 2.8.2 (stable) (Unknown timestamp) on "linux_x64"

I have noticed while using the unawaited_futures lint feature that the lint misses cases quite often. Maybe 2/10 it will miss the case and show nothing. It could be just cases where there is an async and the function returns void.

All 8 comments

This example doesn't show the lint message :

import 'dart:io';

abstract class AbstractEntries {

  void validateServer() async {
    await sleep(const Duration(seconds: 5));
  }
}

class GetAuthentication extends AbstractEntries {

  void getEntries() {
    validateServer();
  }
}

If a function returns void, then it's not considered as returning a future. If it returns Object, it's also not considered as returning a Future, even though it could (since Futures are Objects).

Here the function implementation is async, but the call point doesn't know that (and you can't know that since it's a virtual function you are calling, and it could be overridden in a subclass to actually not return anything), so we trust the return type. A return type of void means that it's safe (and highly recommended) to ignore the returned value.

If you didn't intend the future returned by void validateServer() async { ... } to be ignored, it should be typed as Future<void> validateServer();.

The lint is based on the static types. The static types here do not say that you return a future.

Also, the lint only applies to async functions, and the getEntries function is not async, so even if validateServer returned a future, you couldn't await it, and therefore won't get a warning that you don't.

Is there another lint feature which prompts to return Future<void> instead of void on async functions ?

Thanks @pq
I turned on that lint feature and it notified me to replace 321 voids with Future<void>
That enabled me to discover 17 awaits I had missed.

Should save me quite a bit of time going forward - I wasted 3 hours yesterday debugging one of those awaits I had missed.

Would be good if avoid_void_async was turned on by default in new projects.

Ah, cool. So nice to hear that it was useful! There's been a bunch of lively debate on this one and your experience is a great testimonial. Cheers!

I did search for such a lint feature before asking - my google history shows I searched :

dartlang lint async void

But I never managed to find it :(

Was this page helpful?
0 / 5 - 0 ratings