diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2022-06-27 12:48:38 -0400 |
|---|---|---|
| committer | Jason Newcomb <jsnewcomb@pm.me> | 2022-07-07 20:06:37 -0400 |
| commit | 2872b7e0a48e6ae41570804afa88bd12bf5f507a (patch) | |
| tree | 19d4dd370b8072701c92e681ec906029e78b71ce | |
| parent | 196174ddad8cac276db8d44c22c7739f614a0fbc (diff) | |
| download | rust-2872b7e0a48e6ae41570804afa88bd12bf5f507a.tar.gz rust-2872b7e0a48e6ae41570804afa88bd12bf5f507a.zip | |
Allow `let () = ..` as type inference for `let_unit_value`
| -rw-r--r-- | clippy_lints/src/unit_types/let_unit_value.rs | 19 | ||||
| -rw-r--r-- | tests/ui/let_unit.fixed | 2 | ||||
| -rw-r--r-- | 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() { |
