-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Discussion: throwing away Futures into void locations #57769
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
Another similar case is classes overriding class A {
void foo() {}
}
class B extends A {
@override
Future foo() async {}
} Code that calls with Should I open a separate issue for this? |
Another example that I've come across quite a bit in UI tests is this: someList.forEach((i) => i.click()); // where i.click() returns Future
someList.forEach((i) async {
await i.click();
});
Future<void> click(Elt elt) {}
someList.forEach(click); and also the same for |
Note that this issue is somewhat related to dart-lang/language#3715. If the type inference of function literals is adjusted to match the current specification, I think the following example will behave as requested in this issue, because the function literal will now have return type void foo(void Function() bar) {
...
}
void main() {
foo(() async {
... // Inferred return type: `void`.
});
} So the function literal would be flagged by Note, though, that someList.forEach((i) => i.click()); // where i.click() returns Future may still be accepted silently, because there is no constraint at all on the type of the returned value in an arrow ( |
@scheglov Another example of where inference is wrong in the analyzer. |
See also #58622. |
This could be part of void_checks or unawaited_futures.
Given, the following function which takes a void function:
and the following call site:
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:
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:
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:
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.
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.The text was updated successfully, but these errors were encountered: