2020-04-25 08:33:40 +00:00
|
|
|
#![warn(clippy::match_on_vec_items)]
|
2023-06-27 11:09:28 +00:00
|
|
|
#![allow(clippy::redundant_at_rest_pattern, clippy::useless_vec)]
|
2023-07-27 11:40:22 +00:00
|
|
|
//@no-rustfix
|
2020-04-23 21:28:18 +00:00
|
|
|
fn match_with_wildcard() {
|
|
|
|
let arr = vec![0, 1, 2, 3];
|
|
|
|
let range = 1..3;
|
|
|
|
let idx = 1;
|
|
|
|
|
|
|
|
// Lint, may panic
|
|
|
|
match arr[idx] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
|
|
|
//~| NOTE: `-D clippy::match-on-vec-items` implied by `-D warnings`
|
2020-04-23 21:28:18 +00:00
|
|
|
0 => println!("0"),
|
|
|
|
1 => println!("1"),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lint, may panic
|
|
|
|
match arr[range] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
[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;
|
|
|
|
|
2020-04-24 21:52:02 +00:00
|
|
|
// Lint, may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
match arr[idx] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
0 => println!("0"),
|
|
|
|
1 => println!("1"),
|
|
|
|
num => {},
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:52:02 +00:00
|
|
|
// Lint, may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
match arr[range] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
[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;
|
|
|
|
|
2020-04-24 21:52:02 +00:00
|
|
|
// Lint, may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
match arr[idx] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
0 => println!("0"),
|
|
|
|
1 => println!("1"),
|
|
|
|
_ => println!("Hello, World!"),
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:52:02 +00:00
|
|
|
// Lint, may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
match arr[range] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
2020-04-23 21:28:18 +00:00
|
|
|
[0, 1] => println!("0 1"),
|
|
|
|
[1, 2] => println!("1 2"),
|
|
|
|
_ => println!("Hello, World!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:52:02 +00:00
|
|
|
fn match_vec_ref() {
|
|
|
|
let arr = &vec![0, 1, 2, 3];
|
|
|
|
let range = 1..3;
|
|
|
|
let idx = 3;
|
|
|
|
|
|
|
|
// Lint, may panic
|
|
|
|
match arr[idx] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
2020-04-24 21:52:02 +00:00
|
|
|
0 => println!("0"),
|
|
|
|
1 => println!("1"),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lint, may panic
|
|
|
|
match arr[range] {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: indexing into a vector may panic
|
2020-04-24 21:52:02 +00:00
|
|
|
[0, 1] => println!("0 1"),
|
|
|
|
[1, 2] => println!("1 2"),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:28:18 +00:00
|
|
|
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"),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 14:57:19 +00:00
|
|
|
|
2020-05-02 11:25:45 +00:00
|
|
|
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"),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:57:19 +00:00
|
|
|
fn main() {
|
|
|
|
match_with_wildcard();
|
|
|
|
match_without_wildcard();
|
|
|
|
match_wildcard_and_action();
|
|
|
|
match_vec_ref();
|
|
|
|
match_with_get();
|
|
|
|
match_with_array();
|
2020-05-02 11:25:45 +00:00
|
|
|
match_with_endless_range();
|
2020-04-26 14:57:19 +00:00
|
|
|
}
|