rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Lint idea: Collection is only added to, never read

Open schubart opened this issue 2 years ago • 2 comments

What it does

Adding to a collection and never using it is pointless and might indicate that the developer forgot to do something with the collection.

Currently this does not raise warnings:

#![warn(clippy::all, clippy::pedantic)]

fn main() {
    let mut numbers = Vec::new();
    for number in 1..10 {
        numbers.push(number);
    }
}

The new lint would point out that numbers is never read.

To fix the warning, one could delete all code in main or do something with numbers, such as printing it.

What do people think? If this sounds useful, I'd like to do it as my first contribution.

Lint Name

collection_is_never_read

Category

suspicious

Advantage

The new code no longer unnecessarily does the work of updating the collection.

Drawbacks

No response

Example

<code>

Could be written as:

#![warn(clippy::all, clippy::pedantic)]

fn main() {
    let mut numbers = Vec::new();
    for number in 1..10 {
        numbers.push(number);
    }
    println!("The numbers are: {numbers:?}");
}

schubart avatar Jul 31 '22 11:07 schubart

Note that this lint should only apply to sets if the insert result is unused.

llogiq avatar Jul 31 '22 19:07 llogiq

@rustbot claim

schubart avatar Jul 31 '22 21:07 schubart