Skip to content

Commit cf33eda

Browse files
committed
fix: option_if_let_else don't suggest argless function for Result::map_or_else
closes rust-clippy/issues/15002 Signed-off-by: Zihan <[email protected]>
1 parent b8c16e4 commit cf33eda

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

clippy_lints/src/option_if_let_else.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn try_get_option_occurrence<'tcx>(
225225
let mut app = Applicability::Unspecified;
226226

227227
let (none_body, is_argless_call) = match none_body.kind {
228-
ExprKind::Call(call_expr, []) if !none_body.span.from_expansion() => (call_expr, true),
228+
ExprKind::Call(call_expr, []) if !none_body.span.from_expansion() && !is_result => (call_expr, true),
229229
_ => (none_body, false),
230230
};
231231

@@ -242,7 +242,7 @@ fn try_get_option_occurrence<'tcx>(
242242
),
243243
none_expr: format!(
244244
"{}{}",
245-
if method_sugg == "map_or" || is_argless_call {
245+
if method_sugg == "map_or" || (is_argless_call && !is_result) {
246246
""
247247
} else if is_result {
248248
"|_| "

tests/ui/option_if_let_else.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,8 @@ fn issue15379() {
309309
let _ = unsafe { (*opt_raw_ptr).map_or(1, |o| o) };
310310
//~^ option_if_let_else
311311
}
312+
313+
fn issue15002() {
314+
let res: Result<String, ()> = Ok("_".to_string());
315+
let _ = res.map_or_else(|_| String::new(), |s| s.clone());
316+
}

tests/ui/option_if_let_else.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,12 @@ fn issue15379() {
372372
let _ = unsafe { if let Some(o) = *opt_raw_ptr { o } else { 1 } };
373373
//~^ option_if_let_else
374374
}
375+
376+
fn issue15002() {
377+
let res: Result<String, ()> = Ok("_".to_string());
378+
let _ = match res {
379+
//~^ option_if_let_else
380+
Ok(s) => s.clone(),
381+
Err(_) => String::new(),
382+
};
383+
}

tests/ui/option_if_let_else.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,16 @@ error: use Option::map_or instead of an if let/else
340340
LL | let _ = unsafe { if let Some(o) = *opt_raw_ptr { o } else { 1 } };
341341
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(*opt_raw_ptr).map_or(1, |o| o)`
342342

343-
error: aborting due to 26 previous errors
343+
error: use Option::map_or_else instead of an if let/else
344+
--> tests/ui/option_if_let_else.rs:378:13
345+
|
346+
LL | let _ = match res {
347+
| _____________^
348+
LL | |
349+
LL | | Ok(s) => s.clone(),
350+
LL | | Err(_) => String::new(),
351+
LL | | };
352+
| |_____^ help: try: `res.map_or_else(|_| String::new(), |s| s.clone())`
353+
354+
error: aborting due to 27 previous errors
344355

0 commit comments

Comments
 (0)