Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 71ba2cf

Browse files
committed
Extract leaks_droppable_temporary_with_limited_lifetime()
1 parent 8f1b4bb commit 71ba2cf

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

clippy_lints/src/returns.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
4-
use clippy_utils::visitors::{Descend, for_each_expr, for_each_unconsumed_temporary};
4+
use clippy_utils::visitors::{Descend, for_each_expr};
55
use clippy_utils::{
6-
binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res,
7-
path_to_local_id, span_contains_cfg, span_find_starting_semi,
6+
binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor,
7+
leaks_droppable_temporary_with_limited_lifetime, path_res, path_to_local_id, span_contains_cfg,
8+
span_find_starting_semi,
89
};
910
use core::ops::ControlFlow;
1011
use rustc_ast::MetaItemInner;
@@ -389,22 +390,8 @@ fn check_final_expr<'tcx>(
389390
}
390391
};
391392

392-
if let Some(inner) = inner {
393-
if for_each_unconsumed_temporary(cx, inner, |temporary_ty| {
394-
if temporary_ty.has_significant_drop(cx.tcx, cx.typing_env())
395-
&& temporary_ty
396-
.walk()
397-
.any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(re) if !re.is_static()))
398-
{
399-
ControlFlow::Break(())
400-
} else {
401-
ControlFlow::Continue(())
402-
}
403-
})
404-
.is_break()
405-
{
406-
return;
407-
}
393+
if inner.is_some_and(|inner| leaks_droppable_temporary_with_limited_lifetime(cx, inner)) {
394+
return;
408395
}
409396

410397
if ret_span.from_expansion() || is_from_proc_macro(cx, expr) {

clippy_utils/src/lib.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
117117
use rustc_middle::ty::fast_reject::SimplifiedType;
118118
use rustc_middle::ty::layout::IntegerExt;
119119
use rustc_middle::ty::{
120-
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, FloatTy, GenericArgsRef, IntTy, Ty, TyCtxt,
121-
TypeVisitableExt, UintTy, UpvarCapture,
120+
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, FloatTy, GenericArgKind, GenericArgsRef, IntTy, Ty,
121+
TyCtxt, TypeVisitableExt, UintTy, UpvarCapture,
122122
};
123123
use rustc_span::hygiene::{ExpnKind, MacroKind};
124124
use rustc_span::source_map::SourceMap;
125125
use rustc_span::symbol::{Ident, Symbol, kw};
126126
use rustc_span::{InnerSpan, Span, sym};
127127
use rustc_target::abi::Integer;
128-
use visitors::Visitable;
128+
use visitors::{Visitable, for_each_unconsumed_temporary};
129129

130130
use crate::consts::{ConstEvalCtxt, Constant, mir_to_const};
131131
use crate::higher::Range;
@@ -3465,3 +3465,20 @@ pub fn is_receiver_of_method_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool
34653465
}
34663466
false
34673467
}
3468+
3469+
/// Returns true if `expr` creates any temporary whose type references a non-static lifetime and has
3470+
/// a significant drop and does not consume it.
3471+
pub fn leaks_droppable_temporary_with_limited_lifetime<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
3472+
for_each_unconsumed_temporary(cx, expr, |temporary_ty| {
3473+
if temporary_ty.has_significant_drop(cx.tcx, cx.typing_env())
3474+
&& temporary_ty
3475+
.walk()
3476+
.any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(re) if !re.is_static()))
3477+
{
3478+
ControlFlow::Break(())
3479+
} else {
3480+
ControlFlow::Continue(())
3481+
}
3482+
})
3483+
.is_break()
3484+
}

0 commit comments

Comments
 (0)