return_unawaited_in_try
Reports a future that an async function returns from inside a try without await, so the try does not guard it. Write return await.
- Tier
- blocking
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- awaits the returned future
What it reports
Section titled “What it reports”A return of a future inside the body of a try, in an async function: try { return load(); } catch (e) { ... }. The function’s own future completes with the returned one after the try is left. So a failure of it passes by the catch clauses, and a finally block runs before the work it guards is done. The violation covers the returned value.
It reports a value whose type is a Future, a subtype of it or a FutureOr, nullable or not, also from a nested block, a loop or an inner try inside the body.
It does not report:
- a
returnin acatchclause or afinallyblock; - a
returnin a closure; - a function that is not
async, or that is a generator; - a value of type
dynamic.
The fix writes return await load();, and brackets the value, await (a ?? b), where await would bind tighter than its operators.
Left to the SDK
Section titled “Left to the SDK”From Dart 3.13 on, the analyzer reports this defect itself, with its warning unawaited_return_in_try_block at the return, on by default. It covers every form this rule reports in code that compiles: a return in a function, getter, method, closure or local function, from a nested block or an inner try, of a Future, a subtype or a FutureOr.
So that one defect is one violation, on such an SDK the rule leaves every place to that warning:
- Both hosts drop the rule’s violations, and the analysis server and the standard analysis report the warning instead. The gate counts it as blocking, as it counts every warning of the standard analysis.
- On Dart 3.11 and 3.12 the rule reports as before.
- Where the project sets the warning to
ignore(orfalse) underanalyzer.errors, the rule reports again. Another severity there is the project’s choice for the warning, and the place then has that tier. - Under a configuration in error, the analysis server keeps the rule’s violations, so that the place is reported twice rather than not at all.
Where a place is left to the SDK, assay fix does not apply this rule’s fix there.
The code reads as guarded and is not: the error the catch was written for escapes it, and a lock released or a file closed in finally is released or closed while the work still runs. Tests that complete the future at once rarely show it.
Example
Section titled “Example”Future<User> load() async { try { return api.fetchUser(); } on HttpException { return User.guest; }}Future<User> load() async { try { return await api.fetchUser(); } on HttpException { return User.guest; }}Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// The caller handles the failure of this one.// ignore: return_unawaited_in_tryreturn load();// ignore_for_file: assay/return_unawaited_in_try silences it in a whole file, and the prefixed // ignore: assay/return_unawaited_in_try works too. On Dart 3.13 and later, name the SDK’s warning instead (see above). See Suppressions.