rust-lang / rust-lang/rust-clippy
Warn on all slice methods that are panicky
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
This would be like clippy::indexing_slicing except that it would complain about all methods that are panicky based on an index out of bounds, not just the indexing operator.
Examples of methods that would be captured:
slice::split_atand friends likeslice::split_at_mut,slice::split_array_ref, ...slice::rotate_leftandslice::rotate_rightslice::swap
Lint Name
unchecked_index_out_of_bounds
Category
restriction
Advantage
clippy::indexing_slicingcovers many cases where it's desirable to avoid panics, but it is limited- From experience reviewing code, index-out-of-bounds is a uniquely common class of errors, more so than other panic conditions
- A generic name like
unchecked_index_out_of_boundsmeans that we could easily add more functions to the lint in the future
Drawbacks
- Slippery slope: where do we draw the line for what panics should be linted?
clippy::indexing_slicingalready exists, and perhaps a second, very similar lint would be confusing; should we add these functions to the same lint?
Example
let foo = my_slice.split_at(i);
Could be written as:
let foo = my_slice.checked_split_at(i).ok_or(...)?;
Note: checked_split_at doesn't exist yet, so in the mean time, code could be written as
if i > my_slice.len() {
return Err(...);
}
#[allow(clippy::unchecked_index_out_of_bounds)] // checked above
let foo = my_slice.split_at(i);
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 by comparing the proposed lint with clippy::indexing_slicing and review the listed slice methods, including split_at, rotate_left, rotate_right, and swap. Done means agreeing whether this should be a separate lint or an extension of the existing one, defining its scope, and accounting for the absence of checked_split_at.
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
- 30/100