Add new lint: manual_slice_match#17232
Open
sylvestre wants to merge 1 commit into
Open
Conversation
Collaborator
|
rustbot has assigned @samueltardieu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
3c13460 to
cc357a3
Compare
|
Lintcheck changes for 09edb05
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).
cc357a3 to
09edb05
Compare
Collaborator
|
☔ The latest upstream changes (possibly #17250) made this pull request unmergeable. Please resolve the merge conflicts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a new pedantic lint that detects
if/else ifchains whose conditions only inspect the length/emptiness of one and the same slice orVec(via.is_empty()or.len()compared against an integer literal) and rewrites them as amatchon 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 isMaybeIncorrect(bodies are copied verbatim and matching aVecborrows the receiver across the arms). It is gated on the slice-patterns MSRV (1.42).changelog: Add new lint: manual_slice_match