Skip to content

vec -> array lint for simple cases #6618

Open
@leonardo-m

Description

@leonardo-m

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.T-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions