Skip to content

Commit 00cf07b

Browse files
committed
[option_if_let_else] do not lint if any arm has guard
1 parent cca9938 commit 00cf07b

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

clippy_lints/src/option_if_let_else.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,14 @@ fn try_convert_match<'tcx>(
213213
cx: &LateContext<'tcx>,
214214
arms: &[Arm<'tcx>],
215215
) -> Option<(&'tcx Pat<'tcx>, &'tcx Expr<'tcx>, &'tcx Expr<'tcx>)> {
216-
if arms.len() == 2 {
217-
return if is_none_or_err_arm(cx, &arms[1]) {
218-
Some((arms[0].pat, arms[0].body, arms[1].body))
219-
} else if is_none_or_err_arm(cx, &arms[0]) {
220-
Some((arms[1].pat, arms[1].body, arms[0].body))
216+
if let [first_arm, second_arm] = arms
217+
&& first_arm.guard.is_none()
218+
&& second_arm.guard.is_none()
219+
{
220+
return if is_none_or_err_arm(cx, second_arm) {
221+
Some((first_arm.pat, first_arm.body, second_arm.body))
222+
} else if is_none_or_err_arm(cx, first_arm) {
223+
Some((second_arm.pat, second_arm.body, first_arm.body))
221224
} else {
222225
None
223226
};

tests/ui/option_if_let_else.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,12 @@ fn main() {
189189
let _ = res.map_or(1, |a| a + 1);
190190
let _ = res.map_or(5, |a| a + 1);
191191
}
192+
193+
#[allow(dead_code)]
194+
fn issue9742() -> Option<&'static str> {
195+
// should not lint because of guards
196+
match Some("foo ") {
197+
Some(name) if name.starts_with("foo") => Some(name.trim()),
198+
_ => None,
199+
}
200+
}

tests/ui/option_if_let_else.rs

+9
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,12 @@ fn main() {
230230
};
231231
let _ = if let Ok(a) = res { a + 1 } else { 5 };
232232
}
233+
234+
#[allow(dead_code)]
235+
fn issue9742() -> Option<&'static str> {
236+
// should not lint because of guards
237+
match Some("foo ") {
238+
Some(name) if name.starts_with("foo") => Some(name.trim()),
239+
_ => None,
240+
}
241+
}

0 commit comments

Comments
 (0)