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

Use slices or arrays instead of vecs lint #9262

Open
leonardo-m opened this issue Jul 29, 2022 · 0 comments
Open

Use slices or arrays instead of vecs lint #9262

leonardo-m opened this issue Jul 29, 2022 · 0 comments
Labels
A-lint Area: New lints

Comments

@leonardo-m
Copy link

What it does

The lint suggests to replace an allocated vec![] with a slice. In test blocks I see lot of vecs that aren't necessary.

Lint Name

use_slice_or_array

Category

perf

Advantage

It's more efficient.

Drawbacks

None, I think.

Example

#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

fn append(mut items: Vec<i32>, suffix: &[i32]) -> Vec<i32> {
    items.extend_from_slice(suffix);
    items
}

fn main() {
    assert_eq!(vec![1, 2, 3, 4], append(vec![1, 2], &[3, 4]));
}

Could be written as:

#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

fn append(mut items: Vec<i32>, suffix: &[i32]) -> Vec<i32> {
    items.extend_from_slice(suffix);
    items
}

fn main() {
    assert_eq!(&[1, 2, 3, 4][..], append(vec![1, 2], &[3, 4]));
}

This lint should suggest to use slices or arrays instead of probably-heap-allocated vecs. But only for short literals. Past a certain length better not to fire this lint.

@leonardo-m leonardo-m added the A-lint Area: New lints label Jul 29, 2022
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

1 participant