diff options
| author | y21 <30553356+y21@users.noreply.github.com> | 2023-07-22 14:33:36 +0200 |
|---|---|---|
| committer | y21 <30553356+y21@users.noreply.github.com> | 2023-07-22 14:33:36 +0200 |
| commit | 482d5fafc9ffc7edf9236ea186883ee0346e7938 (patch) | |
| tree | b3929b8c8880d123cb539c6e6036b1f34fe1dd32 | |
| parent | 37b83660bc2501f6e90cb48689304aed73229967 (diff) | |
| download | rust-482d5fafc9ffc7edf9236ea186883ee0346e7938.tar.gz rust-482d5fafc9ffc7edf9236ea186883ee0346e7938.zip | |
replace HashMap with Vec, use span_lint_hir_and_then
| -rw-r--r-- | clippy_lints/src/unused_async.rs | 21 | ||||
| -rw-r--r-- | tests/ui/unused_async.stderr | 36 |
2 files changed, 28 insertions, 29 deletions
diff --git a/clippy_lints/src/unused_async.rs b/clippy_lints/src/unused_async.rs index 5c8c6e58914..bc7c3897a6e 100644 --- a/clippy_lints/src/unused_async.rs +++ b/clippy_lints/src/unused_async.rs @@ -1,6 +1,5 @@ -use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::diagnostics::span_lint_hir_and_then; use clippy_utils::is_def_id_trait_method; -use rustc_data_structures::fx::FxHashMap; use rustc_hir::def::DefKind; use rustc_hir::intravisit::{walk_body, walk_expr, walk_fn, FnKind, Visitor}; use rustc_hir::{Body, Expr, ExprKind, FnDecl, Node, YieldSource}; @@ -47,11 +46,12 @@ pub struct UnusedAsync { async_fns_as_value: LocalDefIdSet, /// Functions with unused `async`, linted post-crate after we've found all uses of local async /// functions - unused_async_fns: FxHashMap<LocalDefId, UnusedAsyncFn>, + unused_async_fns: Vec<UnusedAsyncFn>, } #[derive(Copy, Clone)] struct UnusedAsyncFn { + def_id: LocalDefId, fn_span: Span, await_in_async_block: Option<Span>, } @@ -122,13 +122,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync { // Don't lint just yet, but store the necessary information for later. // The actual linting happens in `check_crate_post`, once we've found all // uses of local async functions that do require asyncness to pass typeck - self.unused_async_fns.insert( + self.unused_async_fns.push(UnusedAsyncFn { + await_in_async_block: visitor.await_in_async_block, + fn_span: span, def_id, - UnusedAsyncFn { - await_in_async_block: visitor.await_in_async_block, - fn_span: span, - }, - ); + }); } } } @@ -164,12 +162,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync { let iter = self .unused_async_fns .iter() - .filter_map(|(did, item)| (!self.async_fns_as_value.contains(did)).then_some(item)); + .filter(|UnusedAsyncFn { def_id, .. }| (!self.async_fns_as_value.contains(def_id))); for fun in iter { - span_lint_and_then( + span_lint_hir_and_then( cx, UNUSED_ASYNC, + cx.tcx.local_def_id_to_hir_id(fun.def_id), fun.fn_span, "unused `async` for function with no await statements", |diag| { diff --git a/tests/ui/unused_async.stderr b/tests/ui/unused_async.stderr index 035dffa7f13..8d9b72c4886 100644 --- a/tests/ui/unused_async.stderr +++ b/tests/ui/unused_async.stderr @@ -1,11 +1,28 @@ error: unused `async` for function with no await statements + --> $DIR/unused_async.rs:13:5 + | +LL | / async fn async_block_await() { +LL | | async { +LL | | ready(()).await; +LL | | }; +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` + --> $DIR/unused_async.rs:15:23 + | +LL | ready(()).await; + | ^^^^^ + = note: `-D clippy::unused-async` implied by `-D warnings` + +error: unused `async` for function with no await statements --> $DIR/unused_async.rs:45:5 | LL | async fn f3() {} | ^^^^^^^^^^^^^^^^ | = help: consider removing the `async` from this function - = note: `-D clippy::unused-async` implied by `-D warnings` error: unused `async` for function with no await statements --> $DIR/unused_async.rs:57:1 @@ -18,23 +35,6 @@ LL | | } = help: consider removing the `async` from this function error: unused `async` for function with no await statements - --> $DIR/unused_async.rs:13:5 - | -LL | / async fn async_block_await() { -LL | | async { -LL | | ready(()).await; -LL | | }; -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` - --> $DIR/unused_async.rs:15:23 - | -LL | ready(()).await; - | ^^^^^ - -error: unused `async` for function with no await statements --> $DIR/unused_async.rs:68:5 | LL | / async fn unused(&self) -> i32 { |
