Skip to content

Commit 2bb659f

Browse files
committed
Update test for question_mark to cover Result
1 parent 7081554 commit 2bb659f

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

tests/ui/question_mark.fixed

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ fn func() -> Option<i32> {
104104
Some(0)
105105
}
106106

107+
fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
108+
let _ = x?;
109+
110+
x?;
111+
112+
// No warning
113+
let y = if let Ok(x) = x {
114+
x
115+
} else {
116+
return Err("some error");
117+
};
118+
119+
let _ = match x {
120+
Ok(x) => (),
121+
_ => return x,
122+
};
123+
124+
let _ = match x {
125+
Ok(x) => (),
126+
_ => return Err("some error"),
127+
};
128+
129+
Ok(y)
130+
}
131+
107132
fn main() {
108133
some_func(Some(42));
109134
some_func(None);
@@ -123,4 +148,6 @@ fn main() {
123148
returns_something_similar_to_option(so);
124149

125150
func();
151+
152+
result_func(Ok(42));
126153
}

tests/ui/question_mark.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,33 @@ fn func() -> Option<i32> {
134134
Some(0)
135135
}
136136

137+
fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
138+
let _ = if let Ok(x) = x { x } else { return x };
139+
140+
if x.is_err() {
141+
return x;
142+
}
143+
144+
// No warning
145+
let y = if let Ok(x) = x {
146+
x
147+
} else {
148+
return Err("some error");
149+
};
150+
151+
let _ = match x {
152+
Ok(x) => (),
153+
_ => return x,
154+
};
155+
156+
let _ = match x {
157+
Ok(x) => (),
158+
_ => return Err("some error"),
159+
};
160+
161+
Ok(y)
162+
}
163+
137164
fn main() {
138165
some_func(Some(42));
139166
some_func(None);
@@ -153,4 +180,6 @@ fn main() {
153180
returns_something_similar_to_option(so);
154181

155182
func();
183+
184+
result_func(Ok(42));
156185
}

tests/ui/question_mark.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,19 @@ LL | | return None;
100100
LL | | }
101101
| |_____^ help: replace it with: `f()?;`
102102

103-
error: aborting due to 11 previous errors
103+
error: this if-let-else may be rewritten with the `?` operator
104+
--> $DIR/question_mark.rs:138:13
105+
|
106+
LL | let _ = if let Ok(x) = x { x } else { return x };
107+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`
108+
109+
error: this block may be rewritten with the `?` operator
110+
--> $DIR/question_mark.rs:140:5
111+
|
112+
LL | / if x.is_err() {
113+
LL | | return x;
114+
LL | | }
115+
| |_____^ help: replace it with: `x?;`
116+
117+
error: aborting due to 13 previous errors
104118

0 commit comments

Comments
 (0)