rust-lang / rust-lang/rust-clippy

New lint: function takes slice and treats it as an array

Open
#8,397 0 comments 1 reaction 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

Finds functions expecting a contiguous view of items of a const length taking a slice instead of an array.

Real world case: https://github.com/image-rs/jpeg-decoder/pull/215/files

Lint Name

fn_slice_param_is_array

Category

pedantic

Advantage
  • Removes bounds checks/panics by giving the caller the responsibility to give the correct size
  • Removes potential programming mistakes
Drawbacks
  • Possible false positives?
Example
const LEN: usize = 64;

fn foo(items: &[u8]) {
    assert_eq!(items.len(), LEN);
    // or...
    debug_assert_eq!(items.len(), LEN);
    // or...
    let items = &items[..LEN];
}

Could be written as:

const LEN: usize = 64;

fn foo(items: &[u8; LEN]) {
}

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 reviewing the proposed fn_slice_param_is_array lint and its Rust examples, including the jpeg-decoder case linked in the issue. Define detection around a constant-length slice used as an array, then validate the behavior against false positives and confirm the lint's pedantic categorization.

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.