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

Concisely creating a vector of a known capacity #11330

Closed
ktnlvr opened this issue Aug 12, 2023 · 1 comment · Fixed by #11373
Closed

Concisely creating a vector of a known capacity #11330

ktnlvr opened this issue Aug 12, 2023 · 1 comment · Fixed by #11373
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@ktnlvr
Copy link

ktnlvr commented Aug 12, 2023

What it does

Informs the user about a more concise way to create a vector with a known capacity.

I often wrote code like the following:

{ 
    let mut v = vec![];
    v.reserve(space_hint);
    v
}

since I didn't know about the existence of Vec::with_capacity, maybe clippy can warn about things like that? It also seems like this can be generalized to cases when we are pushing into an empty vector for a known amount of times.

Advantage

No response

Drawbacks

No response

Example

{ 
    let mut v = vec![];
    v.reserve(space_hint);
    v
}

Could be written as:

Vec::with_capacity(space_hint)
@ktnlvr ktnlvr added the A-lint Area: New lints label Aug 12, 2023
@Alexendoo Alexendoo added the good-first-issue These issues are a good way to get started with Clippy label Aug 13, 2023
@chansuke
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 good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
3 participants