diff options
| author | koka <koka.code@gmail.com> | 2022-11-11 00:14:18 +0900 |
|---|---|---|
| committer | koka <koka.code@gmail.com> | 2022-11-11 00:14:18 +0900 |
| commit | 2bc04bdac26fbb49e3b4ef3673f6a5143df307e7 (patch) | |
| tree | 54d071d92194121436302aed9370a4853b9faa56 /clippy_lints/src/cognitive_complexity.rs | |
| parent | 432baf7026aa896cfe3d44705537879ab2202bfb (diff) | |
| download | rust-2bc04bdac26fbb49e3b4ef3673f6a5143df307e7.tar.gz rust-2bc04bdac26fbb49e3b4ef3673f6a5143df307e7.zip | |
fix: cognitive_complexity for async fn
Diffstat (limited to 'clippy_lints/src/cognitive_complexity.rs')
| -rw-r--r-- | clippy_lints/src/cognitive_complexity.rs | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index 77af3b53d63..1c3a89a9782 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -4,11 +4,11 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::source::snippet_opt; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::visitors::for_each_expr; -use clippy_utils::LimitStack; +use clippy_utils::{get_async_fn_body, is_async_fn, LimitStack}; use core::ops::ControlFlow; use rustc_ast::ast::Attribute; use rustc_hir::intravisit::FnKind; -use rustc_hir::{Body, ExprKind, FnDecl, HirId}; +use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; @@ -56,15 +56,13 @@ impl CognitiveComplexity { cx: &LateContext<'tcx>, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, - body: &'tcx Body<'_>, + expr: &'tcx Expr<'_>, body_span: Span, ) { if body_span.from_expansion() { return; } - let expr = body.value; - let mut cc = 1u64; let mut returns = 0u64; let _: Option<!> = for_each_expr(expr, |e| { @@ -146,7 +144,18 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity { ) { let def_id = cx.tcx.hir().local_def_id(hir_id); if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) { - self.check(cx, kind, decl, body, span); + let expr = if is_async_fn(kind) { + match get_async_fn_body(cx.tcx, body) { + Some(b) => b, + None => { + return; + }, + } + } else { + body.value + }; + + self.check(cx, kind, decl, expr, span); } } |
