Skip to content

Commit 97d4513

Browse files
committed
Auto merge of #9025 - Alexendoo:unused-async-method, r=dswij
unused_async: lint async methods Now lints: ```rust impl Foo { async fn method(&self) -> &'static str { "no await here" } } ``` changelog: [`unused_async`]: lint async methods Fixes #9024
2 parents 195f2cb + a0b107b commit 97d4513

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed

clippy_lints/src/unused_async.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor};
3-
use rustc_hir::{Body, Expr, ExprKind, FnDecl, FnHeader, HirId, IsAsync, YieldSource};
3+
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId, IsAsync, YieldSource};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_middle::hir::nested_filter;
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -68,20 +68,18 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
6868
span: Span,
6969
hir_id: HirId,
7070
) {
71-
if let FnKind::ItemFn(_, _, FnHeader { asyncness, .. }) = &fn_kind {
72-
if matches!(asyncness, IsAsync::Async) {
73-
let mut visitor = AsyncFnVisitor { cx, found_await: false };
74-
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), span, hir_id);
75-
if !visitor.found_await {
76-
span_lint_and_help(
77-
cx,
78-
UNUSED_ASYNC,
79-
span,
80-
"unused `async` for function with no await statements",
81-
None,
82-
"consider removing the `async` from this function",
83-
);
84-
}
71+
if !span.from_expansion() && fn_kind.asyncness() == IsAsync::Async {
72+
let mut visitor = AsyncFnVisitor { cx, found_await: false };
73+
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), span, hir_id);
74+
if !visitor.found_await {
75+
span_lint_and_help(
76+
cx,
77+
UNUSED_ASYNC,
78+
span,
79+
"unused `async` for function with no await statements",
80+
None,
81+
"consider removing the `async` from this function",
82+
);
8583
}
8684
}
8785
}

tests/ui/methods.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
clippy::use_self,
1616
clippy::useless_format,
1717
clippy::wrong_self_convention,
18+
clippy::unused_async,
1819
clippy::unused_self,
1920
unused
2021
)]

tests/ui/methods.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: methods called `new` usually return `Self`
2-
--> $DIR/methods.rs:103:5
2+
--> $DIR/methods.rs:104:5
33
|
44
LL | / fn new() -> i32 {
55
LL | | 0
@@ -9,7 +9,7 @@ LL | | }
99
= note: `-D clippy::new-ret-no-self` implied by `-D warnings`
1010

1111
error: called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(..)` instead
12-
--> $DIR/methods.rs:124:13
12+
--> $DIR/methods.rs:125:13
1313
|
1414
LL | let _ = v.iter().filter(|&x| {
1515
| _____________^

tests/ui/should_impl_trait/corner_cases.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
clippy::missing_safety_doc,
99
clippy::wrong_self_convention,
1010
clippy::missing_panics_doc,
11-
clippy::return_self_not_must_use
11+
clippy::return_self_not_must_use,
12+
clippy::unused_async
1213
)]
1314

1415
use std::ops::Mul;

tests/ui/unused_async.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![warn(clippy::unused_async)]
22

3+
use std::future::Future;
4+
use std::pin::Pin;
5+
36
async fn foo() -> i32 {
47
4
58
}
@@ -8,6 +11,37 @@ async fn bar() -> i32 {
811
foo().await
912
}
1013

14+
struct S;
15+
16+
impl S {
17+
async fn unused(&self) -> i32 {
18+
1
19+
}
20+
21+
async fn used(&self) -> i32 {
22+
self.unused().await
23+
}
24+
}
25+
26+
trait AsyncTrait {
27+
fn trait_method() -> Pin<Box<dyn Future<Output = i32>>>;
28+
}
29+
30+
macro_rules! async_trait_impl {
31+
() => {
32+
impl AsyncTrait for S {
33+
fn trait_method() -> Pin<Box<dyn Future<Output = i32>>> {
34+
async fn unused() -> i32 {
35+
5
36+
}
37+
38+
Box::pin(unused())
39+
}
40+
}
41+
};
42+
}
43+
async_trait_impl!();
44+
1145
fn main() {
1246
foo();
1347
bar();

tests/ui/unused_async.stderr

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unused `async` for function with no await statements
2-
--> $DIR/unused_async.rs:3:1
2+
--> $DIR/unused_async.rs:6:1
33
|
44
LL | / async fn foo() -> i32 {
55
LL | | 4
@@ -9,5 +9,15 @@ LL | | }
99
= note: `-D clippy::unused-async` implied by `-D warnings`
1010
= help: consider removing the `async` from this function
1111

12-
error: aborting due to previous error
12+
error: unused `async` for function with no await statements
13+
--> $DIR/unused_async.rs:17:5
14+
|
15+
LL | / async fn unused(&self) -> i32 {
16+
LL | | 1
17+
LL | | }
18+
| |_____^
19+
|
20+
= help: consider removing the `async` from this function
21+
22+
error: aborting due to 2 previous errors
1323

0 commit comments

Comments
 (0)