rust-lang / rust-lang/rust-clippy
New lint: `out_of_bound_slice_operation`
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
Lint against uses of copy_from_slice, clone_from_slice and swap_with_slice that are guaranteed to panic.
This could also be made part of out_of_bounds_indexing but there is precedent for having this be a separate, warn-by-default lint with iter_out_of_bounds.
Advantage
- Remove code that is obviously erroneous
Drawbacks
No response
Example
The following would be linted (including the clone and swap versions) because it will always panic:
[1u8;10].copy_from_slice(&[1u8; 9]);
let mut a = [1u8;10];
let b = [1u8;9];
a.copy_from_slice(&b);
Ideally it would also lint cases where there is a simple slicing operation before the call:
let mut a = [1u8;10];
let b = [1u8;9];
a[..8].copy_from_slice(&b[..6]);
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 locating the existing out_of_bounds_indexing and iter_out_of_bounds lints, then compare how they handle related guaranteed errors. The work is done when copy_from_slice, clone_from_slice, and swap_with_slice calls that must panic are linted, including the described simple slicing case if 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
- 45/100