rust-lang / rust-lang/rust-clippy
Use slices or arrays instead of vecs lint
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation files, tests, or entry points are mentioned. Start from the use_slice_or_array description and its Rust examples, then determine the short-literal threshold and the expected suggestion behavior; done means the lint handles suitable vec! literals without firing for longer ones.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100