about summary refs log tree commit diff
path: root/clippy_lints/src/methods/unnecessary_lazy_eval.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-10-16 16:27:01 +0000
committerbors <bors@rust-lang.org>2023-10-16 16:27:01 +0000
commit9f27b1562cc8d6f6449cd3c94d89b410b51defc2 (patch)
treeab21b4c7838c36bd40fa631c1fa360724bf33abe /clippy_lints/src/methods/unnecessary_lazy_eval.rs
parent387d756540f63c84033bee6c72daf4152848aefa (diff)
parentbb6516ace013a2ec33ca1276b029786dfbb46ee1 (diff)
downloadrust-9f27b1562cc8d6f6449cd3c94d89b410b51defc2.tar.gz
rust-9f27b1562cc8d6f6449cd3c94d89b410b51defc2.zip
Auto merge of #11673 - y21:issue11672, r=Manishearth
[`unnecessary_lazy_eval`]: reduce applicability if closure has return type annotation

Fixes #11672

We already check if closure parameters don't have type annotations and reduce the applicability to `MaybeIncorrect` if they do, since those help type inference and removing them breaks code. We didn't do this for return type annotations however. This PR adds it. This doesn't change it to produce a fix that will compile, but it will prevent rustfix from auto-applying it.

(In general I'm not sure if we can suggest a fix that will compile. In this specific example, it might be possible to suggest `&[] as &[u8]`, but as-casts won't always work, e.g. `Default::default() as &[u8]` is a compile error, so just reducing applicability should be a safe fix in any case for now)

changelog: [`unnecessary_lazy_eval`]: reduce applicability to `MaybeIncorrect` if closure has return type annotation
Diffstat (limited to 'clippy_lints/src/methods/unnecessary_lazy_eval.rs')
-rw-r--r--clippy_lints/src/methods/unnecessary_lazy_eval.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/unnecessary_lazy_eval.rs b/clippy_lints/src/methods/unnecessary_lazy_eval.rs
index 47e2e744112..4a651396f14 100644
--- a/clippy_lints/src/methods/unnecessary_lazy_eval.rs
+++ b/clippy_lints/src/methods/unnecessary_lazy_eval.rs
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::source::snippet;
 use clippy_utils::ty::is_type_diagnostic_item;
 use clippy_utils::{eager_or_lazy, is_from_proc_macro, usage};
+use hir::FnRetTy;
 use rustc_errors::Applicability;
 use rustc_hir as hir;
 use rustc_lint::LateContext;
@@ -27,7 +28,7 @@ pub(super) fn check<'tcx>(
     let is_bool = cx.typeck_results().expr_ty(recv).is_bool();
 
     if is_option || is_result || is_bool {
-        if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind {
+        if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl, .. }) = arg.kind {
             let body = cx.tcx.hir().body(body);
             let body_expr = &body.value;
 
@@ -48,7 +49,14 @@ pub(super) fn check<'tcx>(
                     .iter()
                     // bindings are checked to be unused above
                     .all(|param| matches!(param.pat.kind, hir::PatKind::Binding(..) | hir::PatKind::Wild))
-                {
+                    && matches!(
+                        fn_decl.output,
+                        FnRetTy::DefaultReturn(_)
+                            | FnRetTy::Return(hir::Ty {
+                                kind: hir::TyKind::Infer,
+                                ..
+                            })
+                    ) {
                     Applicability::MachineApplicable
                 } else {
                     // replacing the lambda may break type inference