rust-lang / rust-lang/rust-clippy

New lint: comment line density for files and functions

Open
#13,402 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

The lint counts the number of lines of the file and count the number of comments in the source file and do a percentage
if the percentage is bellow a specific threshold a warning is raised

In tools like sonarqube it corresponds to comment_lines_density https://docs.sonarsource.com/sonarqube/latest/user-guide/code-metrics/metrics-definition/

Comment Density (%) = (Number of Comment Lines / (Total Lines - Empty Lines)) × 100

Advantage

It is important for some industries (like space industry), some coding standards request it

Drawbacks

This metric is generally not relevant and we want developers to think about relevant comments instead of number of lines of comments, but as said in Advantage it is something requested by standards

Example
<code>
// Calculate nth Fibonacci number
fn fibonacci(n: u32) -> u32 {
    if n <= 1 {
        return n;
    }

    let mut a = 0;
    let mut b = 1;

    for _ in 2..=n {
        let next = a + b;
        a = b;
        b = next;
    }

    b
}

fn main() {
    // Test Fibonacci
    let n = 10;
    println!("Fibonacci of {} is {}", n, fibonacci(n));
}

Could be written as with 52 % (13 of 25) of comments:

<code>
// This function calculates the nth Fibonacci number
fn fibonacci(n: u32) -> u32 {
    // Base case: If n is 0 or 1, return n
    if n <= 1 {
        return n;
    }

    // Initialize variables for the iterative calculation
    let mut a = 0; // This will hold the (n-2)th Fibonacci number
    let mut b = 1; // This will hold the (n-1)th Fibonacci number

    // Loop from 2 to n to calculate the nth Fibonacci number
    for _ in 2..=n {
        // Calculate the next Fibonacci number in the sequence
        let next = a + b;

        // Update a and b for the next iteration
        a = b; // Shift b to a
        b = next; // Set b to the newly calculated Fibonacci number
    }

    // Return the nth Fibonacci number
    b
}

fn main() {
    // Example usage: Calculate and print the 10th Fibonacci number
    let n = 10; // Change this value to calculate a different Fibonacci number
    println!("Fibonacci of {} is {}", n, fibonacci(n));
}

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

No implementation files, tests, or entry points are named. Start by clarifying whether the lint must measure whole files, individual functions, or both, and how Rust comment forms and empty lines are counted. Done should include an agreed threshold configuration, warnings for below-threshold density, and coverage for the Fibonacci examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.