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

proposal: lint to catch unawaited futures in try blocks #59155

Closed
5 tasks
eseidel opened this issue May 23, 2023 · 4 comments
Closed
5 tasks

proposal: lint to catch unawaited futures in try blocks #59155

eseidel opened this issue May 23, 2023 · 4 comments
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-lint-proposal linter-status-pending

Comments

@eseidel
Copy link
Contributor

eseidel commented May 23, 2023

The gotcha occurs when you return Future<T> from Future<T> foo async {} instead of T and that return is inside a try block.

https://twitter.com/_eseidel/status/1660092263190650881

https://dartpad.dev/?id=f4c93eb9f9e24d43079cbc859a867894

class ApiException implements Exception {
  ApiException();
}

Future<void> inner() async {
  throw ApiException();
}

Future<void> _middle() async {
  try {
    return inner();
  } on ApiException catch (e) {
    print('inner: $e');
  }
}

Future<void> outer() async {
  try {
    await _middle();
  } on ApiException catch (e) {
    print('outer: $e');
  }
}

void main() async {
  await outer();
}

Would love a lint that would have caught that (and saved me -- and clearly others according to responses on that twitter thread) a while of searching for the issue.

Discussion checklist

  • List any existing rules this proposal modifies, complements, overlaps or conflicts with.

unawaited_futures could have some overlap, although it doesn't trigger here.

unnecessary_await_in_return seems in direct conflict.

  • List any relevant issues (reported here, the [SDK Tracker], or elsewhere).

  • If there's any prior art (e.g., in other linters), please add references here.
    https://dcm.dev/docs/teams/rules/common/prefer-return-await/

  • If this proposal corresponds to [Effective Dart] or [Flutter Style Guide] advice, please call it out. (If there isn't any corresponding advice, should there be?)

  • If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.

Arguably the real bug is that Future<T> foo async {} allows returning Future<T> directly at all, since it nominally expects you to return a T. But presumably that's an intentional language decision.

@eernstg
Copy link
Member

eernstg commented May 23, 2023

Perhaps this issue reports the same behavior as #44395, where it is reported as a bug?

@eseidel
Copy link
Contributor Author

eseidel commented May 23, 2023

Also probably a dupe of #58279.

I'm not trying to pick a fight. :) Just trying to get to some outcome for users (like me) so I don't spend time chasing exceptions coming from inside code I was expecting was catching them. 🙃

@eseidel
Copy link
Contributor Author

eseidel commented May 23, 2023

Looks like lots of bugs covering this. Closing this one. Thank you!

@eseidel eseidel closed this as completed May 23, 2023
@incendial
Copy link
Contributor

@eseidel hey, considering you added a link to the DCM's rule, would you like to try it? Or is there anything that stops you?

@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 19, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 19, 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-lint-proposal linter-status-pending
Projects
None yet
Development

No branches or pull requests

4 participants