Skip to content

Commit

Permalink
replace HashMap with Vec, use span_lint_hir_and_then
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jul 22, 2023
1 parent 37b8366 commit 482d5fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
21 changes: 10 additions & 11 deletions clippy_lints/src/unused_async.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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,
},
);
});
}
}
}
Expand Down Expand Up @@ -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| {
Expand Down
36 changes: 18 additions & 18 deletions tests/ui/unused_async.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,23 +34,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
|
Expand Down

0 comments on commit 482d5fa

Please sign in to comment.