Skip to content

Commit e6b6678

Browse files
committed
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.
1 parent 30e8adb commit e6b6678

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

compiler/rustc_lint/src/let_underscore.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
129129
}
130130
if let Some(init) = local.init {
131131
let init_ty = cx.typeck_results().expr_ty(init);
132-
let needs_drop = init_ty.needs_drop(cx.tcx, cx.param_env);
132+
// If the type has a trivial Drop implementation, then it doesn't
133+
// matter that we drop the value immediately.
134+
if !init_ty.needs_drop(cx.tcx, cx.param_env) {
135+
return;
136+
}
133137
let is_sync_lock = init_ty.walk().any(|inner| match inner.unpack() {
134138
GenericArgKind::Type(inner_ty) => {
135139
SYNC_GUARD_PATHS.iter().any(|guard_path| match inner_ty.kind() {
@@ -164,7 +168,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
164168
"non-binding let on a expression marked `must_use`",
165169
);
166170
})
167-
} else if needs_drop {
171+
} else {
168172
cx.struct_span_lint(LET_UNDERSCORE_DROP, local.span, |lint| {
169173
build_and_emit_lint(
170174
lint,

0 commit comments

Comments
 (0)