Skip to content

Commit bb6516a

Browse files
committed
[unnecessary_lazy_eval]: don't emit autofix suggestion if closure has return type
1 parent c40359d commit bb6516a

6 files changed

+85
-42
lines changed

clippy_lints/src/methods/unnecessary_lazy_eval.rs

Lines changed: 10 additions & 2 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)