Skip to content

Fix is_async_fn to check FnKind::Method #9836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,11 @@ pub fn if_sequence<'tcx>(mut expr: &'tcx Expr<'tcx>) -> (Vec<&'tcx Expr<'tcx>>,

/// Checks if the given function kind is an async function.
pub fn is_async_fn(kind: FnKind<'_>) -> bool {
matches!(kind, FnKind::ItemFn(_, _, header) if header.asyncness == IsAsync::Async)
match kind {
FnKind::ItemFn(_, _, header) => header.asyncness == IsAsync::Async,
FnKind::Method(_, sig) => sig.header.asyncness == IsAsync::Async,
FnKind::Closure => false,
}
}

/// Peels away all the compiler generated code surrounding the body of an async function,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/cognitive_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,12 @@ mod issue9300 {
let a = 0;
if a == 0 {}
}

pub struct S;
impl S {
pub async fn async_method() {
let a = 0;
if a == 0 {}
}
}
}
10 changes: 9 additions & 1 deletion tests/ui/cognitive_complexity.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,13 @@ LL | async fn a() {
|
= help: you could split it up into multiple smaller functions

error: aborting due to 18 previous errors
error: the function has a cognitive complexity of (2/1)
--> $DIR/cognitive_complexity.rs:406:22
|
LL | pub async fn async_method() {
| ^^^^^^^^^^^^
|
= help: you could split it up into multiple smaller functions

error: aborting due to 19 previous errors