Skip to content

Add a lint for when ranges are used inside of a VecΒ #10932

Closed
@orowith2os

Description

@orowith2os

What it does

When someone goes to put a range inside of a Vec, Clippy should probably check with them to make sure that's what they actually want to do. They may want to go over the range and collect that all into a Vec.

Advantage

  • New programmers to Rust (especially those using Rustlings) can get more information for why using a vec![0..100] doesn't work.
  • Prevents using the wrong type on accident inside of a Vec.

Drawbacks

None AFAIK, aside from needing to disable it for valid use cases.

Example

let a = [0..200];

You may want to make a Vec of integers, with the length specified here, not a Vec of ranges. Here are some potential solutions:

  • Initialize a Vec of length 200, with each value set to 0.
let a = vec![0; 200]
  • Initialize a Vec, containing the values from 1 to 200.
let a = (0..200).collect::<Vec<i32>>()

Alternatively, you may want to make an Array instead, with each value initialized to 0:

let a = [0..200]

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions