1 //! calculate cognitive complexity and warn about overly complex functions
3 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
6 use rustc::{declare_tool_lint, impl_lint_pass};
7 use syntax::ast::Attribute;
8 use syntax::source_map::Span;
10 use crate::utils::{match_type, paths, span_help_and_lint, LimitStack};
12 declare_clippy_lint! {
13 /// **What it does:** Checks for methods with high cognitive complexity.
15 /// **Why is this bad?** Methods of high cognitive complexity tend to be hard to
16 /// both read and maintain. Also LLVM will tend to optimize small methods better.
18 /// **Known problems:** Sometimes it's hard to find a way to reduce the
21 /// **Example:** No. You'll see it when you get the warning.
22 pub COGNITIVE_COMPLEXITY,
24 "functions that should be split up into multiple functions"
27 pub struct CognitiveComplexity {
31 impl CognitiveComplexity {
33 pub fn new(limit: u64) -> Self {
35 limit: LimitStack::new(limit),
40 impl_lint_pass!(CognitiveComplexity => [COGNITIVE_COMPLEXITY]);
42 impl CognitiveComplexity {
43 fn check<'a, 'tcx>(&mut self, cx: &'a LateContext<'a, 'tcx>, body: &'tcx Body, span: Span) {
44 if span.from_expansion() {
48 let expr = &body.value;
50 let mut helper = CCHelper { cc: 1, returns: 0 };
51 helper.visit_expr(expr);
52 let CCHelper { cc, returns } = helper;
53 let ret_ty = cx.tables.node_type(expr.hir_id);
54 let ret_adjust = if match_type(cx, ret_ty, &paths::RESULT) {
57 #[allow(clippy::integer_division)]
62 // prevent degenerate cases where unreachable code contains `return` statements
63 if rust_cc >= ret_adjust {
64 rust_cc -= ret_adjust;
66 if rust_cc > self.limit.limit() {
72 "the function has a cognitive complexity of ({}/{})",
76 "you could split it up into multiple smaller functions",
82 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CognitiveComplexity {
85 cx: &LateContext<'a, 'tcx>,
86 _: intravisit::FnKind<'tcx>,
92 let def_id = cx.tcx.hir().local_def_id(hir_id);
93 if !cx.tcx.has_attr(def_id, sym!(test)) {
94 self.check(cx, body, span);
98 fn enter_lint_attrs(&mut self, cx: &LateContext<'a, 'tcx>, attrs: &'tcx [Attribute]) {
99 self.limit.push_attrs(cx.sess(), attrs, "cognitive_complexity");
101 fn exit_lint_attrs(&mut self, cx: &LateContext<'a, 'tcx>, attrs: &'tcx [Attribute]) {
102 self.limit.pop_attrs(cx.sess(), attrs, "cognitive_complexity");
111 impl<'tcx> Visitor<'tcx> for CCHelper {
112 fn visit_expr(&mut self, e: &'tcx Expr) {
115 ExprKind::Match(_, ref arms, _) => {
119 self.cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
121 ExprKind::Ret(_) => self.returns += 1,
125 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
126 NestedVisitorMap::None