Skip to content

Commit

Permalink
Auto merge of #12029 - torfsen:6459-more-cases-for-redundant-matches!…
Browse files Browse the repository at this point in the history
…, 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!`
```
  • Loading branch information
bors committed Dec 27, 2023
2 parents c689d32 + ebc0588 commit 7343db9
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 56 deletions.
42 changes: 18 additions & 24 deletions clippy_lints/src/matches/redundant_pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,31 +411,25 @@ fn get_good_method<'tcx>(
path_left: &QPath<'_>,
) -> Option<(&'static str, Option<&'tcx Guard<'tcx>>)> {
if let Some(name) = get_ident(path_left) {
return match name.as_str() {
"Ok" => {
find_good_method_for_matches_macro(cx, arms, path_left, Item::Lang(ResultOk), "is_ok()", "is_err()")
},
"Err" => {
find_good_method_for_matches_macro(cx, arms, path_left, Item::Lang(ResultErr), "is_err()", "is_ok()")
},
"Some" => find_good_method_for_matches_macro(
cx,
arms,
path_left,
Item::Lang(OptionSome),
"is_some()",
"is_none()",
),
"None" => find_good_method_for_matches_macro(
cx,
arms,
path_left,
Item::Lang(OptionNone),
"is_none()",
"is_some()",
),
_ => None,
let (expected_item_left, should_be_left, should_be_right) = match name.as_str() {
"Ok" => (Item::Lang(ResultOk), "is_ok()", "is_err()"),
"Err" => (Item::Lang(ResultErr), "is_err()", "is_ok()"),
"Some" => (Item::Lang(OptionSome), "is_some()", "is_none()"),
"None" => (Item::Lang(OptionNone), "is_none()", "is_some()"),
"Ready" => (Item::Lang(PollReady), "is_ready()", "is_pending()"),
"Pending" => (Item::Lang(PollPending), "is_pending()", "is_ready()"),
"V4" => (Item::Diag(sym::IpAddr, sym!(V4)), "is_ipv4()", "is_ipv6()"),
"V6" => (Item::Diag(sym::IpAddr, sym!(V6)), "is_ipv6()", "is_ipv4()"),
_ => return None,
};
return find_good_method_for_matches_macro(
cx,
arms,
path_left,
expected_item_left,
should_be_left,
should_be_right,
);
}
None
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/redundant_pattern_matching_ipaddr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ fn main() {

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

// Issue 6459
if V4(Ipv4Addr::LOCALHOST).is_ipv4() {}

// Issue 6459
if V6(Ipv6Addr::LOCALHOST).is_ipv6() {}

while V4(Ipv4Addr::LOCALHOST).is_ipv4() {}

while V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/redundant_pattern_matching_ipaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ fn main() {

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

// Issue 6459
if matches!(V4(Ipv4Addr::LOCALHOST), V4(_)) {}

// Issue 6459
if matches!(V6(Ipv6Addr::LOCALHOST), V6(_)) {}

while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}

while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
Expand Down
44 changes: 28 additions & 16 deletions tests/ui/redundant_pattern_matching_ipaddr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,31 @@ LL | if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
| -------^^^^^-------------------------- help: try: `if V6(Ipv6Addr::LOCALHOST).is_ipv6()`

error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:21:15
--> $DIR/redundant_pattern_matching_ipaddr.rs:22:8
|
LL | if matches!(V4(Ipv4Addr::LOCALHOST), V4(_)) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `V4(Ipv4Addr::LOCALHOST).is_ipv4()`

error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:25:8
|
LL | if matches!(V6(Ipv6Addr::LOCALHOST), V6(_)) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `V6(Ipv6Addr::LOCALHOST).is_ipv6()`

error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:27:15
|
LL | while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
| ----------^^^^^-------------------------- help: try: `while V4(Ipv4Addr::LOCALHOST).is_ipv4()`

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

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

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

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

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

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

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

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

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

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

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

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

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

error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:88:5
--> $DIR/redundant_pattern_matching_ipaddr.rs:94:5
|
LL | / match V6(Ipv6Addr::LOCALHOST) {
LL | | V4(_) => false,
LL | | V6(_) => true,
LL | | };
| |_____^ help: try: `V6(Ipv6Addr::LOCALHOST).is_ipv6()`

error: aborting due to 18 previous errors
error: aborting due to 20 previous errors

6 changes: 6 additions & 0 deletions tests/ui/redundant_pattern_matching_poll.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ fn main() {
bar();
}

// Issue 6459
if Ready(42).is_ready() {}

// Issue 6459
if Pending::<()>.is_pending() {}

while Ready(42).is_ready() {}

while Ready(42).is_pending() {}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/redundant_pattern_matching_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ fn main() {
bar();
}

// Issue 6459
if matches!(Ready(42), Ready(_)) {}

// Issue 6459
if matches!(Pending::<()>, Pending) {}

while let Ready(_) = Ready(42) {}

while let Pending = Ready(42) {}
Expand Down
44 changes: 28 additions & 16 deletions tests/ui/redundant_pattern_matching_poll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,37 @@ LL | if let Ready(_) = Ready(42) {
| -------^^^^^^^^------------ help: try: `if Ready(42).is_ready()`

error: redundant pattern matching, consider using `is_ready()`
--> $DIR/redundant_pattern_matching_poll.rs:25:15
--> $DIR/redundant_pattern_matching_poll.rs:26:8
|
LL | if matches!(Ready(42), Ready(_)) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Ready(42).is_ready()`

error: redundant pattern matching, consider using `is_pending()`
--> $DIR/redundant_pattern_matching_poll.rs:29:8
|
LL | if matches!(Pending::<()>, Pending) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Pending::<()>.is_pending()`

error: redundant pattern matching, consider using `is_ready()`
--> $DIR/redundant_pattern_matching_poll.rs:31:15
|
LL | while let Ready(_) = Ready(42) {}
| ----------^^^^^^^^------------ help: try: `while Ready(42).is_ready()`

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

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

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

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

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

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

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

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

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

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

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

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

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

error: redundant pattern matching, consider using `is_pending()`
--> $DIR/redundant_pattern_matching_poll.rs:86:5
--> $DIR/redundant_pattern_matching_poll.rs:92:5
|
LL | / match Pending::<()> {
LL | | Ready(_) => false,
LL | | Pending => true,
LL | | };
| |_____^ help: try: `Pending::<()>.is_pending()`

error: aborting due to 18 previous errors
error: aborting due to 20 previous errors

0 comments on commit 7343db9

Please sign in to comment.