rust-lang / rust-lang/rust-clippy
new lint: vector access via index after is_empty()
Open
Nobody has claimed this yet.
A-lint
L-correctness
T-MIR
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
#[cfg_attr(feature = "cargo-clippy", allow(pedantic))]
fn main() {
let v: Vec<i32> = Vec::new();
println!("{}", do_something(&v));
}
fn do_something(v: &[i32]) -> i32 {
if v.is_empty() {
// v must not be modified in between or be immutable
v[0] // BOOM <- catch this at linttime
} else {
124
}
}
// similar:
// if !v.is_empty() {
// //...
// } else {
// v[0]
// }
There are probably other std::collections where this can also be applied.
The idea of this ticket was inspired by cppcheck :)
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
No source file, test, or entry point is named. Start by reproducing the Rust examples in the issue and inspect existing Clippy lint implementations for comparable control-flow and indexing analysis. Done means the lint catches the shown is_empty/index patterns without false positives, with coverage for any additional std::collections cases that are supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100