Skip to content

Commit 2fd1682

Browse files
committed
Update question_mark test to behave better
1 parent a91ec59 commit 2fd1682

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

tests/ui/question_mark.fixed

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
#![allow(unreachable_code)]
33
#![allow(clippy::unnecessary_wraps)]
44

5-
use std::env;
6-
use std::path::PathBuf;
7-
85
fn some_func(a: Option<u32>) -> Option<u32> {
96
a?;
107

@@ -107,30 +104,31 @@ fn func() -> Option<i32> {
107104
Some(0)
108105
}
109106

110-
fn func_returning_result() -> Result<i32, String> {
107+
fn func_returning_result() -> Result<i32, i32> {
111108
Ok(1)
112109
}
113110

114-
fn result_func(x: Result<i32, String>) -> Result<i32, String> {
111+
fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
115112
let _ = x?;
116113

117-
x.as_ref()?;
114+
x?;
118115

119116
// No warning
120117
let y = if let Ok(x) = x {
121118
x
122119
} else {
123-
return Err("some error".to_string());
120+
return Err(0);
124121
};
125122

126123
// issue #7859
127124
// no warning
128125
let _ = if let Ok(x) = func_returning_result() {
129126
x
130127
} else {
131-
return Err("some error".to_string());
128+
return Err(0);
132129
};
133130

131+
// no warning
134132
if func_returning_result().is_err() {
135133
return func_returning_result();
136134
}

tests/ui/question_mark.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
#![allow(unreachable_code)]
33
#![allow(clippy::unnecessary_wraps)]
44

5-
use std::env;
6-
use std::path::PathBuf;
7-
85
fn some_func(a: Option<u32>) -> Option<u32> {
96
if a.is_none() {
107
return None;
@@ -137,11 +134,11 @@ fn func() -> Option<i32> {
137134
Some(0)
138135
}
139136

140-
fn func_returning_result() -> Result<i32, String> {
137+
fn func_returning_result() -> Result<i32, i32> {
141138
Ok(1)
142139
}
143140

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

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

158155
// issue #7859
159156
// no warning
160157
let _ = if let Ok(x) = func_returning_result() {
161158
x
162159
} else {
163-
return Err("some error".to_string());
160+
return Err(0);
164161
};
165162

163+
// no warning
166164
if func_returning_result().is_err() {
167165
return func_returning_result();
168166
}

tests/ui/question_mark.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this block may be rewritten with the `?` operator
2-
--> $DIR/question_mark.rs:9:5
2+
--> $DIR/question_mark.rs:6:5
33
|
44
LL | / if a.is_none() {
55
LL | | return None;
@@ -9,23 +9,23 @@ LL | | }
99
= note: `-D clippy::question-mark` implied by `-D warnings`
1010

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

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

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

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

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

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

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

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

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

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

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

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

117117
error: aborting due to 13 previous errors
118118

0 commit comments

Comments
 (0)