rust-lang / rust-lang/rust-clippy
Lint for Unnecessary Manual Bounds Checking
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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