Skip to content

Commit b90c80a

Browse files
Deprecate match_on_vec_items lint (rust-lang#14217)
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 PR is an (opposite) alternative to rust-lang#14208 (which will add a suggestion to the lint matching the lint description). Discussion on both PRs can be found [on Zulip](https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/Suggestions.20that.20suppress.20panics). changelog: [`match_on_vec_items`]: deprecate lint
2 parents dbc0755 + 1da567b commit b90c80a

8 files changed

+10
-305
lines changed

clippy_lints/src/declared_lints.rs

-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ pub static LINTS: &[&crate::LintInfo] = &[
351351
crate::matches::MATCH_AS_REF_INFO,
352352
crate::matches::MATCH_BOOL_INFO,
353353
crate::matches::MATCH_LIKE_MATCHES_MACRO_INFO,
354-
crate::matches::MATCH_ON_VEC_ITEMS_INFO,
355354
crate::matches::MATCH_OVERLAPPING_ARM_INFO,
356355
crate::matches::MATCH_REF_PATS_INFO,
357356
crate::matches::MATCH_SAME_ARMS_INFO,

clippy_lints/src/deprecated_lints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ declare_with_version! { DEPRECATED(DEPRECATED_VERSION): &[(&str, &str)] = &[
4242
("clippy::wrong_pub_self_convention", "`clippy::wrong_self_convention` now covers this case via the `avoid-breaking-exported-api` config"),
4343
#[clippy::version = "1.86.0"]
4444
("clippy::option_map_or_err_ok", "`clippy::manual_ok_or` covers this case"),
45+
#[clippy::version = "1.86.0"]
46+
("clippy::match_on_vec_items", "`clippy::indexing_slicing` covers indexing and slicing on `Vec<_>`"),
4547
// end deprecated lints. used by `cargo dev deprecate_lint`
4648
]}
4749

clippy_lints/src/matches/match_on_vec_items.rs

-50
This file was deleted.

clippy_lints/src/matches/mod.rs

-39
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod manual_utils;
88
mod match_as_ref;
99
mod match_bool;
1010
mod match_like_matches;
11-
mod match_on_vec_items;
1211
mod match_ref_pats;
1312
mod match_same_arms;
1413
mod match_single_binding;
@@ -759,42 +758,6 @@ declare_clippy_lint! {
759758
"check if a `match` or `if let` can be simplified with `unwrap_or_default`"
760759
}
761760

762-
declare_clippy_lint! {
763-
/// ### What it does
764-
/// Checks for `match vec[idx]` or `match vec[n..m]`.
765-
///
766-
/// ### Why is this bad?
767-
/// This can panic at runtime.
768-
///
769-
/// ### Example
770-
/// ```rust, no_run
771-
/// let arr = vec![0, 1, 2, 3];
772-
/// let idx = 1;
773-
///
774-
/// match arr[idx] {
775-
/// 0 => println!("{}", 0),
776-
/// 1 => println!("{}", 3),
777-
/// _ => {},
778-
/// }
779-
/// ```
780-
///
781-
/// Use instead:
782-
/// ```rust, no_run
783-
/// let arr = vec![0, 1, 2, 3];
784-
/// let idx = 1;
785-
///
786-
/// match arr.get(idx) {
787-
/// Some(0) => println!("{}", 0),
788-
/// Some(1) => println!("{}", 3),
789-
/// _ => {},
790-
/// }
791-
/// ```
792-
#[clippy::version = "1.45.0"]
793-
pub MATCH_ON_VEC_ITEMS,
794-
pedantic,
795-
"matching on vector elements can panic"
796-
}
797-
798761
declare_clippy_lint! {
799762
/// ### What it does
800763
/// Checks for `match` expressions modifying the case of a string with non-compliant arms
@@ -1078,7 +1041,6 @@ impl_lint_pass!(Matches => [
10781041
COLLAPSIBLE_MATCH,
10791042
MANUAL_UNWRAP_OR,
10801043
MANUAL_UNWRAP_OR_DEFAULT,
1081-
MATCH_ON_VEC_ITEMS,
10821044
MATCH_STR_CASE_MISMATCH,
10831045
SIGNIFICANT_DROP_IN_SCRUTINEE,
10841046
TRY_ERR,
@@ -1156,7 +1118,6 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
11561118
match_wild_enum::check(cx, ex, arms);
11571119
match_as_ref::check(cx, ex, arms, expr);
11581120
needless_match::check_match(cx, ex, arms, expr);
1159-
match_on_vec_items::check(cx, ex);
11601121
match_str_case_mismatch::check(cx, ex, arms);
11611122
redundant_guards::check(cx, arms, self.msrv);
11621123

tests/ui/deprecated.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
#![warn(clippy::pub_enum_variant_names)] //~ ERROR: lint `clippy::pub_enum_variant_names`
1717
#![warn(clippy::wrong_pub_self_convention)] //~ ERROR: lint `clippy::wrong_pub_self_convention`
1818
#![warn(clippy::option_map_or_err_ok)] //~ ERROR: lint `clippy::option_map_or_err_ok`
19+
#![warn(clippy::match_on_vec_items)] //~ ERROR: lint `clippy::match_on_vec_items`
1920

2021
fn main() {}

tests/ui/deprecated.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,11 @@ error: lint `clippy::option_map_or_err_ok` has been removed: `clippy::manual_ok_
8585
LL | #![warn(clippy::option_map_or_err_ok)]
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8787

88-
error: aborting due to 14 previous errors
88+
error: lint `clippy::match_on_vec_items` has been removed: `clippy::indexing_slicing` covers indexing and slicing on `Vec<_>`
89+
--> tests/ui/deprecated.rs:19:9
90+
|
91+
LL | #![warn(clippy::match_on_vec_items)]
92+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
93+
94+
error: aborting due to 15 previous errors
8995

tests/ui/match_on_vec_items.rs

-161
This file was deleted.

tests/ui/match_on_vec_items.stderr

-53
This file was deleted.

0 commit comments

Comments
 (0)