Skip to content

Commit

Permalink
point to await expr in note
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed May 23, 2023
1 parent 3eeeaa2 commit 8ef6240
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 10 additions & 6 deletions clippy_lints/src/unused_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct AsyncFnVisitor<'a, 'tcx> {
found_await: bool,
/// Also keep track of `await`s in nested async blocks so we can mention
/// it in a note
found_await_in_async_block: bool,
await_in_async_block: Option<Span>,
async_depth: usize,
}

Expand All @@ -55,8 +55,8 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> {
if let ExprKind::Yield(_, YieldSource::Await { .. }) = ex.kind {
if self.async_depth == 1 {
self.found_await = true;
} else {
self.found_await_in_async_block = true;
} else if self.await_in_async_block.is_none() {
self.await_in_async_block = Some(ex.span);
}
}
walk_expr(self, ex);
Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
cx,
found_await: false,
async_depth: 0,
found_await_in_async_block: false,
await_in_async_block: None,
};
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), def_id);
if !visitor.found_await {
Expand All @@ -108,8 +108,12 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
|diag| {
diag.help("consider removing the `async` from this function");

if visitor.found_await_in_async_block {
diag.note("`await` used in an async block, which does not require the enclosing function to be `async`");
if let Some(span) = visitor.await_in_async_block {
diag.span_note(
span,
"`await` used in an async block, which does not require \
the enclosing function to be `async`",
);
}
},
);
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/unused_async.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ LL | | }
| |_____^
|
= help: consider removing the `async` from this function
= note: `await` used in an async block, which does not require the enclosing function to be `async`
note: `await` used in an async block, which does not require the enclosing function to be `async`
--> $DIR/unused_async.rs:13:23
|
LL | ready(()).await;
| ^^^^^
= note: `-D clippy::unused-async` implied by `-D warnings`

error: unused `async` for function with no await statements
Expand Down

0 comments on commit 8ef6240

Please sign in to comment.