rust-lang / rust-lang/rust-clippy

Lint for Unnecessary Manual Bounds Checking

Open
#1,008 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I ran across a section of translated C code that was written as such to manually avoid accessing a value out of bounds and returning a default value if it is out of bounds:

let hash_value = if pos + ZOPFLI_MIN_MATCH <= array.len() {
    array[pos + ZOPFLI_MIN_MATCH - 1]
} else {
    0
};

However, it would be better to detect this behaviour and recommend the following:

let hash_value = array.iter().nth(pos + ZOPFLI_MIN_MATCH - 1).cloned().unwrap_or(0);

Or even the following for more complicated mapping.

let hash_value = array.iter().nth(pos + ZOPFLI_MIN_MATCH - 1).map_or(0, |value| *value);

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

Start with the two Rust examples in the issue and inspect rust-clippy's existing lint entry points for bounds-checking patterns. Define the matching cases and verify that the lint recommends the iterator-based alternatives shown; the issue names no source file or test, so the appropriate locations must first be found in the repository.

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.