Skip to content

Commit 9f27b15

Browse files
committed
Auto merge of rust-lang#11673 - y21:issue11672, r=Manishearth
[`unnecessary_lazy_eval`]: reduce applicability if closure has return type annotation Fixes rust-lang#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
2 parents 387d756 + bb6516a commit 9f27b15

6 files changed

+85
-42
lines changed

clippy_lints/src/methods/unnecessary_lazy_eval.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::{eager_or_lazy, is_from_proc_macro, usage};
5+
use hir::FnRetTy;
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
78
use rustc_lint::LateContext;
@@ -27,7 +28,7 @@ pub(super) fn check<'tcx>(
2728
let is_bool = cx.typeck_results().expr_ty(recv).is_bool();
2829

2930
if is_option || is_result || is_bool {
30-
if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind {
31+
if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl, .. }) = arg.kind {
3132
let body = cx.tcx.hir().body(body);
3233
let body_expr = &body.value;
3334

@@ -48,7 +49,14 @@ pub(super) fn check<'tcx>(
4849
.iter()
4950
// bindings are checked to be unused above
5051
.all(|param| matches!(param.pat.kind, hir::PatKind::Binding(..) | hir::PatKind::Wild))
51-
{
52+
&& matches!(
53+
fn_decl.output,
54+
FnRetTy::DefaultReturn(_)
55+
| FnRetTy::Return(hir::Ty {
56+
kind: hir::TyKind::Infer,
57+
..
58+
})
59+
) {
5260
Applicability::MachineApplicable
5361
} else {
5462
// replacing the lambda may break type inference

tests/ui/unnecessary_lazy_eval.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![allow(clippy::map_identity)]
66
#![allow(clippy::needless_borrow)]
77
#![allow(clippy::unnecessary_literal_unwrap)]
8+
#![allow(clippy::unit_arg)]
89

910
use std::ops::Deref;
1011

@@ -76,6 +77,8 @@ fn main() {
7677
let _ = opt.ok_or(2);
7778
let _ = nested_tuple_opt.unwrap_or(Some((1, 2)));
7879
let _ = cond.then_some(astronomers_pi);
80+
let _ = true.then_some({});
81+
let _ = true.then_some({});
7982

8083
// Should lint - Builtin deref
8184
let r = &1;

tests/ui/unnecessary_lazy_eval.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![allow(clippy::map_identity)]
66
#![allow(clippy::needless_borrow)]
77
#![allow(clippy::unnecessary_literal_unwrap)]
8+
#![allow(clippy::unit_arg)]
89

910
use std::ops::Deref;
1011

@@ -76,6 +77,8 @@ fn main() {
7677
let _ = opt.ok_or_else(|| 2);
7778
let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2)));
7879
let _ = cond.then(|| astronomers_pi);
80+
let _ = true.then(|| -> _ {});
81+
let _ = true.then(|| {});
7982

8083
// Should lint - Builtin deref
8184
let r = &1;

0 commit comments

Comments
 (0)