rust-lang / rust-lang/rust-clippy

New lint: `out_of_bound_slice_operation`

Open
#14,960 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.