From 2872b7e0a48e6ae41570804afa88bd12bf5f507a Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Mon, 27 Jun 2022 12:48:38 -0400 Subject: [PATCH] Allow `let () = ..` as type inference for `let_unit_value` --- clippy_lints/src/unit_types/let_unit_value.rs | 19 +++++++++++-------- tests/ui/let_unit.fixed | 2 ++ tests/ui/let_unit.rs | 2 ++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/unit_types/let_unit_value.rs b/clippy_lints/src/unit_types/let_unit_value.rs index c98f9e01a8d..aec028d5c48 100644 --- a/clippy_lints/src/unit_types/let_unit_value.rs +++ b/clippy_lints/src/unit_types/let_unit_value.rs @@ -18,20 +18,23 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) { && !in_external_macro(cx.sess(), local.span) && cx.typeck_results().pat_ty(local.pat).is_unit() { - if local.ty.is_some() && expr_needs_inferred_result(cx, init) { - if !matches!(local.pat.kind, PatKind::Wild) { + if (local.ty.map_or(false, |ty| !matches!(ty.kind, TyKind::Infer)) + || matches!(local.pat.kind, PatKind::Tuple([], None))) + && expr_needs_inferred_result(cx, init) + { + if !matches!(local.pat.kind, PatKind::Wild | PatKind::Tuple([], None)) { span_lint_and_then( cx, LET_UNIT_VALUE, local.span, "this let-binding has unit value", |diag| { - diag.span_suggestion( - local.pat.span, - "use a wild (`_`) binding", - "_", - Applicability::MaybeIncorrect, // snippet - ); + diag.span_suggestion( + local.pat.span, + "use a wild (`_`) binding", + "_", + Applicability::MaybeIncorrect, // snippet + ); }, ); } diff --git a/tests/ui/let_unit.fixed b/tests/ui/let_unit.fixed index 3d2a9635d75..6343cff0f7f 100644 --- a/tests/ui/let_unit.fixed +++ b/tests/ui/let_unit.fixed @@ -163,6 +163,8 @@ fn _returns_generic() { let _: () = opt; } } + + let () = f(); } fn attributes() { diff --git a/tests/ui/let_unit.rs b/tests/ui/let_unit.rs index 625927f76f1..c9bb2849f5c 100644 --- a/tests/ui/let_unit.rs +++ b/tests/ui/let_unit.rs @@ -163,6 +163,8 @@ fn _returns_generic() { let _: () = opt; } } + + let () = f(); } fn attributes() {