Skip to content

Commit

Permalink
Update question_mark test to behave better
Browse files Browse the repository at this point in the history
  • Loading branch information
dswij committed Oct 22, 2021
1 parent 8d9bf87 commit a6bbbb9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
14 changes: 6 additions & 8 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#![allow(unreachable_code)]
#![allow(clippy::unnecessary_wraps)]

use std::env;
use std::path::PathBuf;

fn some_func(a: Option<u32>) -> Option<u32> {
a?;

Expand Down Expand Up @@ -107,30 +104,31 @@ fn func() -> Option<i32> {
Some(0)
}

fn func_returning_result() -> Result<i32, String> {
fn func_returning_result() -> Result<i32, i32> {
Ok(1)
}

fn result_func(x: Result<i32, String>) -> Result<i32, String> {
fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
let _ = x?;

x.as_ref()?;
x?;

// No warning
let y = if let Ok(x) = x {
x
} else {
return Err("some error".to_string());
return Err(0);
};

// issue #7859
// no warning
let _ = if let Ok(x) = func_returning_result() {
x
} else {
return Err("some error".to_string());
return Err(0);
};

// no warning
if func_returning_result().is_err() {
return func_returning_result();
}
Expand Down
12 changes: 5 additions & 7 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#![allow(unreachable_code)]
#![allow(clippy::unnecessary_wraps)]

use std::env;
use std::path::PathBuf;

fn some_func(a: Option<u32>) -> Option<u32> {
if a.is_none() {
return None;
Expand Down Expand Up @@ -137,11 +134,11 @@ fn func() -> Option<i32> {
Some(0)
}

fn func_returning_result() -> Result<i32, String> {
fn func_returning_result() -> Result<i32, i32> {
Ok(1)
}

fn result_func(x: Result<i32, String>) -> Result<i32, String> {
fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
let _ = if let Ok(x) = x { x } else { return x };

if x.is_err() {
Expand All @@ -152,17 +149,18 @@ fn result_func(x: Result<i32, String>) -> Result<i32, String> {
let y = if let Ok(x) = x {
x
} else {
return Err("some error".to_string());
return Err(0);
};

// issue #7859
// no warning
let _ = if let Ok(x) = func_returning_result() {
x
} else {
return Err("some error".to_string());
return Err(0);
};

// no warning
if func_returning_result().is_err() {
return func_returning_result();
}
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:9:5
--> $DIR/question_mark.rs:6:5
|
LL | / if a.is_none() {
LL | | return None;
Expand All @@ -9,23 +9,23 @@ LL | | }
= note: `-D clippy::question-mark` implied by `-D warnings`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:54:9
--> $DIR/question_mark.rs:51:9
|
LL | / if (self.opt).is_none() {
LL | | return None;
LL | | }
| |_________^ help: replace it with: `(self.opt)?;`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:58:9
--> $DIR/question_mark.rs:55:9
|
LL | / if self.opt.is_none() {
LL | | return None
LL | | }
| |_________^ help: replace it with: `self.opt?;`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:62:17
--> $DIR/question_mark.rs:59:17
|
LL | let _ = if self.opt.is_none() {
| _________________^
Expand All @@ -36,7 +36,7 @@ LL | | };
| |_________^ help: replace it with: `Some(self.opt?)`

error: this if-let-else may be rewritten with the `?` operator
--> $DIR/question_mark.rs:68:17
--> $DIR/question_mark.rs:65:17
|
LL | let _ = if let Some(x) = self.opt {
| _________________^
Expand All @@ -47,31 +47,31 @@ LL | | };
| |_________^ help: replace it with: `self.opt?`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:85:9
--> $DIR/question_mark.rs:82:9
|
LL | / if self.opt.is_none() {
LL | | return None;
LL | | }
| |_________^ help: replace it with: `self.opt.as_ref()?;`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:93:9
--> $DIR/question_mark.rs:90:9
|
LL | / if self.opt.is_none() {
LL | | return None;
LL | | }
| |_________^ help: replace it with: `self.opt.as_ref()?;`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:101:9
--> $DIR/question_mark.rs:98:9
|
LL | / if self.opt.is_none() {
LL | | return None;
LL | | }
| |_________^ help: replace it with: `self.opt.as_ref()?;`

error: this if-let-else may be rewritten with the `?` operator
--> $DIR/question_mark.rs:108:26
--> $DIR/question_mark.rs:105:26
|
LL | let v: &Vec<_> = if let Some(ref v) = self.opt {
| __________________________^
Expand All @@ -82,7 +82,7 @@ LL | | };
| |_________^ help: replace it with: `self.opt.as_ref()?`

error: this if-let-else may be rewritten with the `?` operator
--> $DIR/question_mark.rs:118:17
--> $DIR/question_mark.rs:115:17
|
LL | let v = if let Some(v) = self.opt {
| _________________^
Expand All @@ -93,26 +93,26 @@ LL | | };
| |_________^ help: replace it with: `self.opt?`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:133:5
--> $DIR/question_mark.rs:130:5
|
LL | / if f().is_none() {
LL | | return None;
LL | | }
| |_____^ help: replace it with: `f()?;`

error: this if-let-else may be rewritten with the `?` operator
--> $DIR/question_mark.rs:145:13
--> $DIR/question_mark.rs:142:13
|
LL | let _ = if let Ok(x) = x { x } else { return x };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:147:5
--> $DIR/question_mark.rs:144:5
|
LL | / if x.is_err() {
LL | | return x;
LL | | }
| |_____^ help: replace it with: `x.as_ref()?;`
| |_____^ help: replace it with: `x?;`

error: aborting due to 13 previous errors

0 comments on commit a6bbbb9

Please sign in to comment.