Skip to content

cognitive_complexity lint includes code expanded from macro in calculated complexity #14417

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

Open
KonaeAkira opened this issue Mar 16, 2025 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@KonaeAkira
Copy link
Contributor

Summary

Code expanded from macros should not count towards cognitive complexity. Instead, a macro call should have the same cognitive complexity as a function call.

Lint Name

cognitive_complexity

Reproducer

I tried this code:

#![warn(clippy::cognitive_complexity)]

// exact content of the macro is not important
macro_rules! create_4d_vec {
    ( $dim_1:literal, $dim_2:literal, $dim_3:literal, $dim_4:literal) => {
        let mut vec = vec![vec![vec![vec![0; $dim_4]; $dim_3]; $dim_2]; $dim_1];
        for a in 0..$dim_1 {
            for b in 0..$dim_2 {
                for c in 0..$dim_3 {
                    for d in 0..$dim_4 {
                        vec[a][b][c][d] = a + b + c + d;
                    }
                }
            }
        }
    };
}

fn foo() {
    create_4d_vec!(1, 2, 3, 4);
    create_4d_vec!(1, 2, 3, 4);
    create_4d_vec!(1, 2, 3, 4);
    create_4d_vec!(1, 2, 3, 4);
    create_4d_vec!(1, 2, 3, 4);
    create_4d_vec!(1, 2, 3, 4);
    create_4d_vec!(1, 2, 3, 4);
}

I saw this happen:

warning: the function has a cognitive complexity of (29/25)
  --> src/main.rs:18:4
   |
18 | fn foo() {
   |    ^^^
   |
   = help: you could split it up into multiple smaller functions
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity

I expected to see this happen:

Lint does not trigger.

Version

rustc 1.87.0-nightly (ecade534c 2025-03-14)
binary: rustc
commit-hash: ecade534c66478c51c5d3c1d3682dc4beb0ac972
commit-date: 2025-03-14
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

@rustbot label +L-nursery

@KonaeAkira KonaeAkira added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

1 participant