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

Needless call of to_vec() on temporary Vec #7908

Open
Kixunil opened this issue Oct 31, 2021 · 1 comment
Open

Needless call of to_vec() on temporary Vec #7908

Kixunil opened this issue Oct 31, 2021 · 1 comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@Kixunil
Copy link

Kixunil commented Oct 31, 2021

What it does

When to_vec() is called on temporary Vec<_> the lint should warn that to_vec() is not needed and causes extra allocation. (Suggesting removing it.)

Categories

  • Kind: performance

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

  • One less allocation
  • More concise code (the returned value is already a Vec<_>)

Drawbacks

None.

Example

(playground)

fn create_vec() -> Vec<u8> {
    vec![1, 2, 42]
}

fn main() {
    println!("{:?}", create_vec().to_vec());
}

Could be written as:

fn create_vec() -> Vec<u8> {
    vec![1, 2, 42]
}

fn main() {
    println!("{:?}", create_vec());
}
@Kixunil Kixunil added the A-lint Area: New lints label Oct 31, 2021
@camsteffen
Copy link
Contributor

This can be an enhancement of redundant_clone. And it should share logic with implicit_clone.

@camsteffen camsteffen added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages and removed A-lint Area: New lints labels Nov 3, 2021
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
Projects
None yet
Development

No branches or pull requests

2 participants