summary refs log tree commit diff
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2023-05-23 17:22:23 +0200
committery21 <30553356+y21@users.noreply.github.com>2023-05-23 17:22:23 +0200
commit8ef6240afb00ec9a892cb41c36cde102b688343a (patch)
tree4d87f620b88ff0c9624af5605d9f8ad7b2a5d646
parent3eeeaa2bc7bff6b9f11dfe99889a4627904de221 (diff)
downloadrust-8ef6240afb00ec9a892cb41c36cde102b688343a.tar.gz
rust-8ef6240afb00ec9a892cb41c36cde102b688343a.zip
point to `await` expr in note
-rw-r--r--clippy_lints/src/unused_async.rs16
-rw-r--r--tests/ui/unused_async.stderr6
2 files changed, 15 insertions, 7 deletions
diff --git a/clippy_lints/src/unused_async.rs b/clippy_lints/src/unused_async.rs
index 4c68cda7aa7..117dda09222 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<Span>,
     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 e39f9b20b88..8ac2066a6b2 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