Skip to content

Commit 4f1698d

Browse files
committed
Auto merge of #9828 - koka831:fix/9300, r=Alexendoo
fix: cognitive_complexity for async fn fix rust-lang/rust-clippy#9300 changelog: [`cognitive_complexity`] support async fn
2 parents 432baf7 + 2bc04bd commit 4f1698d

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

clippy_lints/src/cognitive_complexity.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use clippy_utils::diagnostics::span_lint_and_help;
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::is_type_diagnostic_item;
66
use clippy_utils::visitors::for_each_expr;
7-
use clippy_utils::LimitStack;
7+
use clippy_utils::{get_async_fn_body, is_async_fn, LimitStack};
88
use core::ops::ControlFlow;
99
use rustc_ast::ast::Attribute;
1010
use rustc_hir::intravisit::FnKind;
11-
use rustc_hir::{Body, ExprKind, FnDecl, HirId};
11+
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId};
1212
use rustc_lint::{LateContext, LateLintPass, LintContext};
1313
use rustc_session::{declare_tool_lint, impl_lint_pass};
1414
use rustc_span::source_map::Span;
@@ -56,15 +56,13 @@ impl CognitiveComplexity {
5656
cx: &LateContext<'tcx>,
5757
kind: FnKind<'tcx>,
5858
decl: &'tcx FnDecl<'_>,
59-
body: &'tcx Body<'_>,
59+
expr: &'tcx Expr<'_>,
6060
body_span: Span,
6161
) {
6262
if body_span.from_expansion() {
6363
return;
6464
}
6565

66-
let expr = body.value;
67-
6866
let mut cc = 1u64;
6967
let mut returns = 0u64;
7068
let _: Option<!> = for_each_expr(expr, |e| {
@@ -146,7 +144,18 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity {
146144
) {
147145
let def_id = cx.tcx.hir().local_def_id(hir_id);
148146
if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) {
149-
self.check(cx, kind, decl, body, span);
147+
let expr = if is_async_fn(kind) {
148+
match get_async_fn_body(cx.tcx, body) {
149+
Some(b) => b,
150+
None => {
151+
return;
152+
},
153+
}
154+
} else {
155+
body.value
156+
};
157+
158+
self.check(cx, kind, decl, expr, span);
150159
}
151160
}
152161

tests/ui/cognitive_complexity.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,11 @@ impl Moo {
393393
}
394394
}
395395
}
396+
397+
#[clippy::cognitive_complexity = "1"]
398+
mod issue9300 {
399+
async fn a() {
400+
let a = 0;
401+
if a == 0 {}
402+
}
403+
}

tests/ui/cognitive_complexity.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,13 @@ LL | fn moo(&self) {
135135
|
136136
= help: you could split it up into multiple smaller functions
137137

138-
error: aborting due to 17 previous errors
138+
error: the function has a cognitive complexity of (2/1)
139+
--> $DIR/cognitive_complexity.rs:399:14
140+
|
141+
LL | async fn a() {
142+
| ^
143+
|
144+
= help: you could split it up into multiple smaller functions
145+
146+
error: aborting due to 18 previous errors
139147

0 commit comments

Comments
 (0)