rust-lang / rust-lang/rust-clippy

Warn on all slice methods that are panicky

Open
#10,024 0 comments 2 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

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_at and friends like slice::split_at_mut, slice::split_array_ref, ...
  • slice::rotate_left and slice::rotate_right
  • slice::swap
Lint Name

unchecked_index_out_of_bounds

Category

restriction

Advantage
  • clippy::indexing_slicing covers 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_bounds means 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_slicing already 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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.