about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-11-07 10:01:46 +0000
committerbors <bors@rust-lang.org>2020-11-07 10:01:46 +0000
commit4bbef42c4890f9b2d6a742872e525b7b548fd019 (patch)
tree708b22ef7f541e2d5fda3201ddf5d4a001e7574a
parent2ea08e1c534455c2867c34ed51582f900861a084 (diff)
parenta6611de75a9c6cf8b0ccfa371491a646a743667f (diff)
downloadrust-4bbef42c4890f9b2d6a742872e525b7b548fd019.tar.gz
rust-4bbef42c4890f9b2d6a742872e525b7b548fd019.zip
Auto merge of #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 #6240
-rw-r--r--clippy_lints/src/methods/unnecessary_lazy_eval.rs13
-rw-r--r--tests/ui/unnecessary_lazy_eval_unfixable.rs18
-rw-r--r--tests/ui/unnecessary_lazy_eval_unfixable.stderr22
3 files changed, 52 insertions, 1 deletions
diff --git a/clippy_lints/src/methods/unnecessary_lazy_eval.rs b/clippy_lints/src/methods/unnecessary_lazy_eval.rs
index cde89983a26..a867bdb326d 100644
--- a/clippy_lints/src/methods/unnecessary_lazy_eval.rs
+++ b/clippy_lints/src/methods/unnecessary_lazy_eval.rs
@@ -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,
@@ -46,7 +57,7 @@ pub(super) fn lint<'tcx>(
                         simplify_using,
                         snippet(cx, body_expr.span, ".."),
                     ),
-                    Applicability::MachineApplicable,
+                    applicability,
                 );
             }
         }
diff --git a/tests/ui/unnecessary_lazy_eval_unfixable.rs b/tests/ui/unnecessary_lazy_eval_unfixable.rs
new file mode 100644
index 00000000000..2e923bc97a2
--- /dev/null
+++ b/tests/ui/unnecessary_lazy_eval_unfixable.rs
@@ -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);
+}
diff --git a/tests/ui/unnecessary_lazy_eval_unfixable.stderr b/tests/ui/unnecessary_lazy_eval_unfixable.stderr
new file mode 100644
index 00000000000..581d641cbf5
--- /dev/null
+++ b/tests/ui/unnecessary_lazy_eval_unfixable.stderr
@@ -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
+