rust-lang / rust-lang/rust-clippy

New lint suggestion: needles slicing of array

Open
#11,690 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

This lint would detect slicing of arrays looking like arr[..N] or arr[0..N] when arr is of type [T; N]. The only thing this can do is coerce the array to a slice, but that will be either done automatically by the compiler or be better done with as_slice/as_mut_slice.

This lint could also lint against arr[smth..N] to suggest using arr[smth..], but Is this desirable given that clippy currently doesn't even lint slice[0..smth] to suggest slice[..smth]? Maybe good for a second lint.

Advantage
  • Removes unnecessary syntax
Drawbacks

The slicing might force a coercion to a slice that makes the thing compile (or with generics could even change behaviour), forcing to use as_slice if removed.

Example
let arr = [0; 10];
println!("{:?}", &arr[..10]);

Could be written as:

let arr = [0; 10];
println!("{:?}", &arr);

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

The issue does not name a file or test; start by reviewing existing Clippy slicing and array lint implementations and deciding whether the proposed [..N] and [0..N] cases are in scope. Done means the agreed cases are covered with tests, including the coercion and generic-behavior concerns; the arr[smth..N] idea may remain a separate decision.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.