Skip to content

False positive occuring with cognitive_complexity on macros #7905

@edward-shen

Description

@edward-shen

Lint name: cognitive_complexity

I tried this code (playground):

#![warn(clippy::cognitive_complexity)]

use tracing::info; // 0.1.26

fn main() {
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
    info!("Hello world!");
}

I expected to see this happen: To not get a cognitive complexity lint.

Instead, this happened: I receive the following lint:

warning: the function has a cognitive complexity of (27/25)
 --> src/main.rs:5:4
  |
5 | fn main() {
  |    ^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::cognitive_complexity)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: you could split it up into multiple smaller functions

Meta

Rust version (rustc -Vv):

rustc 1.58.0-nightly (efd048394 2021-10-20)
binary: rustc
commit-hash: efd0483949496b067cd5f7569d1b28cd3d5d3c72
commit-date: 2021-10-20
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

Looks like the lint is expanding the macros out before evaluating cognitive complexity which is causing this to misfire. Suggestions would be to not expand macros when determining cognitive complexity or treating externally defined macros as a normal function call when determining cognitive complexity.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions