Skip to content

Commit 4bbef42

Browse files
committed
Auto merge of rust-lang#6272 - camsteffen:unnecesary-lazy-eval-type, r=llogiq
Fix unnecessary_lazy_eval suggestion applicability changelog: Fix unnecessary_lazy_eval suggestion applicability when breaking type inference Fixes rust-lang#6240
2 parents 2ea08e1 + a6611de commit 4bbef42

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

clippy_lints/src/methods/unnecessary_lazy_eval.rs

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

3748
span_lint_and_sugg(
3849
cx,
@@ -46,7 +57,7 @@ pub(super) fn lint<'tcx>(
4657
simplify_using,
4758
snippet(cx, body_expr.span, ".."),
4859
),
49-
Applicability::MachineApplicable,
60+
applicability,
5061
);
5162
}
5263
}
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)