Deprecate match_on_vec_items lint

This lint does more harm than good: in its description, it proposes to
rewrite `match` on `Vec<_>` indexes or slices by a version which cannot
panic but masks the failure by choosing the default variant.

The `clippy::indexing_slicing` restriction lint covers those cases more
safely, by suggesting to use a non-panicking version to retrieve the
value from the container, without suggesting to fallback to the default
success variant in case of failure.
This commit is contained in:
Samuel Tardieu 2025-02-14 12:50:37 +01:00
parent 1893e1e5e7
commit 1da567b619
8 changed files with 10 additions and 305 deletions

View file

@ -16,5 +16,6 @@
#![warn(clippy::pub_enum_variant_names)] //~ ERROR: lint `clippy::pub_enum_variant_names`
#![warn(clippy::wrong_pub_self_convention)] //~ ERROR: lint `clippy::wrong_pub_self_convention`
#![warn(clippy::option_map_or_err_ok)] //~ ERROR: lint `clippy::option_map_or_err_ok`
#![warn(clippy::match_on_vec_items)] //~ ERROR: lint `clippy::match_on_vec_items`
fn main() {}

View file

@ -85,5 +85,11 @@ error: lint `clippy::option_map_or_err_ok` has been removed: `clippy::manual_ok_
LL | #![warn(clippy::option_map_or_err_ok)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 14 previous errors
error: lint `clippy::match_on_vec_items` has been removed: `clippy::indexing_slicing` covers indexing and slicing on `Vec<_>`
--> tests/ui/deprecated.rs:19:9
|
LL | #![warn(clippy::match_on_vec_items)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 15 previous errors

View file

@ -1,161 +0,0 @@
#![warn(clippy::match_on_vec_items)]
#![allow(clippy::redundant_at_rest_pattern, clippy::useless_vec)]
//@no-rustfix
fn match_with_wildcard() {
let arr = vec![0, 1, 2, 3];
let range = 1..3;
let idx = 1;
// Lint, may panic
match arr[idx] {
//~^ match_on_vec_items
0 => println!("0"),
1 => println!("1"),
_ => {},
}
// Lint, may panic
match arr[range] {
//~^ match_on_vec_items
[0, 1] => println!("0 1"),
[1, 2] => println!("1 2"),
_ => {},
}
}
fn match_without_wildcard() {
let arr = vec![0, 1, 2, 3];
let range = 1..3;
let idx = 2;
// Lint, may panic
match arr[idx] {
//~^ match_on_vec_items
0 => println!("0"),
1 => println!("1"),
num => {},
}
// Lint, may panic
match arr[range] {
//~^ match_on_vec_items
[0, 1] => println!("0 1"),
[1, 2] => println!("1 2"),
[ref sub @ ..] => {},
}
}
fn match_wildcard_and_action() {
let arr = vec![0, 1, 2, 3];
let range = 1..3;
let idx = 3;
// Lint, may panic
match arr[idx] {
//~^ match_on_vec_items
0 => println!("0"),
1 => println!("1"),
_ => println!("Hello, World!"),
}
// Lint, may panic
match arr[range] {
//~^ match_on_vec_items
[0, 1] => println!("0 1"),
[1, 2] => println!("1 2"),
_ => println!("Hello, World!"),
}
}
fn match_vec_ref() {
let arr = &vec![0, 1, 2, 3];
let range = 1..3;
let idx = 3;
// Lint, may panic
match arr[idx] {
//~^ match_on_vec_items
0 => println!("0"),
1 => println!("1"),
_ => {},
}
// Lint, may panic
match arr[range] {
//~^ match_on_vec_items
[0, 1] => println!("0 1"),
[1, 2] => println!("1 2"),
_ => {},
}
}
fn match_with_get() {
let arr = vec![0, 1, 2, 3];
let range = 1..3;
let idx = 3;
// Ok
match arr.get(idx) {
Some(0) => println!("0"),
Some(1) => println!("1"),
_ => {},
}
// Ok
match arr.get(range) {
Some(&[0, 1]) => println!("0 1"),
Some(&[1, 2]) => println!("1 2"),
_ => {},
}
}
fn match_with_array() {
let arr = [0, 1, 2, 3];
let range = 1..3;
let idx = 3;
// Ok
match arr[idx] {
0 => println!("0"),
1 => println!("1"),
_ => {},
}
// Ok
match arr[range] {
[0, 1] => println!("0 1"),
[1, 2] => println!("1 2"),
_ => {},
}
}
fn match_with_endless_range() {
let arr = vec![0, 1, 2, 3];
let range = ..;
// Ok
match arr[range] {
[0, 1] => println!("0 1"),
[1, 2] => println!("1 2"),
[0, 1, 2, 3] => println!("0, 1, 2, 3"),
_ => {},
}
// Ok
match arr[..] {
[0, 1] => println!("0 1"),
[1, 2] => println!("1 2"),
[0, 1, 2, 3] => println!("0, 1, 2, 3"),
_ => {},
}
}
fn main() {
match_with_wildcard();
match_without_wildcard();
match_wildcard_and_action();
match_vec_ref();
match_with_get();
match_with_array();
match_with_endless_range();
}

View file

@ -1,53 +0,0 @@
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:10:11
|
LL | match arr[idx] {
| ^^^^^^^^ help: try: `arr.get(idx)`
|
= note: `-D clippy::match-on-vec-items` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::match_on_vec_items)]`
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:18:11
|
LL | match arr[range] {
| ^^^^^^^^^^ help: try: `arr.get(range)`
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:32:11
|
LL | match arr[idx] {
| ^^^^^^^^ help: try: `arr.get(idx)`
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:40:11
|
LL | match arr[range] {
| ^^^^^^^^^^ help: try: `arr.get(range)`
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:54:11
|
LL | match arr[idx] {
| ^^^^^^^^ help: try: `arr.get(idx)`
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:62:11
|
LL | match arr[range] {
| ^^^^^^^^^^ help: try: `arr.get(range)`
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:76:11
|
LL | match arr[idx] {
| ^^^^^^^^ help: try: `arr.get(idx)`
error: indexing into a vector may panic
--> tests/ui/match_on_vec_items.rs:84:11
|
LL | match arr[range] {
| ^^^^^^^^^^ help: try: `arr.get(range)`
error: aborting due to 8 previous errors