Change redundant_pattern_matching to also lint std::net::IpAddr

Suggest using utility methods `is_ipv4` and `is_ipv6`.
This commit is contained in:
Christiaan Dirkx 2020-11-25 02:01:05 +01:00
parent f897d27d8b
commit dc075b4266
11 changed files with 341 additions and 36 deletions

View file

@ -0,0 +1,73 @@
// run-rustfix
#![warn(clippy::all)]
#![warn(clippy::redundant_pattern_matching)]
#![allow(unused_must_use, clippy::needless_bool, clippy::match_like_matches_macro)]
use std::net::{
IpAddr::{self, V4, V6},
Ipv4Addr, Ipv6Addr,
};
fn main() {
let ipaddr: IpAddr = V4(Ipv4Addr::LOCALHOST);
if ipaddr.is_ipv4() {}
if V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
if V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
while V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
while V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
if V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
if V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
if let V4(ipaddr) = V4(Ipv4Addr::LOCALHOST) {
println!("{}", ipaddr);
}
V4(Ipv4Addr::LOCALHOST).is_ipv4();
V4(Ipv4Addr::LOCALHOST).is_ipv6();
V6(Ipv6Addr::LOCALHOST).is_ipv6();
V6(Ipv6Addr::LOCALHOST).is_ipv4();
let _ = if V4(Ipv4Addr::LOCALHOST).is_ipv4() {
true
} else {
false
};
ipaddr_const();
let _ = if gen_ipaddr().is_ipv4() {
1
} else if gen_ipaddr().is_ipv6() {
2
} else {
3
};
}
fn gen_ipaddr() -> IpAddr {
V4(Ipv4Addr::LOCALHOST)
}
const fn ipaddr_const() {
if V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
if V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
while V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
while V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
V4(Ipv4Addr::LOCALHOST).is_ipv4();
V6(Ipv6Addr::LOCALHOST).is_ipv6();
}

View file

@ -0,0 +1,91 @@
// run-rustfix
#![warn(clippy::all)]
#![warn(clippy::redundant_pattern_matching)]
#![allow(unused_must_use, clippy::needless_bool, clippy::match_like_matches_macro)]
use std::net::{
IpAddr::{self, V4, V6},
Ipv4Addr, Ipv6Addr,
};
fn main() {
let ipaddr: IpAddr = V4(Ipv4Addr::LOCALHOST);
if let V4(_) = &ipaddr {}
if let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
if V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
if V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
if let V4(ipaddr) = V4(Ipv4Addr::LOCALHOST) {
println!("{}", ipaddr);
}
match V4(Ipv4Addr::LOCALHOST) {
V4(_) => true,
V6(_) => false,
};
match V4(Ipv4Addr::LOCALHOST) {
V4(_) => false,
V6(_) => true,
};
match V6(Ipv6Addr::LOCALHOST) {
V4(_) => false,
V6(_) => true,
};
match V6(Ipv6Addr::LOCALHOST) {
V4(_) => true,
V6(_) => false,
};
let _ = if let V4(_) = V4(Ipv4Addr::LOCALHOST) {
true
} else {
false
};
ipaddr_const();
let _ = if let V4(_) = gen_ipaddr() {
1
} else if let V6(_) = gen_ipaddr() {
2
} else {
3
};
}
fn gen_ipaddr() -> IpAddr {
V4(Ipv4Addr::LOCALHOST)
}
const fn ipaddr_const() {
if let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
match V4(Ipv4Addr::LOCALHOST) {
V4(_) => true,
V6(_) => false,
};
match V6(Ipv6Addr::LOCALHOST) {
V4(_) => false,
V6(_) => true,
};
}

View file

