rust-lang / rust-lang/rust-clippy

vec -> array lint for simple cases

Open
#6,618 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

In many blog posts online I see code like:

fn main() {
    let a = vec![10, 20, 30];
    let tot: u32 = a.iter().sum();
    println!("Total: {}", tot);
}

I'd like a Clippy lint that suggests to write instead:

fn main() {
    let a = [10, 20, 30];
    let tot: u32 = a.iter().sum();
    println!("Total: {}", tot);
}

That is, to suggest to use arrays in simple cases where dynamic arrays aren't necessary. Even if such lint is very conservative it's still going to be useful to teach inexperienced Rust programmers to avoid heap allocations (Clippy is also a learning tool today). Rust programmers later could remember the idea and generalize it.

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

The issue names no implementation files, tests, or entry points. Start by locating Clippy's lint and test conventions, then define conservative cases based on the vec! and array examples; done means those simple cases receive a tested suggestion without affecting cases that need dynamic allocation.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.