From 8ef6240afb00ec9a892cb41c36cde102b688343a Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Tue, 23 May 2023 17:22:23 +0200 Subject: [PATCH] point to `await` expr in note --- clippy_lints/src/unused_async.rs | 16 ++++++++++------ tests/ui/unused_async.stderr | 6 +++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/unused_async.rs b/clippy_lints/src/unused_async.rs index 4c68cda7a..117dda092 100644 --- a/clippy_lints/src/unused_async.rs +++ b/clippy_lints/src/unused_async.rs @@ -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, async_depth: usize, } @@ -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); @@ -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 { @@ -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`", + ); } }, ); diff --git a/tests/ui/unused_async.stderr b/tests/ui/unused_async.stderr index e39f9b20b..8ac2066a6 100644 --- a/tests/ui/unused_async.stderr +++ b/tests/ui/unused_async.stderr @@ -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