rust-lang / rust-lang/rust-clippy

Use slices or arrays instead of vecs lint

Open
#9,262 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.