Skip to content

Commit 8ef6240

Browse files
committed
point to await expr in note
1 parent 3eeeaa2 commit 8ef6240

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

clippy_lints/src/unused_async.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct AsyncFnVisitor<'a, 'tcx> {
4444
found_await: bool,
4545
/// Also keep track of `await`s in nested async blocks so we can mention
4646
/// it in a note
47-
found_await_in_async_block: bool,
47+
await_in_async_block: Option<Span>,
4848
async_depth: usize,
4949
}
5050

@@ -55,8 +55,8 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> {
5555
if let ExprKind::Yield(_, YieldSource::Await { .. }) = ex.kind {
5656
if self.async_depth == 1 {
5757
self.found_await = true;
58-
} else {
59-
self.found_await_in_async_block = true;
58+
} else if self.await_in_async_block.is_none() {
59+
self.await_in_async_block = Some(ex.span);
6060
}
6161
}
6262
walk_expr(self, ex);
@@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
9696
cx,
9797
found_await: false,
9898
async_depth: 0,
99-
found_await_in_async_block: false,
99+
await_in_async_block: None,
100100
};
101101
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), def_id);
102102
if !visitor.found_await {
@@ -108,8 +108,12 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
108108
|diag| {
109109
diag.help("consider removing the `async` from this function");
110110

111-
if visitor.found_await_in_async_block {
112-
diag.note("`await` used in an async block, which does not require the enclosing function to be `async`");
111+
if let Some(span) = visitor.await_in_async_block {
112+
diag.span_note(
113+
span,
114+
"`await` used in an async block, which does not require \
115+
the enclosing function to be `async`",
116+
);
113117
}
114118
},
115119
);

tests/ui/unused_async.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ LL | | }
99
| |_____^
1010
|
1111
= help: consider removing the `async` from this function
12-
= note: `await` used in an async block, which does not require the enclosing function to be `async`
12+
note: `await` used in an async block, which does not require the enclosing function to be `async`
13+
--> $DIR/unused_async.rs:13:23
14+
|
15+
LL | ready(()).await;
16+
| ^^^^^
1317
= note: `-D clippy::unused-async` implied by `-D warnings`
1418

1519
error: unused `async` for function with no await statements

0 commit comments

Comments
 (0)