Skip to content

Commit f5d225d

Browse files
committed
Auto merge of #9722 - ebobrow:question-mark, r=Manishearth
`question_mark` don't lint on `if let Err` with `else` cc #9518 AFAICT the only time this would be a valid suggestion is the rather esoteric ```rust let _ = if let Err(e) = x { return Err(e); } else { // no side effects x.unwrap() } ``` which doesn't seem worth checking to me. Please correct me if I'm missing something. changelog: [`question_mark`] don't lint on `if let Err` with `else`
2 parents 710999d + 98250af commit f5d225d

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_
189189
&& expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, Some(let_pat_sym)))
190190
|| is_res_lang_ctor(cx, res, ResultErr)
191191
&& expr_return_none_or_err(smbl, cx, if_then, let_expr, Some(let_pat_sym))
192+
&& if_else.is_none()
192193
},
193194
_ => false,
194195
}

tests/ui/question_mark.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
134134
return func_returning_result();
135135
}
136136

137+
// no warning
138+
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };
139+
137140
Ok(y)
138141
}
139142

tests/ui/question_mark.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
166166
return func_returning_result();
167167
}
168168

169+
// no warning
170+
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };
171+
169172
Ok(y)
170173
}
171174

tests/ui/question_mark.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ LL | | }
115115
| |_____^ help: replace it with: `x?;`
116116

117117
error: this block may be rewritten with the `?` operator
118-
--> $DIR/question_mark.rs:193:5
118+
--> $DIR/question_mark.rs:196:5
119119
|
120120
LL | / if let Err(err) = func_returning_result() {
121121
LL | | return Err(err);
122122
LL | | }
123123
| |_____^ help: replace it with: `func_returning_result()?;`
124124

125125
error: this block may be rewritten with the `?` operator
126-
--> $DIR/question_mark.rs:200:5
126+
--> $DIR/question_mark.rs:203:5
127127
|
128128
LL | / if let Err(err) = func_returning_result() {
129129
LL | | return Err(err);

0 commit comments

Comments
 (0)