Skip to content

Clippy cognitive complexity false positive #553

@tcharding

Description

@tcharding

Bug Report

Version

tracing = "0.1"
cargo 1.40.0 (bc8e4c8be 2019-11-22)
clippy 0.0.212 (c8e3cfb 2019-10-28)

Platform

Linux ares 5.4.14-050414-generic #202001230832 SMP Thu Jan 23 08:34:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Description

When using tracing library to create an event in the match arms of an enum with six or more variants the Clippy 'cognitive complexity' warning is triggered.

Here is a minimal reproducer

fn main() {
    println!("Clippy cognitive complexity repro");

    fun(Run::There);
}

fn fun(r: Run) {
    match r {
        Run::Here => tracing::error!("error"),
        Run::There => tracing::error!("error"),
        Run::Everywhere => tracing::error!("error"),
        Run::Makes => tracing::error!("error"),
        Run::Clippy => tracing::error!("error"),
        Run::Choke => tracing::error!("error"),
    }
}

#[allow(dead_code)]
enum Run {
    Here,
    There,
    Everywhere,
    Makes,
    Clippy,
    Choke,
}

And Clippy gives the following warning:

cargo clippy
warning: the function has a cognitive complexity of (26/25)
  --> src/main.rs:7:1
   |
7  | / fn fun(r: Run) {
8  | |     match r {
9  | |         Run::Here => tracing::error!("error"),
10 | |         Run::There => tracing::error!("error"),
...  |
15 | |     }
16 | | }
   | |_^
   |
   = note: `#[warn(clippy::cognitive_complexity)]` on by default
   = 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

Removing the final variant (Choke) removes the warning.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions