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

Commit 2fda4f6

Browse files
committed
add is_expr_temporary_value helper function
1 parent d92da0f commit 2fda4f6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

clippy_utils/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,6 +2331,18 @@ pub fn is_expr_final_block_expr(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
23312331
matches!(tcx.parent_hir_node(expr.hir_id), Node::Block(..))
23322332
}
23332333

2334+
/// Checks if the expression is a temporary value.
2335+
// This logic is the same as the one used in rustc's `check_named_place_expr function`.
2336+
// https://github.com/rust-lang/rust/blob/3ed2a10d173d6c2e0232776af338ca7d080b1cd4/compiler/rustc_hir_typeck/src/expr.rs#L482-L499
2337+
pub fn is_expr_temporary_value(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
2338+
!expr.is_place_expr(|base| {
2339+
cx.typeck_results()
2340+
.adjustments()
2341+
.get(base.hir_id)
2342+
.is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
2343+
})
2344+
}
2345+
23342346
pub fn std_or_core(cx: &LateContext<'_>) -> Option<&'static str> {
23352347
if !is_no_std_crate(cx) {
23362348
Some("std")

0 commit comments

Comments
 (0)