Skip to content

Commit 482d5fa

Browse files
committed
replace HashMap with Vec, use span_lint_hir_and_then
1 parent 37b8366 commit 482d5fa

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

clippy_lints/src/unused_async.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::is_def_id_trait_method;
3-
use rustc_data_structures::fx::FxHashMap;
43
use rustc_hir::def::DefKind;
54
use rustc_hir::intravisit::{walk_body, walk_expr, walk_fn, FnKind, Visitor};
65
use rustc_hir::{Body, Expr, ExprKind, FnDecl, Node, YieldSource};
@@ -47,11 +46,12 @@ pub struct UnusedAsync {
4746
async_fns_as_value: LocalDefIdSet,
4847
/// Functions with unused `async`, linted post-crate after we've found all uses of local async
4948
/// functions
50-
unused_async_fns: FxHashMap<LocalDefId, UnusedAsyncFn>,
49+
unused_async_fns: Vec<UnusedAsyncFn>,
5150
}
5251

5352
#[derive(Copy, Clone)]
5453
struct UnusedAsyncFn {
54+
def_id: LocalDefId,
5555
fn_span: Span,
5656
await_in_async_block: Option<Span>,
5757
}
@@ -122,13 +122,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
122122
// Don't lint just yet, but store the necessary information for later.
123123
// The actual linting happens in `check_crate_post`, once we've found all
124124
// uses of local async functions that do require asyncness to pass typeck
125-
self.unused_async_fns.insert(
125+
self.unused_async_fns.push(UnusedAsyncFn {
126+
await_in_async_block: visitor.await_in_async_block,
127+
fn_span: span,
126128
def_id,
127-
UnusedAsyncFn {
128-
await_in_async_block: visitor.await_in_async_block,
129-
fn_span: span,
130-
},
131-
);
129+
});
132130
}
133131
}
134132
}
@@ -164,12 +162,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
164162
let iter = self
165163
.unused_async_fns
166164
.iter()
167-
.filter_map(|(did, item)| (!self.async_fns_as_value.contains(did)).then_some(item));
165+
.filter(|UnusedAsyncFn { def_id, .. }| (!self.async_fns_as_value.contains(def_id)));
168166

169167
for fun in iter {
170-
span_lint_and_then(
168+
span_lint_hir_and_then(
171169
cx,
172170
UNUSED_ASYNC,
171+
cx.tcx.local_def_id_to_hir_id(fun.def_id),
173172
fun.fn_span,
174173
"unused `async` for function with no await statements",
175174
|diag| {

tests/ui/unused_async.stderr

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1+
error: unused `async` for function with no await statements
2+
--> $DIR/unused_async.rs:13:5
3+
|
4+
LL | / async fn async_block_await() {
5+
LL | | async {
6+
LL | | ready(()).await;
7+
LL | | };
8+
LL | | }
9+
| |_____^
10+
|
11+
= help: consider removing the `async` from this function
12+
note: `await` used in an async block, which does not require the enclosing function to be `async`
13+
--> $DIR/unused_async.rs:15:23
14+
|
15+
LL | ready(()).await;
16+
| ^^^^^
17+
= note: `-D clippy::unused-async` implied by `-D warnings`
18+
119
error: unused `async` for function with no await statements
220
--> $DIR/unused_async.rs:45:5
321
|
422
LL | async fn f3() {}
523
| ^^^^^^^^^^^^^^^^
624
|
725
= help: consider removing the `async` from this function
8-
= note: `-D clippy::unused-async` implied by `-D warnings`
926

1027
error: unused `async` for function with no await statements
1128
--> $DIR/unused_async.rs:57:1
@@ -17,23 +34,6 @@ LL | | }
1734
|
1835
= help: consider removing the `async` from this function
1936

20-
error: unused `async` for function with no await statements
21-
--> $DIR/unused_async.rs:13:5
22-
|
23-
LL | / async fn async_block_await() {
24-
LL | | async {
25-
LL | | ready(()).await;
26-
LL | | };
27-
LL | | }
28-
| |_____^
29-
|
30-
= help: consider removing the `async` from this function
31-
note: `await` used in an async block, which does not require the enclosing function to be `async`
32-
--> $DIR/unused_async.rs:15:23
33-
|
34-
LL | ready(()).await;
35-
| ^^^^^
36-
3737
error: unused `async` for function with no await statements
3838
--> $DIR/unused_async.rs:68:5
3939
|

0 commit comments

Comments
 (0)