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_vec: Vec::append or Vec::extend using vec!. #13461

Open
jakubdabek opened this issue Sep 26, 2024 · 1 comment
Open

needless_vec: Vec::append or Vec::extend using vec!. #13461

jakubdabek opened this issue Sep 26, 2024 · 1 comment
Labels
A-lint Area: New lints

Comments

@jakubdabek
Copy link

What it does

Detects code that uses Vec::append or Vec::extend with a newly-created vec!.

Advantage

  • Removes unnecessary allocations
  • Clearer intent with push instead of append/extend

Drawbacks

Should there be a separate lint for some_vec.extend([single_elem]) to use push instead? This lint could create code that would require linting again.

Example

some_vec.append(&mut vec![elem]); // 1
some_vec.extend(vec![elem]); // 2
some_vec.append(&mut vec![elem1, elem2]); // 3
some_vec.extend(vec![elem1, elem2]); // 4
// 5:
let mut v = vec![elem1, elem2];
some_vec.append(&mut v);

Could be written as:

some_vec.push(elem); // 1 and 2
some_vec.extend([elem1, elem2]); // 3 and 4
// 5
let v = [elem1, elem2];
some_vec.extend(v);
@jakubdabek jakubdabek added the A-lint Area: New lints label Sep 26, 2024
@lolbinarycat
Copy link

don't forget extend_from_slice.

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

No branches or pull requests

2 participants