@ -0,0 +1,130 @@
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:14:12
|
LL | if let V4(_) = &ipaddr {}
| -------^^^^^---------- help: try this: `if ipaddr.is_ipv4()`
|
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:16:12
|
LL | if let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
| -------^^^^^-------------------------- help: try this: `if V4(Ipv4Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:18:12
|
LL | if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
| -------^^^^^-------------------------- help: try this: `if V6(Ipv6Addr::LOCALHOST).is_ipv6()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:20:15
|
LL | while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
| ----------^^^^^-------------------------- help: try this: `while V4(Ipv4Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:22:15
|
LL | while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
| ----------^^^^^-------------------------- help: try this: `while V6(Ipv6Addr::LOCALHOST).is_ipv6()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:32:5
|
LL | / match V4(Ipv4Addr::LOCALHOST) {
LL | | V4(_) => true,
LL | | V6(_) => false,
LL | | };
| |_____^ help: try this: `V4(Ipv4Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:37:5
|
LL | / match V4(Ipv4Addr::LOCALHOST) {
LL | | V4(_) => false,
LL | | V6(_) => true,
LL | | };
| |_____^ help: try this: `V4(Ipv4Addr::LOCALHOST).is_ipv6()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:42:5
|
LL | / match V6(Ipv6Addr::LOCALHOST) {
LL | | V4(_) => false,
LL | | V6(_) => true,
LL | | };
| |_____^ help: try this: `V6(Ipv6Addr::LOCALHOST).is_ipv6()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:47:5
|
LL | / match V6(Ipv6Addr::LOCALHOST) {
LL | | V4(_) => true,
LL | | V6(_) => false,
LL | | };
| |_____^ help: try this: `V6(Ipv6Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:52:20
|
LL | let _ = if let V4(_) = V4(Ipv4Addr::LOCALHOST) {
| -------^^^^^-------------------------- help: try this: `if V4(Ipv4Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:60:20
|
LL | let _ = if let V4(_) = gen_ipaddr() {
| -------^^^^^--------------- help: try this: `if gen_ipaddr().is_ipv4()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:62:19
|
LL | } else if let V6(_) = gen_ipaddr() {
| -------^^^^^--------------- help: try this: `if gen_ipaddr().is_ipv6()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:74:12
|
LL | if let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
| -------^^^^^-------------------------- help: try this: `if V4(Ipv4Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:76:12
|
LL | if let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
| -------^^^^^-------------------------- help: try this: `if V6(Ipv6Addr::LOCALHOST).is_ipv6()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:78:15
|
LL | while let V4(_) = V4(Ipv4Addr::LOCALHOST) {}
| ----------^^^^^-------------------------- help: try this: `while V4(Ipv4Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:80:15
|
LL | while let V6(_) = V6(Ipv6Addr::LOCALHOST) {}
| ----------^^^^^-------------------------- help: try this: `while V6(Ipv6Addr::LOCALHOST).is_ipv6()`
error: redundant pattern matching, consider using `is_ipv4()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:82:5
|
LL | / match V4(Ipv4Addr::LOCALHOST) {
LL | | V4(_) => true,
LL | | V6(_) => false,
LL | | };
| |_____^ help: try this: `V4(Ipv4Addr::LOCALHOST).is_ipv4()`
error: redundant pattern matching, consider using `is_ipv6()`
--> $DIR/redundant_pattern_matching_ipaddr.rs:87:5
|
LL | / match V6(Ipv6Addr::LOCALHOST) {
LL | | V4(_) => false,
LL | | V6(_) => true,
LL | | };
| |_____^ help: try this: `V6(Ipv6Addr::LOCALHOST).is_ipv6()`
error: aborting due to 18 previous errors

View file

@ -37,8 +37,7 @@ fn main() {
let _ = None::<()>.is_none();
let opt = Some(false);
let x = if opt.is_some() { true } else { false };
takes_bool(x);
let _ = if opt.is_some() { true } else { false };
issue6067();
@ -55,8 +54,6 @@ fn gen_opt() -> Option<()> {
None
}
fn takes_bool(_: bool) {}
fn foo() {}
fn bar() {}

View file

@ -46,8 +46,7 @@ fn main() {
};
let opt = Some(false);
let x = if let Some(_) = opt { true } else { false };
takes_bool(x);
let _ = if let Some(_) = opt { true } else { false };
issue6067();
@ -64,8 +63,6 @@ fn gen_opt() -> Option<()> {
None
}
fn takes_bool(_: bool) {}
fn foo() {}
fn bar() {}

View file

@ -73,47 +73,47 @@ LL | | };
error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:49:20
|
LL | let x = if let Some(_) = opt { true } else { false };
LL | let _ = if let Some(_) = opt { true } else { false };
| -------^^^^^^^------ help: try this: `if opt.is_some()`
error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:54:20
--> $DIR/redundant_pattern_matching_option.rs:53:20
|
LL | let _ = if let Some(_) = gen_opt() {
| -------^^^^^^^------------ help: try this: `if gen_opt().is_some()`
error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:56:19
--> $DIR/redundant_pattern_matching_option.rs:55:19
|
LL | } else if let None = gen_opt() {
| -------^^^^------------ help: try this: `if gen_opt().is_none()`
error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:77:12
--> $DIR/redundant_pattern_matching_option.rs:74:12
|
LL | if let Some(_) = Some(42) {}
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`
error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:79:12
--> $DIR/redundant_pattern_matching_option.rs:76:12
|
LL | if let None = None::<()> {}
| -------^^^^------------- help: try this: `if None::<()>.is_none()`
error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:81:15
--> $DIR/redundant_pattern_matching_option.rs:78:15
|
LL | while let Some(_) = Some(42) {}
| ----------^^^^^^^----------- help: try this: `while Some(42).is_some()`
error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:83:15
--> $DIR/redundant_pattern_matching_option.rs:80:15
|
LL | while let None = None::<()> {}
| ----------^^^^------------- help: try this: `while None::<()>.is_none()`
error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:85:5
--> $DIR/redundant_pattern_matching_option.rs:82:5
|
LL | / match Some(42) {
LL | | Some(_) => true,
@ -122,7 +122,7 @@ LL | | };
| |_____^ help: try this: `Some(42).is_some()`
error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:90:5
--> $DIR/redundant_pattern_matching_option.rs:87:5
|
LL | / match None::<()> {
LL | | Some(_) => false,

View file

@ -34,8 +34,7 @@ fn main() {
let _ = Pending::<()>.is_pending();
let poll = Ready(false);
let x = if poll.is_ready() { true } else { false };
takes_poll(x);
let _ = if poll.is_ready() { true } else { false };
poll_const();
@ -52,8 +51,6 @@ fn gen_poll() -> Poll<()> {
Pending
}
fn takes_poll(_: bool) {}
fn foo() {}
fn bar() {}

View file

@ -43,8 +43,7 @@ fn main() {
};
let poll = Ready(false);
let x = if let Ready(_) = poll { true } else { false };
takes_poll(x);
let _ = if let Ready(_) = poll { true } else { false };
poll_const();
@ -61,8 +60,6 @@ fn gen_poll() -> Poll<()> {
Pending
}
fn takes_poll(_: bool) {}
fn foo() {}
fn bar() {}

View file

@ -67,47 +67,47 @@ LL | | };
error: redundant pattern matching, consider using `is_ready()`
--> $DIR/redundant_pattern_matching_poll.rs:46:20
|
LL | let x = if let Ready(_) = poll { true } else { false };
LL | let _ = if let Ready(_) = poll { true } else { false };
| -------^^^^^^^^------- help: try this: `if poll.is_ready()`
error: redundant pattern matching, consider using `is_ready()`
--> $DIR/redundant_pattern_matching_poll.rs:51:20
--> $DIR/redundant_pattern_matching_poll.rs:50:20
|
LL | let _ = if let Ready(_) = gen_poll() {
| -------^^^^^^^^------------- help: try this: `if gen_poll().is_ready()`
error: redundant pattern matching, consider using `is_pending()`
--> $DIR/redundant_pattern_matching_poll.rs:53:19
--> $DIR/redundant_pattern_matching_poll.rs:52:19
|
LL | } else if let Pending = gen_poll() {
| -------^^^^^^^------------- help: try this: `if gen_poll().is_pending()`
error: redundant pattern matching, consider using `is_ready()`
--> $DIR/redundant_pattern_matching_poll.rs:71:12
--> $DIR/redundant_pattern_matching_poll.rs:68:12
|
LL | if let Ready(_) = Ready(42) {}
| -------^^^^^^^^------------ help: try this: `if Ready(42).is_ready()`
error: redundant pattern matching, consider using `is_pending()`
--> $DIR/redundant_pattern_matching_poll.rs:73:12
--> $DIR/redundant_pattern_matching_poll.rs:70:12
|
LL | if let Pending = Pending::<()> {}
| -------^^^^^^^---------------- help: try this: `if Pending::<()>.is_pending()`
error: redundant pattern matching, consider using `is_ready()`
--> $DIR/redundant_pattern_matching_poll.rs:75:15
--> $DIR/redundant_pattern_matching_poll.rs:72:15
|
LL | while let Ready(_) = Ready(42) {}
| ----------^^^^^^^^------------ help: try this: `while Ready(42).is_ready()`
error: redundant pattern matching, consider using `is_pending()`
--> $DIR/redundant_pattern_matching_poll.rs:77:15
--> $DIR/redundant_pattern_matching_poll.rs:74:15
|
LL | while let Pending = Pending::<()> {}
| ----------^^^^^^^---------------- help: try this: `while Pending::<()>.is_pending()`
error: redundant pattern matching, consider using `is_ready()`
--> $DIR/redundant_pattern_matching_poll.rs:79:5
--> $DIR/redundant_pattern_matching_poll.rs:76:5
|
LL | / match Ready(42) {
LL | | Ready(_) => true,
@ -116,7 +116,7 @@ LL | | };
| |_____^ help: try this: `Ready(42).is_ready()`
error: redundant pattern matching, consider using `is_pending()`
--> $DIR/redundant_pattern_matching_poll.rs:84:5
--> $DIR/redundant_pattern_matching_poll.rs:81:5
|
LL | / match Pending::<()> {
LL | | Ready(_) => false,