manual-unwrap-or / pr remarks

This commit is contained in:
Tim Nielens 2020-10-22 22:46:10 +02:00
parent 915ce36087
commit f2da0c701e
3 changed files with 82 additions and 40 deletions

View file

@ -1,5 +1,6 @@
// run-rustfix
#![allow(dead_code)]
#![allow(unused_variables)]
fn option_unwrap_or() {
// int case
@ -67,44 +68,58 @@ fn option_unwrap_or() {
fn result_unwrap_or() {
// int case
Ok::<i32, &str>(1).unwrap_or(42);
// int case, suggestion must surround with parenthesis
(Ok(1) as Result<i32, &str>).unwrap_or(42);
// int case reversed
(Ok(1) as Result<i32, &str>).unwrap_or(42);
Ok::<i32, &str>(1).unwrap_or(42);
// richer none expr
(Ok(1) as Result<i32, &str>).unwrap_or(1 + 42);
Ok::<i32, &str>(1).unwrap_or(1 + 42);
// multiline case
#[rustfmt::skip]
(Ok(1) as Result<i32, &str>).unwrap_or({
Ok::<i32, &str>(1).unwrap_or({
42 + 42
+ 42 + 42 + 42
+ 42 + 42 + 42
});
// string case
(Ok("Bob") as Result<&str, &str>).unwrap_or("Alice");
Ok::<&str, &str>("Bob").unwrap_or("Alice");
// don't lint
match Ok(1) as Result<i32, &str> {
match Ok::<i32, &str>(1) {
Ok(i) => i + 2,
Err(_) => 42,
};
match Ok(1) as Result<i32, &str> {
match Ok::<i32, &str>(1) {
Ok(i) => i,
Err(_) => return,
};
for j in 0..4 {
match Ok(j) as Result<i32, &str> {
match Ok::<i32, &str>(j) {
Ok(i) => i,
Err(_) => continue,
};
match Ok(j) as Result<i32, &str> {
match Ok::<i32, &str>(j) {
Ok(i) => i,
Err(_) => break,
};
}
// don't lint, Err value is used
match Ok::<&str, &str>("Alice") {
Ok(s) => s,
Err(s) => s,
};
// could lint, but unused_variables takes care of it
match Ok::<&str, &str>("Alice") {
Ok(s) => s,
Err(s) => "Bob",
};
}
fn main() {}

View file

@ -1,5 +1,6 @@
// run-rustfix
#![allow(dead_code)]
#![allow(unused_variables)]
fn option_unwrap_or() {
// int case
@ -82,26 +83,32 @@ fn option_unwrap_or() {
fn result_unwrap_or() {
// int case
match Ok::<i32, &str>(1) {
Ok(i) => i,
Err(_) => 42,
};
// int case, suggestion must surround with parenthesis
match Ok(1) as Result<i32, &str> {
Ok(i) => i,
Err(_) => 42,
};
// int case reversed
match Ok(1) as Result<i32, &str> {
match Ok::<i32, &str>(1) {
Err(_) => 42,
Ok(i) => i,
};
// richer none expr
match Ok(1) as Result<i32, &str> {
match Ok::<i32, &str>(1) {
Ok(i) => i,
Err(_) => 1 + 42,
};
// multiline case
#[rustfmt::skip]
match Ok(1) as Result<i32, &str> {
match Ok::<i32, &str>(1) {
Ok(i) => i,
Err(_) => {
42 + 42
@ -111,30 +118,41 @@ fn result_unwrap_or() {
};
// string case
match Ok("Bob") as Result<&str, &str> {
match Ok::<&str, &str>("Bob") {
Ok(i) => i,
Err(_) => "Alice",
};
// don't lint
match Ok(1) as Result<i32, &str> {
match Ok::<i32, &str>(1) {
Ok(i) => i + 2,
Err(_) => 42,
};
match Ok(1) as Result<i32, &str> {
match Ok::<i32, &str>(1) {
Ok(i) => i,
Err(_) => return,
};
for j in 0..4 {
match Ok(j) as Result<i32, &str> {
match Ok::<i32, &str>(j) {
Ok(i) => i,
Err(_) => continue,
};
match Ok(j) as Result<i32, &str> {
match Ok::<i32, &str>(j) {
Ok(i) => i,
Err(_) => break,
};
}
// don't lint, Err value is used
match Ok::<&str, &str>("Alice") {
Ok(s) => s,
Err(s) => s,
};
// could lint, but unused_variables takes care of it
match Ok::<&str, &str>("Alice") {
Ok(s) => s,
Err(s) => "Bob",
};
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: this pattern reimplements `Option::unwrap_or`
--> $DIR/manual_unwrap_or.rs:6:5
--> $DIR/manual_unwrap_or.rs:7:5
|
LL | / match Some(1) {
LL | | Some(i) => i,
@ -10,7 +10,7 @@ LL | | };
= note: `-D clippy::manual-unwrap-or` implied by `-D warnings`
error: this pattern reimplements `Option::unwrap_or`
--> $DIR/manual_unwrap_or.rs:12:5
--> $DIR/manual_unwrap_or.rs:13:5
|
LL | / match Some(1) {
LL | | None => 42,
@ -19,7 +19,7 @@ LL | | };
| |_____^ help: replace with: `Some(1).unwrap_or(42)`
error: this pattern reimplements `Option::unwrap_or`
--> $DIR/manual_unwrap_or.rs:18:5
--> $DIR/manual_unwrap_or.rs:19:5
|
LL | / match Some(1) {
LL | | Some(i) => i,
@ -28,7 +28,7 @@ LL | | };
| |_____^ help: replace with: `Some(1).unwrap_or(1 + 42)`
error: this pattern reimplements `Option::unwrap_or`
--> $DIR/manual_unwrap_or.rs:25:5
--> $DIR/manual_unwrap_or.rs:26:5
|
LL | / match Some(1) {
LL | | Some(i) => i,
@ -49,7 +49,7 @@ LL | });
|
error: this pattern reimplements `Option::unwrap_or`
--> $DIR/manual_unwrap_or.rs:35:5
--> $DIR/manual_unwrap_or.rs:36:5
|
LL | / match Some("Bob") {
LL | | Some(i) => i,
@ -58,7 +58,16 @@ LL | | };
| |_____^ help: replace with: `Some("Bob").unwrap_or("Alice")`
error: this pattern reimplements `Result::unwrap_or`
--> $DIR/manual_unwrap_or.rs:85:5
--> $DIR/manual_unwrap_or.rs:86:5
|
LL | / match Ok::<i32, &str>(1) {
LL | | Ok(i) => i,
LL | | Err(_) => 42,
LL | | };
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
error: this pattern reimplements `Result::unwrap_or`
--> $DIR/manual_unwrap_or.rs:92:5
|
LL | / match Ok(1) as Result<i32, &str> {
LL | | Ok(i) => i,
@ -67,27 +76,27 @@ LL | | };
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
error: this pattern reimplements `Result::unwrap_or`
--> $DIR/manual_unwrap_or.rs:91:5
--> $DIR/manual_unwrap_or.rs:98:5
|
LL | / match Ok(1) as Result<i32, &str> {
LL | / match Ok::<i32, &str>(1) {
LL | | Err(_) => 42,
LL | | Ok(i) => i,
LL | | };
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
error: this pattern reimplements `Result::unwrap_or`
--> $DIR/manual_unwrap_or.rs:97:5
|
LL | / match Ok(1) as Result<i32, &str> {
LL | | Ok(i) => i,
LL | | Err(_) => 1 + 42,
LL | | };
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(1 + 42)`
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
error: this pattern reimplements `Result::unwrap_or`
--> $DIR/manual_unwrap_or.rs:104:5
|
LL | / match Ok(1) as Result<i32, &str> {
LL | / match Ok::<i32, &str>(1) {
LL | | Ok(i) => i,
LL | | Err(_) => 1 + 42,
LL | | };
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(1 + 42)`
error: this pattern reimplements `Result::unwrap_or`
--> $DIR/manual_unwrap_or.rs:111:5
|
LL | / match Ok::<i32, &str>(1) {
LL | | Ok(i) => i,
LL | | Err(_) => {
LL | | 42 + 42
@ -98,7 +107,7 @@ LL | | };
|
help: replace with
|
LL | (Ok(1) as Result<i32, &str>).unwrap_or({
LL | Ok::<i32, &str>(1).unwrap_or({
LL | 42 + 42
LL | + 42 + 42 + 42
LL | + 42 + 42 + 42
@ -106,13 +115,13 @@ LL | });
|
error: this pattern reimplements `Result::unwrap_or`
--> $DIR/manual_unwrap_or.rs:114:5
--> $DIR/manual_unwrap_or.rs:121:5
|
LL | / match Ok("Bob") as Result<&str, &str> {
LL | / match Ok::<&str, &str>("Bob") {
LL | | Ok(i) => i,
LL | | Err(_) => "Alice",
LL | | };
| |_____^ help: replace with: `(Ok("Bob") as Result<&str, &str>).unwrap_or("Alice")`
| |_____^ help: replace with: `Ok::<&str, &str>("Bob").unwrap_or("Alice")`
error: aborting due to 10 previous errors
error: aborting due to 11 previous errors