rust/tests/ui/match_on_vec_items.rs
CrazyRoka 940c662654 Small lint update
- Changed lint category to `correctness`
- Moved main function to bottom in test file
- Added `FIXME` comment to `span_lint_and_sugg` to improve later
2020-04-26 18:00:51 +03:00

131 lines
2.4 KiB
Rust

#![warn(clippy::match_on_vec_items)]
fn match_with_wildcard() {
let arr = vec![0, 1, 2, 3];
let range = 1..3;
let idx = 1;
// Lint, may panic
match arr[idx] {
0 => println!("0"),
1 => println!("1"),
_ => {},
}
// Lint, may panic
match arr[range] {
[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] {
0 => println!("0"),
1 => println!("1"),
num => {},
}
// Lint, may panic
match arr[range] {
[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] {
0 => println!("0"),
1 => println!("1"),
_ => println!("Hello, World!"),
}
// Lint, may panic
match arr[range] {
[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] {
0 => println!("0"),
1 => println!("1"),
_ => {},
}
// Lint, may panic
match arr[range] {
[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 main() {
match_with_wildcard();
match_without_wildcard();
match_wildcard_and_action();
match_vec_ref();
match_with_get();
match_with_array();
}