about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-11-12 20:54:39 +0000
committerbors <bors@rust-lang.org>2022-11-12 20:54:39 +0000
commitbb326e761b51f6b07906871a8da9e076a7ffb2cd (patch)
treea39aa4b24e84feccdbd8b07bc7715656a00df2c9
parent755fe4dc24af549559fc9c32bc953eed7a3c86e4 (diff)
parent34c4520eae7adf1a3adf3fce5376eac2911d08b8 (diff)
downloadrust-bb326e761b51f6b07906871a8da9e076a7ffb2cd.tar.gz
rust-bb326e761b51f6b07906871a8da9e076a7ffb2cd.zip
Auto merge of #9836 - koka831:patch-9300, r=Alexendoo
Fix is_async_fn to check FnKind::Method

This is a follow-up PR of https://github.com/rust-lang/rust-clippy/pull/9828 to support also async methods.

changelog: [`cognitive_complexity`] support async method

r? `@Alexendoo`
-rw-r--r--clippy_utils/src/lib.rs6
-rw-r--r--tests/ui/cognitive_complexity.rs8
-rw-r--r--tests/ui/cognitive_complexity.stderr10
3 files changed, 22 insertions, 2 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs
index f7d3c91777e..ac4d0b89f17 100644
--- a/clippy_utils/src/lib.rs
+++ b/clippy_utils/src/lib.rs
@@ -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,
diff --git a/tests/ui/cognitive_complexity.rs b/tests/ui/cognitive_complexity.rs
index 498a9f0644f..07bdaff00dc 100644
--- a/tests/ui/cognitive_complexity.rs
+++ b/tests/ui/cognitive_complexity.rs
@@ -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 {}
+        }
+    }
 }
diff --git a/tests/ui/cognitive_complexity.stderr b/tests/ui/cognitive_complexity.stderr
index 208f4d7b347..5824631fa83 100644
--- a/tests/ui/cognitive_complexity.stderr
+++ b/tests/ui/cognitive_complexity.stderr
@@ -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