Skip to content

Commit 9104400

Browse files
committed
Add postfix match support & UI tests
1 parent 9155012 commit 9104400

File tree

4 files changed

+226
-1
lines changed

4 files changed

+226
-1
lines changed

src/tools/clippy/clippy_lints/src/matches/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
10531053
}
10541054

10551055
if !from_expansion && !contains_cfg_arm(cx, expr, ex, arms) {
1056-
if source == MatchSource::Normal {
1056+
if let MatchSource::Normal | MatchSource::Postfix = source {
10571057
if !(self.msrv.meets(msrvs::MATCHES_MACRO) && match_like_matches::check_match(cx, expr, ex, arms)) {
10581058
match_same_arms::check(cx, arms);
10591059
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#![feature(postfix_match)]
2+
#![allow(clippy::redundant_at_rest_pattern, clippy::useless_vec)]
3+
#![warn(
4+
clippy::match_on_vec_items,
5+
clippy::single_match,
6+
clippy::match_bool,
7+
clippy::match_as_ref,
8+
clippy::needless_match,
9+
clippy::match_str_case_mismatch,
10+
clippy::redundant_guards
11+
)]
12+
13+
fn main() {
14+
// match_on_vec_items
15+
let arr = vec![0, 1, 2, 3];
16+
let idx = 1;
17+
18+
arr.get(idx).match {
19+
//~^ ERROR: indexing into a vector may panic
20+
//~| NOTE: `-D clippy::match-on-vec-items` implied by `-D warnings`
21+
0 => println!("{}", 0),
22+
1 => println!("{}", 3),
23+
_ => {},
24+
};
25+
26+
// single_match
27+
let x = Some(1u8);
28+
29+
if let Some(y) = x {
30+
println!("{:?}", y);
31+
};
32+
33+
// match_bool
34+
let test: bool = true;
35+
36+
if test { 0 } else { 42 };
37+
38+
// match_as_ref
39+
let owned: Option<()> = None;
40+
let borrowed: Option<&()> = owned.as_ref();
41+
42+
// needless_match
43+
let i = 10;
44+
let _: i32 = i;
45+
46+
// match_str_case_mismatch
47+
let var = "BAR";
48+
var.to_ascii_lowercase().as_str().match {
49+
"foo" => {},
50+
"bar" => {},
51+
_ => {},
52+
}
53+
54+
// redundant_guards
55+
struct C(u32, u32);
56+
57+
let c = C(1, 2);
58+
c.match {
59+
C(x, 1) => ..,
60+
_ => todo!(),
61+
};
62+
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#![feature(postfix_match)]
2+
#![allow(clippy::redundant_at_rest_pattern, clippy::useless_vec)]
3+
#![warn(
4+
clippy::match_on_vec_items,
5+
clippy::single_match,
6+
clippy::match_bool,
7+
clippy::match_as_ref,
8+
clippy::needless_match,
9+
clippy::match_str_case_mismatch,
10+
clippy::redundant_guards
11+
)]
12+
13+
fn main() {
14+
// match_on_vec_items
15+
let arr = vec![0, 1, 2, 3];
16+
let idx = 1;
17+
18+
arr[idx].match {
19+
//~^ ERROR: indexing into a vector may panic
20+
//~| NOTE: `-D clippy::match-on-vec-items` implied by `-D warnings`
21+
0 => println!("{}", 0),
22+
1 => println!("{}", 3),
23+
_ => {},
24+
};
25+
26+
// single_match
27+
let x = Some(1u8);
28+
29+
x.match {
30+
Some(y) => {
31+
println!("{:?}", y);
32+
},
33+
_ => (),
34+
};
35+
36+
// match_bool
37+
let test: bool = true;
38+
39+
test.match {
40+
//~^ ERROR: you seem to be trying to match on a boolean expression
41+
true => 0,
42+
false => 42,
43+
};
44+
45+
// match_as_ref
46+
let owned: Option<()> = None;
47+
let borrowed: Option<&()> = owned.match {
48+
None => None,
49+
Some(ref v) => Some(v),
50+
};
51+
52+
// needless_match
53+
let i = 10;
54+
let _: i32 = i.match {
55+
0 => 0,
56+
1 => 1,
57+
2 => 2,
58+
_ => i,
59+
};
60+
61+
// match_str_case_mismatch
62+
let var = "BAR";
63+
var.to_ascii_lowercase().as_str().match {
64+
"foo" => {},
65+
"bar" => {},
66+
_ => {},
67+
}
68+
69+
// redundant_guards
70+
struct C(u32, u32);
71+
72+
let c = C(1, 2);
73+
c.match {
74+
C(x, y) if 1 == y => ..,
75+
_ => todo!(),
76+
};
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
error: indexing into a vector may panic
2+
--> $DIR/postfix_match.rs:18:5
3+
|
4+
LL | arr[idx].match {
5+
| ^^^^^^^^ help: try: `arr.get(idx)`
6+
|
7+
= note: `-D clippy::match-on-vec-items` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::match_on_vec_items)]`
9+
10+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
11+
--> $DIR/postfix_match.rs:29:5
12+
|
13+
LL | / x.match {
14+
LL | | Some(y) => {
15+
LL | | println!("{:?}", y);
16+
LL | | },
17+
LL | | _ => (),
18+
LL | | };
19+
| |_____^
20+
|
21+
= note: `-D clippy::single-match` implied by `-D warnings`
22+
= help: to override `-D warnings` add `#[allow(clippy::single_match)]`
23+
help: try
24+
|
25+
LL ~ if let Some(y) = x {
26+
LL + println!("{:?}", y);
27+
LL ~ };
28+
|
29+
30+
error: you seem to be trying to match on a boolean expression
31+
--> $DIR/postfix_match.rs:39:5
32+
|
33+
LL | / test.match {
34+
LL | |
35+
LL | | true => 0,
36+
LL | | false => 42,
37+
LL | | };
38+
| |_____^ help: consider using an `if`/`else` expression: `if test { 0 } else { 42 }`
39+
|
40+
= note: `-D clippy::match-bool` implied by `-D warnings`
41+
= help: to override `-D warnings` add `#[allow(clippy::match_bool)]`
42+
43+
error: use `as_ref()` instead
44+
--> $DIR/postfix_match.rs:47:33
45+
|
46+
LL | let borrowed: Option<&()> = owned.match {
47+
| _________________________________^
48+
LL | | None => None,
49+
LL | | Some(ref v) => Some(v),
50+
LL | | };
51+
| |_____^ help: try: `owned.as_ref()`
52+
|
53+
= note: `-D clippy::match-as-ref` implied by `-D warnings`
54+
= help: to override `-D warnings` add `#[allow(clippy::match_as_ref)]`
55+
56+
error: this match expression is unnecessary
57+
--> $DIR/postfix_match.rs:54:19
58+
|
59+
LL | let _: i32 = i.match {
60+
| ___________________^
61+
LL | | 0 => 0,
62+
LL | | 1 => 1,
63+
LL | | 2 => 2,
64+
LL | | _ => i,
65+
LL | | };
66+
| |_____^ help: replace it with: `i`
67+
|
68+
= note: `-D clippy::needless-match` implied by `-D warnings`
69+
= help: to override `-D warnings` add `#[allow(clippy::needless_match)]`
70+
71+
error: redundant guard
72+
--> $DIR/postfix_match.rs:74:20
73+
|
74+
LL | C(x, y) if 1 == y => ..,
75+
| ^^^^^^
76+
|
77+
= note: `-D clippy::redundant-guards` implied by `-D warnings`
78+
= help: to override `-D warnings` add `#[allow(clippy::redundant_guards)]`
79+
help: try
80+
|
81+
LL - C(x, y) if 1 == y => ..,
82+
LL + C(x, 1) => ..,
83+
|
84+
85+
error: aborting due to 6 previous errors
86+

0 commit comments

Comments
 (0)