Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint idea: Collection is only added to, never read #9267

Closed
schubart opened this issue Jul 31, 2022 · 2 comments · Fixed by #10415
Closed

Lint idea: Collection is only added to, never read #9267

schubart opened this issue Jul 31, 2022 · 2 comments · Fixed by #10415
Assignees
Labels
A-lint Area: New lints

Comments

@schubart
Copy link
Contributor

schubart commented Jul 31, 2022

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 schubart added the A-lint Area: New lints label Jul 31, 2022
@llogiq
Copy link
Contributor

llogiq commented Jul 31, 2022

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

@schubart
Copy link
Contributor Author

@rustbot claim

bors added a commit that referenced this issue Oct 2, 2022
Fix sorting in ` cargo dev update_lints` script

changelog: none

The old code cloned and sorted `usable_lints` into `sorted_usable_lints`, but then failed to do anything with `sorted_usable_lints`.

This was discovered by my new `collection_is_never_read` lint (#9267) that I'm working on!

Fix: I renamed the sorted vector to `usable_lints`.  Therefore it now gets used where the unsorted one was used previously.
@bors bors closed this as completed in 41fa24c Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants