Skip to content

Commit c0d1002

Browse files
committed
Fix unnecessary_lazy_eval suggestion applicability
Fixes rust-lang#6240
1 parent 0be6544 commit c0d1002

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

clippy_lints/src/methods/unnecessary_lazy_eval.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ pub(super) fn lint<'tcx>(
3232
} else {
3333
"unnecessary closure used to substitute value for `Result::Err`"
3434
};
35+
let applicability = if body
36+
.params
37+
.iter()
38+
.all(|param| matches!(param.pat.kind, hir::PatKind::Wild))
39+
{
40+
Applicability::MachineApplicable
41+
} else {
42+
// replacing the lambda may break type inference
43+
Applicability::MaybeIncorrect
44+
};
3545

3646
span_lint_and_sugg(
3747
cx,
@@ -45,7 +55,7 @@ pub(super) fn lint<'tcx>(
4555
simplify_using,
4656
snippet(cx, body_expr.span, ".."),
4757
),
48-
Applicability::MachineApplicable,
58+
applicability,
4959
);
5060
}
5161
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::unnecessary_lazy_evaluations)]
2+
3+
struct Deep(Option<usize>);
4+
5+
#[derive(Copy, Clone)]
6+
struct SomeStruct {
7+
some_field: usize,
8+
}
9+
10+
fn main() {
11+
// fix will break type inference
12+
let _ = Ok(1).unwrap_or_else(|()| 2);
13+
mod e {
14+
pub struct E;
15+
}
16+
let _ = Ok(1).unwrap_or_else(|e::E| 2);
17+
let _ = Ok(1).unwrap_or_else(|SomeStruct { .. }| 2);
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: unnecessary closure used to substitute value for `Result::Err`
2+
--> $DIR/unnecessary_lazy_eval_unfixable.rs:12:13
3+
|
4+
LL | let _ = Ok(1).unwrap_or_else(|()| 2);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `Ok(1).unwrap_or(2)`
6+
|
7+
= note: `-D clippy::unnecessary-lazy-evaluations` implied by `-D warnings`
8+
9+
error: unnecessary closure used to substitute value for `Result::Err`
10+
--> $DIR/unnecessary_lazy_eval_unfixable.rs:16:13
11+
|
12+
LL | let _ = Ok(1).unwrap_or_else(|e::E| 2);
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `Ok(1).unwrap_or(2)`
14+
15+
error: unnecessary closure used to substitute value for `Result::Err`
16+
--> $DIR/unnecessary_lazy_eval_unfixable.rs:17:13
17+
|
18+
LL | let _ = Ok(1).unwrap_or_else(|SomeStruct { .. }| 2);
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `Ok(1).unwrap_or(2)`
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)