Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unawaited_futures and returning Future results in exceptions not caught by expected function code #58234

Closed
hoylen opened this issue Sep 24, 2020 · 1 comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-false-negative P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@hoylen
Copy link
Contributor

hoylen commented Sep 24, 2020

Describe the issue

Normally, if a function returns a Future and the last statement calls a second function to obtain that Future, the await can be omitted. That is, there is nothing wrong with this:

Future<int> foo() async {
  ...
  return bar();
}

But when an exception can be thrown asynchronously by that second function and the first function expects to catch it, the await is mandatory, otherwise it will not be caught within the function.

Future<int> foo() async {
  try {
    ...
    return bar();
  } catch (e) {
    ... // exceptions thrown by bar might NOT be caught here, but the programmer probably expected them to be
  }
}

The unawaited_futures linter rule currently does not detect this situation.

It would be good if the linter could alert the programmer that they might want to change the code to:

Future<int> foo() async {
  try {
    ...
    return await bar();
  } catch (e) {
    ... // exceptions thrown by bar are caught here
  }
}

To Reproduce

Future<void> main(List<String> arguments) async {
  try {
    final price = await foo('good');
    print(price);

    final price2 = await foo('bad');
    print(price2);

    await Future.delayed(Duration(seconds: 2));
  } catch (e) {
    print('Unexpected exception caught in main: $e');
  }

  print('done');
}

Future<int> foo(String symbol) async {
  try {
    // return await bar(symbol); // correct code
    return bar(symbol); // incorrect code
  } catch (e) {
    print('Expected exception caught in foo: $e');
    return 0;
  }
}

Future<int> bar(String symbol) async {
  if (symbol == 'good') {
    return 42;
  } else {
    await Future.delayed(Duration(seconds: 1));
    throw ArgumentError('symbol: $symbol');
  }
}

The return statement inside the try block in the _foo_function is where the problem is.

Expected behavior

Linter warns the programmer about a potential missing await, otherwise asynchronously thrown exceptions will not be caught by the catch block(s).

Additional context

It would be good if the unawaited_futures linter rule detected this situation, since it is an example of where an await is missing for proper/expected behaviour. Triggering if the return statement is inside a try/catch block.

Or maybe it needs to be a new linter rule, since there could be interactions/complications with the unnecessary_await_in_return linter rule.

@pq pq added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Dec 23, 2020
@srawlins srawlins added P3 A lower priority bug or feature request linter-false-negative labels Sep 25, 2022
@srawlins
Copy link
Member

Duplicate of #58279; this one is older but that one has more discussion.

@devoncarew devoncarew added analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-false-negative P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants