Skip to content

Commit

Permalink
Auto merge of rust-lang#9025 - Alexendoo:unused-async-method, r=dswij
Browse files Browse the repository at this point in the history
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 rust-lang#9024
  • Loading branch information
bors committed Jun 20, 2022
2 parents 195f2cb + a0b107b commit 97d4513
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
28 changes: 13 additions & 15 deletions clippy_lints/src/unused_async.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor};
use rustc_hir::{Body, Expr, ExprKind, FnDecl, FnHeader, HirId, IsAsync, YieldSource};
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId, IsAsync, YieldSource};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -68,20 +68,18 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
span: Span,
hir_id: HirId,
) {
if let FnKind::ItemFn(_, _, FnHeader { asyncness, .. }) = &fn_kind {
if matches!(asyncness, IsAsync::Async) {
let mut visitor = AsyncFnVisitor { cx, found_await: false };
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), span, hir_id);
if !visitor.found_await {
span_lint_and_help(
cx,
UNUSED_ASYNC,
span,
"unused `async` for function with no await statements",
None,
"consider removing the `async` from this function",
);
}
if !span.from_expansion() && fn_kind.asyncness() == IsAsync::Async {
let mut visitor = AsyncFnVisitor { cx, found_await: false };
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), span, hir_id);
if !visitor.found_await {
span_lint_and_help(
cx,
UNUSED_ASYNC,
span,
"unused `async` for function with no await statements",
None,
"consider removing the `async` from this function",
);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
clippy::use_self,
clippy::useless_format,
clippy::wrong_self_convention,
clippy::unused_async,
clippy::unused_self,
unused
)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/methods.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: methods called `new` usually return `Self`
--> $DIR/methods.rs:103:5
--> $DIR/methods.rs:104:5
|
LL | / fn new() -> i32 {
LL | | 0
Expand All @@ -9,7 +9,7 @@ LL | | }
= note: `-D clippy::new-ret-no-self` implied by `-D warnings`

error: called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(..)` instead
--> $DIR/methods.rs:124:13
--> $DIR/methods.rs:125:13
|
LL | let _ = v.iter().filter(|&x| {
| _____________^
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/should_impl_trait/corner_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
clippy::missing_safety_doc,
clippy::wrong_self_convention,
clippy::missing_panics_doc,
clippy::return_self_not_must_use
clippy::return_self_not_must_use,
clippy::unused_async
)]

use std::ops::Mul;
Expand Down
34 changes: 34 additions & 0 deletions tests/ui/unused_async.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![warn(clippy::unused_async)]

use std::future::Future;
use std::pin::Pin;

async fn foo() -> i32 {
4
}
Expand All @@ -8,6 +11,37 @@ async fn bar() -> i32 {
foo().await
}

struct S;

impl S {
async fn unused(&self) -> i32 {
1
}

async fn used(&self) -> i32 {
self.unused().await
}
}

trait AsyncTrait {
fn trait_method() -> Pin<Box<dyn Future<Output = i32>>>;
}

macro_rules! async_trait_impl {
() => {
impl AsyncTrait for S {
fn trait_method() -> Pin<Box<dyn Future<Output = i32>>> {
async fn unused() -> i32 {
5
}

Box::pin(unused())
}
}
};
}
async_trait_impl!();

fn main() {
foo();
bar();
Expand Down
14 changes: 12 additions & 2 deletions tests/ui/unused_async.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unused `async` for function with no await statements
--> $DIR/unused_async.rs:3:1
--> $DIR/unused_async.rs:6:1
|
LL | / async fn foo() -> i32 {
LL | | 4
Expand All @@ -9,5 +9,15 @@ LL | | }
= note: `-D clippy::unused-async` implied by `-D warnings`
= help: consider removing the `async` from this function

error: aborting due to previous error
error: unused `async` for function with no await statements
--> $DIR/unused_async.rs:17:5
|
LL | / async fn unused(&self) -> i32 {
LL | | 1
LL | | }
| |_____^
|
= help: consider removing the `async` from this function

error: aborting due to 2 previous errors

0 comments on commit 97d4513

Please sign in to comment.