Skip to content

Add new lint: manual_slice_match#17232

Open
sylvestre wants to merge 1 commit into
rust-lang:masterfrom
sylvestre:manual_slice_match
Open

Add new lint: manual_slice_match#17232
sylvestre wants to merge 1 commit into
rust-lang:masterfrom
sylvestre:manual_slice_match

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor

Add a new pedantic lint that detects if/else if chains whose conditions only inspect the length/emptiness of one and the same slice or Vec (via .is_empty() or .len() compared against an integer literal) and rewrites them as a match on a slice pattern.

The lint is conservative: it requires at least two conditions over the same receiver and an explicit final else, and rejects chains that mix in value guards. It generates concrete arms ([], [_], [_, _, ..]) where the length predicates are expressible as slice patterns, and falls back to a help-only diagnostic for comparisons that cannot be a single pattern set (<, <=, !=). Arrays are not linted, since their length is a compile-time constant. The suggestion is MaybeIncorrect (bodies are copied verbatim and matching a Vec borrows the receiver across the arms). It is gated on the slice-patterns MSRV (1.42).

changelog: Add new lint: manual_slice_match

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Jun 13, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 13, 2026
@rustbot

rustbot commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@sylvestre sylvestre force-pushed the manual_slice_match branch 2 times, most recently from 3c13460 to cc357a3 Compare June 13, 2026 21:31
@sylvestre sylvestre marked this pull request as draft June 13, 2026 21:38
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 13, 2026
@github-actions

github-actions Bot commented Jun 13, 2026

Copy link
Copy Markdown

Lintcheck changes for 09edb05

Lint Added Removed Changed
clippy::manual_slice_match 5 0 0

This comment will be updated if you push new changes

Add a new pedantic lint that detects `if`/`else if` chains whose
conditions only inspect the length/emptiness of one and the same slice
or `Vec` (via `.is_empty()` or `.len()` compared against an integer
literal) and rewrites them as a `match` on a slice pattern.

The lint is conservative: it requires at least two conditions over the
same receiver and an explicit final `else`, and rejects chains that mix
in value guards. It generates concrete arms (`[]`, `[_]`, `[_, _, ..]`)
where the length predicates are expressible as slice patterns, and falls
back to a help-only diagnostic for comparisons that cannot be a single
pattern set (`<`, `<=`, `!=`). Arrays are not linted, since their length
is a compile-time constant. The suggestion is `MaybeIncorrect` (bodies
are copied verbatim and matching a `Vec` borrows the receiver across the
arms). It is gated on the slice-patterns MSRV (1.42).
@sylvestre sylvestre force-pushed the manual_slice_match branch from cc357a3 to 09edb05 Compare June 13, 2026 21:42
@sylvestre sylvestre marked this pull request as ready for review June 13, 2026 21:45
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 13, 2026
@rustbot

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17250) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants