From e6b66784aca8564557485d902968ff7523cf30ca Mon Sep 17 00:00:00 2001 From: Aaron Kofsky Date: Sat, 4 Jun 2022 20:19:19 -0400 Subject: [PATCH] Bail out early if the type does not has a trivial Drop implementation. If the type has a trivial Drop implementation, then it is probably irrelevant that the type was dropped immediately, since nothing important happens on drop. Hence, we can bail out early instead of doing some expensive checks. --- compiler/rustc_lint/src/let_underscore.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_lint/src/let_underscore.rs b/compiler/rustc_lint/src/let_underscore.rs index d33ea450b3e..1e4565a226c 100644 --- a/compiler/rustc_lint/src/let_underscore.rs +++ b/compiler/rustc_lint/src/let_underscore.rs @@ -129,7 +129,11 @@ fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) { } if let Some(init) = local.init { let init_ty = cx.typeck_results().expr_ty(init); - let needs_drop = init_ty.needs_drop(cx.tcx, cx.param_env); + // If the type has a trivial Drop implementation, then it doesn't + // matter that we drop the value immediately. + if !init_ty.needs_drop(cx.tcx, cx.param_env) { + return; + } let is_sync_lock = init_ty.walk().any(|inner| match inner.unpack() { GenericArgKind::Type(inner_ty) => { SYNC_GUARD_PATHS.iter().any(|guard_path| match inner_ty.kind() { @@ -164,7 +168,7 @@ fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) { "non-binding let on a expression marked `must_use`", ); }) - } else if needs_drop { + } else { cx.struct_span_lint(LET_UNDERSCORE_DROP, local.span, |lint| { build_and_emit_lint( lint,