Skip to content

Commit 7343db9

Browse files
committed
Auto merge of #12029 - torfsen:6459-more-cases-for-redundant-matches!, r=llogiq
6459: Check for redundant `matches!` with `Ready`, `Pending`, `V4`, `V6` Fixes #6459. ``` changelog: [`redundant_pattern_matching`]: Add checks for `Poll::{Ready,Pending}` and `IpAddr::{V4,V6}` in `matches!` ```
2 parents c689d32 + ebc0588 commit 7343db9

7 files changed

+98
-56
lines changed

clippy_lints/src/matches/redundant_pattern_match.rs

+18-24
Original file line numberDiff line numberDiff line change
@@ -411,31 +411,25 @@ fn get_good_method<'tcx>(
411411
path_left: &QPath<'_>,
412412
) -> Option<(&'static str, Option<&'tcx Guard<'tcx>>)> {
413413
if let Some(name) = get_ident(path_left) {
414-
return match name.as_str() {
415-
"Ok" => {
416-
find_good_method_for_matches_macro(cx, arms, path_left, Item::Lang(ResultOk), "is_ok()", "is_err()")
417-
},
418-
"Err" => {
419-
find_good_method_for_matches_macro(cx, arms, path_left, Item::Lang(ResultErr), "is_err()", "is_ok()")
420-
},
421-
"Some" => find_good_method_for_matches_macro(
422-
cx,
423-
arms,
424-
path_left,
425-
Item::Lang(OptionSome),
426-
"is_some()",
427-
"is_none()",
428-
),
429-
"None" => find_good_method_for_matches_macro(
430-
cx,
431-
arms,
432-
path_left,
433-
Item::Lang(OptionNone),
434-
"is_none()",
435-
"is_some()",
436-
),
437-
_ => None,
414+
let (expected_item_left, should_be_left, should_be_right) = match name.as_str() {
415+
"Ok" => (Item::Lang(ResultOk), "is_ok()", "is_err()"),
416+
"Err" => (Item::Lang(ResultErr), "is_err()", "is_ok()"),
417+
"Some" => (Item::Lang(OptionSome), "is_some()", "is_none()"),
418+
"None" => (Item::Lang(OptionNone), "is_none()", "is_some()"),
419+
"Ready" => (Item::Lang(PollReady), "is_ready()", "is_pending()"),
420+
"Pending" => (Item::Lang(PollPending), "is_pending()", "is_ready()"),
421+
"V4" => (Item::Diag(sym::IpAddr, sym!(V4)), "is_ipv4()", "is_ipv6()"),
422+
"V6" => (Item::Diag(sym::IpAddr, sym!(V6)), "is_ipv6()", "is_ipv4()"),
423+
_ => return None,
438424
};
425+
return find_good_method_for_matches_macro(
426+
cx,
427+
arms,
428+
path_left,
429+
expected_item_left,
430+
should_be_left,
431+
should_be_right,
432+
);
439433
}
440434
None
441435
}

tests/ui/redundant_pattern_matching_ipaddr.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ fn main() {
1818

1919
if V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
2020

21+
// Issue 6459
22+
if V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
23+
24+
// Issue 6459
25+
if V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
26+
2127
while V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
2228

2329
while V6(Ipv6Addr::LOCALHOST).is_ipv6() {}

tests/ui/redundant_pattern_matching_ipaddr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ fn main() {
1818

1919
if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
2020

21+
// Issue 6459
22+
if matches!(V4(Ipv4Addr::LOCALHOST), V4(_)) {}
23+
24+
// Issue 6459
25+
if matches!(V6(Ipv6Addr::LOCALHOST), V6(_)) {}
26+
2127
while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
2228

2329
while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}

tests/ui/redundant_pattern_matching_ipaddr.stderr

+28-16
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,31 @@ LL | if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
2020
| -------^^^^^-------------------------- help: try: `if V6(Ipv6Addr::LOCALHOST).is_ipv6()`
2121

2222
error: redundant pattern matching, consider using `is_ipv4()`
23-
--> $DIR/redundant_pattern_matching_ipaddr.rs:21:15
23+
--> $DIR/redundant_pattern_matching_ipaddr.rs:22:8
24+
|
25+
LL | if matches!(V4(Ipv4Addr::LOCALHOST), V4(_)) {}
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `V4(Ipv4Addr::LOCALHOST).is_ipv4()`
27+
28+
error: redundant pattern matching, consider using `is_ipv6()`
29+
--> $DIR/redundant_pattern_matching_ipaddr.rs:25:8
30+
|
31+
LL | if matches!(V6(Ipv6Addr::LOCALHOST), V6(_)) {}
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `V6(Ipv6Addr::LOCALHOST).is_ipv6()`
33+
34+
error: redundant pattern matching, consider using `is_ipv4()`
35+
--> $DIR/redundant_pattern_matching_ipaddr.rs:27:15
2436
|
2537
LL | while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
2638
| ----------^^^^^-------------------------- help: try: `while V4(Ipv4Addr::LOCALHOST).is_ipv4()`
2739

2840
error: redundant pattern matching, consider using `is_ipv6()`
29-
--> $DIR/redundant_pattern_matching_ipaddr.rs:23:15
41+
--> $DIR/redundant_pattern_matching_ipaddr.rs:29:15
3042
|
3143
LL | while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
3244
| ----------^^^^^-------------------------- help: try: `while V6(Ipv6Addr::LOCALHOST).is_ipv6()`
3345

3446
error: redundant pattern matching, consider using `is_ipv4()`
35-
--> $DIR/redundant_pattern_matching_ipaddr.rs:33:5
47+
--> $DIR/redundant_pattern_matching_ipaddr.rs:39:5
3648
|
3749
LL | / match V4(Ipv4Addr::LOCALHOST) {
3850
LL | | V4(_) => true,
@@ -41,7 +53,7 @@ LL | | };
4153
| |_____^ help: try: `V4(Ipv4Addr::LOCALHOST).is_ipv4()`
4254

4355
error: redundant pattern matching, consider using `is_ipv6()`
44-
--> $DIR/redundant_pattern_matching_ipaddr.rs:38:5
56+
--> $DIR/redundant_pattern_matching_ipaddr.rs:44:5
4557
|
4658
LL | / match V4(Ipv4Addr::LOCALHOST) {
4759
LL | | V4(_) => false,
@@ -50,7 +62,7 @@ LL | | };
5062
| |_____^ help: try: `V4(Ipv4Addr::LOCALHOST).is_ipv6()`
5163

5264
error: redundant pattern matching, consider using `is_ipv6()`
53-
--> $DIR/redundant_pattern_matching_ipaddr.rs:43:5
65+
--> $DIR/redundant_pattern_matching_ipaddr.rs:49:5
5466
|
5567
LL | / match V6(Ipv6Addr::LOCALHOST) {
5668
LL | | V4(_) => false,
@@ -59,7 +71,7 @@ LL | | };
5971
| |_____^ help: try: `V6(Ipv6Addr::LOCALHOST).is_ipv6()`
6072

6173
error: redundant pattern matching, consider using `is_ipv4()`
62-
--> $DIR/redundant_pattern_matching_ipaddr.rs:48:5
74+
--> $DIR/redundant_pattern_matching_ipaddr.rs:54:5
6375
|
6476
LL | / match V6(Ipv6Addr::LOCALHOST) {
6577
LL | | V4(_) => true,
@@ -68,49 +80,49 @@ LL | | };
6880
| |_____^ help: try: `V6(Ipv6Addr::LOCALHOST).is_ipv4()`
6981

7082
error: redundant pattern matching, consider using `is_ipv4()`
71-
--> $DIR/redundant_pattern_matching_ipaddr.rs:53:20
83+
--> $DIR/redundant_pattern_matching_ipaddr.rs:59:20
7284
|
7385
LL | let _ = if let V4(_) = V4(Ipv4Addr::LOCALHOST) {
7486
| -------^^^^^-------------------------- help: try: `if V4(Ipv4Addr::LOCALHOST).is_ipv4()`
7587

7688
error: redundant pattern matching, consider using `is_ipv4()`
77-
--> $DIR/redundant_pattern_matching_ipaddr.rs:61:20
89+
--> $DIR/redundant_pattern_matching_ipaddr.rs:67:20
7890
|
7991
LL | let _ = if let V4(_) = gen_ipaddr() {
8092
| -------^^^^^--------------- help: try: `if gen_ipaddr().is_ipv4()`
8193

8294
error: redundant pattern matching, consider using `is_ipv6()`
83-
--> $DIR/redundant_pattern_matching_ipaddr.rs:63:19
95+
--> $DIR/redundant_pattern_matching_ipaddr.rs:69:19
8496
|
8597
LL | } else if let V6(_) = gen_ipaddr() {
8698
| -------^^^^^--------------- help: try: `if gen_ipaddr().is_ipv6()`
8799

88100
error: redundant pattern matching, consider using `is_ipv4()`
89-
--> $DIR/redundant_pattern_matching_ipaddr.rs:75:12
101+
--> $DIR/redundant_pattern_matching_ipaddr.rs:81:12
90102
|
91103
LL | if let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
92104
| -------^^^^^-------------------------- help: try: `if V4(Ipv4Addr::LOCALHOST).is_ipv4()`
93105

94106
error: redundant pattern matching, consider using `is_ipv6()`
95-
--> $DIR/redundant_pattern_matching_ipaddr.rs:77:12
107+
--> $DIR/redundant_pattern_matching_ipaddr.rs:83:12
96108
|
97109
LL | if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
98110
| -------^^^^^-------------------------- help: try: `if V6(Ipv6Addr::LOCALHOST).is_ipv6()`
99111

100112
error: redundant pattern matching, consider using `is_ipv4()`
101-
--> $DIR/redundant_pattern_matching_ipaddr.rs:79:15
113+
--> $DIR/redundant_pattern_matching_ipaddr.rs:85:15
102114
|
103115
LL | while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
104116
| ----------^^^^^-------------------------- help: try: `while V4(Ipv4Addr::LOCALHOST).is_ipv4()`
105117

106118
error: redundant pattern matching, consider using `is_ipv6()`
107-
--> $DIR/redundant_pattern_matching_ipaddr.rs:81:15
119+
--> $DIR/redundant_pattern_matching_ipaddr.rs:87:15
108120
|
109121
LL | while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
110122
| ----------^^^^^-------------------------- help: try: `while V6(Ipv6Addr::LOCALHOST).is_ipv6()`
111123

112124
error: redundant pattern matching, consider using `is_ipv4()`
113-
--> $DIR/redundant_pattern_matching_ipaddr.rs:83:5
125+
--> $DIR/redundant_pattern_matching_ipaddr.rs:89:5
114126
|
115127
LL | / match V4(Ipv4Addr::LOCALHOST) {
116128
LL | | V4(_) => true,
@@ -119,13 +131,13 @@ LL | | };
119131
| |_____^ help: try: `V4(Ipv4Addr::LOCALHOST).is_ipv4()`
120132

121133
error: redundant pattern matching, consider using `is_ipv6()`
122-
--> $DIR/redundant_pattern_matching_ipaddr.rs:88:5
134+
--> $DIR/redundant_pattern_matching_ipaddr.rs:94:5
123135
|
124136
LL | / match V6(Ipv6Addr::LOCALHOST) {
125137
LL | | V4(_) => false,
126138
LL | | V6(_) => true,
127139
LL | | };
128140
| |_____^ help: try: `V6(Ipv6Addr::LOCALHOST).is_ipv6()`
129141

130-
error: aborting due to 18 previous errors
142+
error: aborting due to 20 previous errors
131143

tests/ui/redundant_pattern_matching_poll.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ fn main() {
2222
bar();
2323
}
2424

25+
// Issue 6459
26+
if Ready(42).is_ready() {}
27+
28+
// Issue 6459
29+
if Pending::<()>.is_pending() {}
30+
2531
while Ready(42).is_ready() {}
2632

2733
while Ready(42).is_pending() {}

tests/ui/redundant_pattern_matching_poll.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ fn main() {
2222
bar();
2323
}
2424

25+
// Issue 6459
26+
if matches!(Ready(42), Ready(_)) {}
27+
28+
// Issue 6459
29+
if matches!(Pending::<()>, Pending) {}
30+
2531
while let Ready(_) = Ready(42) {}
2632

2733
while let Pending = Ready(42) {}

tests/ui/redundant_pattern_matching_poll.stderr

+28-16
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,37 @@ LL | if let Ready(_) = Ready(42) {
2020
| -------^^^^^^^^------------ help: try: `if Ready(42).is_ready()`
2121

2222
error: redundant pattern matching, consider using `is_ready()`
23-
--> $DIR/redundant_pattern_matching_poll.rs:25:15
23+
--> $DIR/redundant_pattern_matching_poll.rs:26:8
24+
|
25+
LL | if matches!(Ready(42), Ready(_)) {}
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Ready(42).is_ready()`
27+
28+
error: redundant pattern matching, consider using `is_pending()`
29+
--> $DIR/redundant_pattern_matching_poll.rs:29:8
30+
|
31+
LL | if matches!(Pending::<()>, Pending) {}
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Pending::<()>.is_pending()`
33+
34+
error: redundant pattern matching, consider using `is_ready()`
35+
--> $DIR/redundant_pattern_matching_poll.rs:31:15
2436
|
2537
LL | while let Ready(_) = Ready(42) {}
2638
| ----------^^^^^^^^------------ help: try: `while Ready(42).is_ready()`
2739

2840
error: redundant pattern matching, consider using `is_pending()`
29-
--> $DIR/redundant_pattern_matching_poll.rs:27:15
41+
--> $DIR/redundant_pattern_matching_poll.rs:33:15
3042
|
3143
LL | while let Pending = Ready(42) {}
3244
| ----------^^^^^^^------------ help: try: `while Ready(42).is_pending()`
3345

3446
error: redundant pattern matching, consider using `is_pending()`
35-
--> $DIR/redundant_pattern_matching_poll.rs:29:15
47+
--> $DIR/redundant_pattern_matching_poll.rs:35:15
3648
|
3749
LL | while let Pending = Pending::<()> {}
3850
| ----------^^^^^^^---------------- help: try: `while Pending::<()>.is_pending()`
3951

4052
error: redundant pattern matching, consider using `is_ready()`
41-
--> $DIR/redundant_pattern_matching_poll.rs:35:5
53+
--> $DIR/redundant_pattern_matching_poll.rs:41:5
4254
|
4355
LL | / match Ready(42) {
4456
LL | | Ready(_) => true,
@@ -47,7 +59,7 @@ LL | | };
4759
| |_____^ help: try: `Ready(42).is_ready()`
4860

4961
error: redundant pattern matching, consider using `is_pending()`
50-
--> $DIR/redundant_pattern_matching_poll.rs:40:5
62+
--> $DIR/redundant_pattern_matching_poll.rs:46:5
5163
|
5264
LL | / match Pending::<()> {
5365
LL | | Ready(_) => false,
@@ -56,7 +68,7 @@ LL | | };
5668
| |_____^ help: try: `Pending::<()>.is_pending()`
5769

5870
error: redundant pattern matching, consider using `is_pending()`
59-
--> $DIR/redundant_pattern_matching_poll.rs:45:13
71+
--> $DIR/redundant_pattern_matching_poll.rs:51:13
6072
|
6173
LL | let _ = match Pending::<()> {
6274
| _____________^
@@ -66,49 +78,49 @@ LL | | };
6678
| |_____^ help: try: `Pending::<()>.is_pending()`
6779

6880
error: redundant pattern matching, consider using `is_ready()`
69-
--> $DIR/redundant_pattern_matching_poll.rs:51:20
81+
--> $DIR/redundant_pattern_matching_poll.rs:57:20
7082
|
7183
LL | let _ = if let Ready(_) = poll { true } else { false };
7284
| -------^^^^^^^^------- help: try: `if poll.is_ready()`
7385

7486
error: redundant pattern matching, consider using `is_ready()`
75-
--> $DIR/redundant_pattern_matching_poll.rs:55:20
87+
--> $DIR/redundant_pattern_matching_poll.rs:61:20
7688
|
7789
LL | let _ = if let Ready(_) = gen_poll() {
7890
| -------^^^^^^^^------------- help: try: `if gen_poll().is_ready()`
7991

8092
error: redundant pattern matching, consider using `is_pending()`
81-
--> $DIR/redundant_pattern_matching_poll.rs:57:19
93+
--> $DIR/redundant_pattern_matching_poll.rs:63:19
8294
|
8395
LL | } else if let Pending = gen_poll() {
8496
| -------^^^^^^^------------- help: try: `if gen_poll().is_pending()`
8597

8698
error: redundant pattern matching, consider using `is_ready()`
87-
--> $DIR/redundant_pattern_matching_poll.rs:73:12
99+
--> $DIR/redundant_pattern_matching_poll.rs:79:12
88100
|
89101
LL | if let Ready(_) = Ready(42) {}
90102
| -------^^^^^^^^------------ help: try: `if Ready(42).is_ready()`
91103

92104
error: redundant pattern matching, consider using `is_pending()`
93-
--> $DIR/redundant_pattern_matching_poll.rs:75:12
105+
--> $DIR/redundant_pattern_matching_poll.rs:81:12
94106
|
95107
LL | if let Pending = Pending::<()> {}
96108
| -------^^^^^^^---------------- help: try: `if Pending::<()>.is_pending()`
97109

98110
error: redundant pattern matching, consider using `is_ready()`
99-
--> $DIR/redundant_pattern_matching_poll.rs:77:15
111+
--> $DIR/redundant_pattern_matching_poll.rs:83:15
100112
|
101113
LL | while let Ready(_) = Ready(42) {}
102114
| ----------^^^^^^^^------------ help: try: `while Ready(42).is_ready()`
103115

104116
error: redundant pattern matching, consider using `is_pending()`
105-
--> $DIR/redundant_pattern_matching_poll.rs:79:15
117+
--> $DIR/redundant_pattern_matching_poll.rs:85:15
106118
|
107119
LL | while let Pending = Pending::<()> {}
108120
| ----------^^^^^^^---------------- help: try: `while Pending::<()>.is_pending()`
109121

110122
error: redundant pattern matching, consider using `is_ready()`
111-
--> $DIR/redundant_pattern_matching_poll.rs:81:5
123+
--> $DIR/redundant_pattern_matching_poll.rs:87:5
112124
|
113125
LL | / match Ready(42) {
114126
LL | | Ready(_) => true,
@@ -117,13 +129,13 @@ LL | | };
117129
| |_____^ help: try: `Ready(42).is_ready()`
118130

119131
error: redundant pattern matching, consider using `is_pending()`
120-
--> $DIR/redundant_pattern_matching_poll.rs:86:5
132+
--> $DIR/redundant_pattern_matching_poll.rs:92:5
121133
|
122134
LL | / match Pending::<()> {
123135
LL | | Ready(_) => false,
124136
LL | | Pending => true,
125137
LL | | };
126138
| |_____^ help: try: `Pending::<()>.is_pending()`
127139

128-
error: aborting due to 18 previous errors
140+
error: aborting due to 20 previous errors
129141

0 commit comments

Comments
 (0)