-
Notifications
You must be signed in to change notification settings - Fork 1.7k
unawaited_future should not warn if .then block is present #27401
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
Comments
For extra bonus points, if we can distinguish a |
reviewing it, also cc @ochafik |
I added the following snippet to the test with the second line linted:
@Hixie IIUC we want to lint handlers with an explicit return statement? The rule seems to behave as expected. IIRC flutter has a fork of the dart sdk, Could it be that recent improvements to the analyzer itself fixed this issue? Tested at dart-archive/linter@407d736 |
Have you considered to make an unawaited future (even with a Whether the then-block returns |
@irhn raises a good point. I don't know what the right solution is. |
Can we make sure we're only muting lints for these @lrhn: the lint currently only trigger inside async bodies. |
I would find this quite confusing. I have a hard time seeing why doSomething(); and doSomething().then((Result result) {
// handle the result
}); should be treated differently. The only difference is that the 2nd does "something" when the async call completes but both continue executing the following code immediately without waiting for the async call to complete, which might or might not be intended in both cases. |
And both fail to catch exceptions, leaving them as uncaught (even if that line is wrapped in a try/catch block). I guess the Future chain needs to end with something that handles errors for the lint to not complain. The question is, though, how would you write the code if you did want to this to be done asynchronously with the rest of the function? |
I seem to remember something the discussions from when this hint was introduced. // no hint, future is accounted for. Might get an unused variable warning, though?
var _ = asyncFunction();
restOfFunction(); It is true that the only way to know that a future is handled is to |
Assigning to a var will not result in a lint, as only non-awaited expression statements trigger the lint (bar some exceptions). For reference: |
Assigning to a variable is no good because (a) the variable will be unused, so we would still be getting a warning for that regardless, and (b) the bug is still there -- we're ignoring the errors from that future, and it will eventually result in those errors being uncaught top-level exceptions in the zone, so we should be getting an "unawaited future" warning. |
As mentioned in this thread it's no safer to skip the |
If a method returns a
Future
and a.then
expression is attached to the result, the unawaited_future still reports a problem since the.then
expression itself returns aFuture
. Yes, the developer could add an// ignore: unawaited_futures
comment above thethen
expression, but that clutters the code for the 99% of situations where the developer is properly handling the result.I propose that we change the definition of the unawaited_future lint to:
GOOD:
BAD:
The text was updated successfully, but these errors were encountered: