Skip to content

Improve the cognitive_complexity lint to show what lines are causing the complexity #4470

Open
@iddm

Description

@iddm

We could improve the cognitive_complexity lint further by showing the ^ sign under tokens which cause the increment of cognitive complexity. Doing so would let users know what actually was causing the complexity and so they would spend their time more effectively fixing the issue.

fn f(a: u8) {
    if a == 0 {
    } else if a == 3 {
    } else {
    }
}
warning: the function has a cognitive complexity of (3/2)
  --> src/main.rs:12:1
   |
12 | / fn f(a: u8) {
13 | |     if a == 0 {
14 | |     } else if a == 3 {
15 | |     } else {
16 | |     }
17 | | }
   | |_^
   |
   = 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

note: these places cause additional cognitive complexity
   |
1 | /     fn f(a: u8) {
2 | |         if a == 0 {
  | |_________^ increments to 1

   |
1 | /     fn f(a: u8) {
2 | |         if a == 0 {
3 | |         } else if a == 3 {
  | |___________^ increments to 2

   |
1 | /     fn f(a: u8) {
2 | |         if a == 0 {
... |
4 | |         } else {
  | |___________^ increments to 3

Right now, for people unfamiliar with the cognitive complexity, it is impossible to know for sure what is causing problems in their code, they have to study the paper. Also, even people who had it known before the problem appeared, could simply have forgotten it. This explicit show will not only help them understand what causes a problem here but also will teach them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-an-interesting-projectCategory: Interesting projects, that usually are more involved design/code wise.C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-nurseryLint: Currently in the nursery group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions