Skip to content

Commit

Permalink
Allow let () = .. as type inference for let_unit_value
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Jun 27, 2022
1 parent 51e6175 commit d773ce0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
19 changes: 11 additions & 8 deletions clippy_lints/src/unit_types/let_unit_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
&& !in_external_macro(cx.sess(), stmt.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,
stmt.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
);
},
);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/let_unit.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,6 @@ fn _returns_generic() {
let _: () = opt;
}
}

let () = f();
}
2 changes: 2 additions & 0 deletions tests/ui/let_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,6 @@ fn _returns_generic() {
let _: () = opt;
}
}

let () = f();
}

0 comments on commit d773ce0

Please sign in to comment.