Skip to content

Commit 750c7a1

Browse files
committed
Auto merge of rust-lang#11042 - y21:issue10459, r=Manishearth
[`unused_async`]: don't lint if function is part of a trait Fixes rust-lang#10459. We shouldn't lint if the function is part of a trait, because the user won't be able to easily remove the `async`, as this will then not match with the function signature in the trait definition changelog: [`unused_async`]: don't lint if function is part of a trait
2 parents 36f4feb + b713cd5 commit 750c7a1

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

clippy_lints/src/unused_async.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::is_def_id_trait_method;
23
use rustc_hir::intravisit::{walk_body, walk_expr, walk_fn, FnKind, Visitor};
34
use rustc_hir::{Body, Expr, ExprKind, FnDecl, YieldSource};
45
use rustc_lint::{LateContext, LateLintPass};
@@ -91,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
9192
span: Span,
9293
def_id: LocalDefId,
9394
) {
94-
if !span.from_expansion() && fn_kind.asyncness().is_async() {
95+
if !span.from_expansion() && fn_kind.asyncness().is_async() && !is_def_id_trait_method(cx, def_id) {
9596
let mut visitor = AsyncFnVisitor {
9697
cx,
9798
found_await: false,

clippy_utils/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol)
327327
.map_or(false, |did| is_diag_trait_item(cx, did, diag_item))
328328
}
329329

330+
/// Checks if the `def_id` belongs to a function that is part of a trait impl.
331+
pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
332+
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
333+
&& let Node::Item(item) = cx.tcx.hir().get_parent(hir_id)
334+
&& let ItemKind::Impl(imp) = item.kind
335+
{
336+
imp.of_trait.is_some()
337+
} else {
338+
false
339+
}
340+
}
341+
330342
/// Checks if the given expression is a path referring an item on the trait
331343
/// that is marked with the given diagnostic item.
332344
///

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)