rust-lang / rust-lang/rust-clippy

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

Open
#4,470 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-an-interesting-project C-enhancement E-medium L-nursery
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Rust implementation and tests for the cognitive_complexity lint, then reproduce the issue with the example in the description. The work is done when diagnostics identify the tokens that add complexity with caret markers and increment values, while retaining the existing warning output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.