Skip to content

Commit a43bfef

Browse files
committed
[unused_async]: don't lint on async trait impls
1 parent 2e32905 commit a43bfef

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

clippy_lints/src/unused_async.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_hir::intravisit::{walk_body, walk_expr, walk_fn, FnKind, Visitor};
3-
use rustc_hir::{Body, Expr, ExprKind, FnDecl, YieldSource};
3+
use rustc_hir::{Body, Expr, ExprKind, FnDecl, ItemKind, Node, YieldSource};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_middle::hir::nested_filter;
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -81,6 +81,20 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> {
8181
}
8282
}
8383

84+
/// Checks if the `def_id` belongs to a function and that function is part of a trait impl,
85+
/// in which case we shouldn't lint because `async` is part of the trait definition and therefore
86+
/// can't be removed.
87+
fn is_in_trait_impl(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
88+
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
89+
&& let Node::Item(item) = cx.tcx.hir().get_parent(hir_id)
90+
&& let ItemKind::Impl(imp) = item.kind
91+
{
92+
imp.of_trait.is_some()
93+
} else {
94+
false
95+
}
96+
}
97+
8498
impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
8599
fn check_fn(
86100
&mut self,
@@ -91,7 +105,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
91105
span: Span,
92106
def_id: LocalDefId,
93107
) {
94-
if !span.from_expansion() && fn_kind.asyncness().is_async() {
108+
if !span.from_expansion() && fn_kind.asyncness().is_async() && !is_in_trait_impl(cx, def_id) {
95109
let mut visitor = AsyncFnVisitor {
96110
cx,
97111
found_await: false,

tests/ui/unused_async.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![warn(clippy::unused_async)]
2+
#![feature(async_fn_in_trait)]
3+
#![allow(incomplete_features)]
24

35
use std::future::Future;
46
use std::pin::Pin;
@@ -23,6 +25,18 @@ mod issue10800 {
2325
}
2426
}
2527

28+
mod issue10459 {
29+
trait HasAsyncMethod {
30+
async fn do_something() -> u32;
31+
}
32+
33+
impl HasAsyncMethod for () {
34+
async fn do_something() -> u32 {
35+
1
36+
}
37+
}
38+
}
39+
2640
async fn foo() -> i32 {
2741
4
2842
}

tests/ui/unused_async.stderr

Lines changed: 4 additions & 4 deletions
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:11:5
2+
--> $DIR/unused_async.rs:13:5
33
|
44
LL | / async fn async_block_await() {
55
LL | | async {
@@ -10,14 +10,14 @@ LL | | }
1010
|
1111
= help: consider removing the `async` from this function
1212
note: `await` used in an async block, which does not require the enclosing function to be `async`
13-
--> $DIR/unused_async.rs:13:23
13+
--> $DIR/unused_async.rs:15:23
1414
|
1515
LL | ready(()).await;
1616
| ^^^^^
1717
= note: `-D clippy::unused-async` implied by `-D warnings`
1818

1919
error: unused `async` for function with no await statements
20-
--> $DIR/unused_async.rs:26:1
20+
--> $DIR/unused_async.rs:40:1
2121
|
2222
LL | / async fn foo() -> i32 {
2323
LL | | 4
@@ -27,7 +27,7 @@ LL | | }
2727
= help: consider removing the `async` from this function
2828

2929
error: unused `async` for function with no await statements
30-
--> $DIR/unused_async.rs:37:5
30+
--> $DIR/unused_async.rs:51:5
3131
|
3232
LL | / async fn unused(&self) -> i32 {
3333
LL | | 1

0 commit comments

Comments
 (0)