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 Iterator::count instead of collecting + len #7110

Closed
Luro02 opened this issue Apr 19, 2021 · 5 comments · Fixed by #7163
Closed

Suggest Iterator::count instead of collecting + len #7110

Luro02 opened this issue Apr 19, 2021 · 5 comments · Fixed by #7163
Assignees
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience.

Comments

@Luro02
Copy link

Luro02 commented Apr 19, 2021

What it does

Suggests the use of Iterator::count instead of collecting into a collection and then calling its len function.

Categories (optional)

  • Kind: clippy::complexity

What is the advantage of the recommended code over the original code

  • Simpler to read
  • No Heap Allocations

Drawbacks

None.

Example

fn count(string: &str) -> usize {
    let buffer: Vec<&str> = string.split('/').collect();
    buffer.len()
}

Could be written as:

fn count_rewritten(string: &str) -> usize {
    string.split('/').count()
}

playground

@Luro02 Luro02 added the A-lint Area: New lints label Apr 19, 2021
@llogiq
Copy link
Contributor

llogiq commented Apr 19, 2021

That should be caught by needless_collect.

@camsteffen camsteffen added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience. and removed A-lint Area: New lints labels May 3, 2021
@mgacek8
Copy link
Contributor

mgacek8 commented May 3, 2021

@rustbot claim

@llogiq
Copy link
Contributor

llogiq commented May 4, 2021

Note that this should only lint for collections where the len() actually equals the count(). Vec and VecDeque as well as BinaryHeap. Notably, neither the *Sets nor Strings uphold this equality.

@mgacek8
Copy link
Contributor

mgacek8 commented May 4, 2021

Note that this should only lint for collections where the len() actually equals the count().

Yep, that's right. Currently this lint (in case of indirect usage, as in the above example) respects Vec, VecDeque and LinkedList.
The problem is that it doesn't work when a type annotation is used.

@mgacek8
Copy link
Contributor

mgacek8 commented May 4, 2021

as well as BinaryHeap

BinaryHeap is missing, so I'll add that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience.
Projects
None yet
4 participants