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

Suggest Vec::retain() in cases of assigning a chain of .filter().collect() to the original variable. #8097

Closed
jwannebo opened this issue Dec 8, 2021 · 1 comment · Fixed by #8972
Assignees
Labels
A-lint Area: New lints

Comments

@jwannebo
Copy link

jwannebo commented Dec 8, 2021

What it does

Guides users to use Vec::retain() over various permutations of into_iter().filter().collect() if used to assign over the original vector.

Lint Name

vec_retain

Category

style, complexity, perf, pedantic

Advantage

Conciseness, readability, and reuses existing memory (in the .iter()...copied() case)

Drawbacks

Difficulty parsing out particularly complex chains of function calls.

Example

let mut my_vec = vec![0, 1, 2, 3, 4, 5];

my_vec = my_vec.into_iter().filter(|x| x % 2 == 0).collect();
// Or
my_vec = my_vec.iter().filter(|&x| x % 2 == 0).copied().collect();

Could be written as:

let mut my_vec = vec![0, 1, 2, 3, 4, 5];

my_vec.retain(|x| x % 2 == 0);
@jwannebo jwannebo added the A-lint Area: New lints label Dec 8, 2021
@kyoto7250
Copy link
Contributor

@rustbot claim

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