Skip to content

Commit

Permalink
Auto merge of #6272 - camsteffen:unnecesary-lazy-eval-type, r=llogiq
Browse files Browse the repository at this point in the history
Fix unnecessary_lazy_eval suggestion applicability

changelog: Fix unnecessary_lazy_eval suggestion applicability when breaking type inference

Fixes #6240
  • Loading branch information
bors committed Nov 7, 2020
2 parents 2ea08e1 + a6611de commit 4bbef42
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
13 changes: 12 additions & 1 deletion clippy_lints/src/methods/unnecessary_lazy_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ pub(super) fn lint<'tcx>(
} else {
"unnecessary closure used to substitute value for `Result::Err`"
};
let applicability = if body
.params
.iter()
// bindings are checked to be unused above
.all(|param| matches!(param.pat.kind, hir::PatKind::Binding(..) | hir::PatKind::Wild))
{
Applicability::MachineApplicable
} else {
// replacing the lambda may break type inference
Applicability::MaybeIncorrect
};

span_lint_and_sugg(
cx,
Expand All @@ -46,7 +57,7 @@ pub(super) fn lint<'tcx>(
simplify_using,
snippet(cx, body_expr.span, ".."),
),
Applicability::MachineApplicable,
applicability,
);
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/unnecessary_lazy_eval_unfixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![warn(clippy::unnecessary_lazy_evaluations)]

struct Deep(Option<usize>);

#[derive(Copy, Clone)]
struct SomeStruct {
some_field: usize,
}

fn main() {
// fix will break type inference
let _ = Ok(1).unwrap_or_else(|()| 2);
mod e {
pub struct E;
}
let _ = Ok(1).unwrap_or_else(|e::E| 2);
let _ = Ok(1).unwrap_or_else(|SomeStruct { .. }| 2);
}
22 changes: 22 additions & 0 deletions tests/ui/unnecessary_lazy_eval_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval_unfixable.rs:12:13
|
LL | let _ = Ok(1).unwrap_or_else(|()| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `Ok(1).unwrap_or(2)`
|
= note: `-D clippy::unnecessary-lazy-evaluations` implied by `-D warnings`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval_unfixable.rs:16:13
|
LL | let _ = Ok(1).unwrap_or_else(|e::E| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `Ok(1).unwrap_or(2)`

error: unnecessary closure used to substitute value for `Result::Err`
--> $DIR/unnecessary_lazy_eval_unfixable.rs:17:13
|
LL | let _ = Ok(1).unwrap_or_else(|SomeStruct { .. }| 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Use `unwrap_or` instead: `Ok(1).unwrap_or(2)`

error: aborting due to 3 previous errors

0 comments on commit 4bbef42

Please sign in to comment.