Skip to content

If indexing slice more than once, suggest asserting length beforehand #8296

Closed
@frewsxcv

Description

@frewsxcv

What it does

If you assert before indexing, Rust will remove the bounds checks on the indexings.

An example: https://godbolt.org/z/fczP9YTbj (correctly optimizes at opt-level=1 and higher)

Lint Name

multiple-indexing-assert-length

Category

No response

Advantage

  • Improved performance

Drawbacks

No response

Example

let _ = x[0]; // bounds check here
let _ = x[1..4]; // bounds check here

Could be written as:

assert!(x.len() >= 10);

let _ = x[0]; // no bounds check here
let _ = x[1..4]; // or here

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