about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlapla-cogito <me@lapla.dev>2025-02-25 08:32:37 +0900
committerlapla-cogito <me@lapla.dev>2025-03-06 07:54:48 +0900
commit2fda4f6bb85ae334ae36cc4b3d88a609b3132bd0 (patch)
tree8e82ea75cb9ff23be8a60aa323a7592754f6bf27
parentd92da0fb322e833e481a4c50f0f73b7b27829546 (diff)
downloadrust-2fda4f6bb85ae334ae36cc4b3d88a609b3132bd0.tar.gz
rust-2fda4f6bb85ae334ae36cc4b3d88a609b3132bd0.zip
add `is_expr_temporary_value` helper function
-rw-r--r--clippy_utils/src/lib.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs
index 62f70a5d957..82433405cde 100644
--- a/clippy_utils/src/lib.rs
+++ b/clippy_utils/src/lib.rs
@@ -2331,6 +2331,18 @@ pub fn is_expr_final_block_expr(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
     matches!(tcx.parent_hir_node(expr.hir_id), Node::Block(..))
 }
 
+/// Checks if the expression is a temporary value.
+// This logic is the same as the one used in rustc's `check_named_place_expr function`.
+// https://github.com/rust-lang/rust/blob/3ed2a10d173d6c2e0232776af338ca7d080b1cd4/compiler/rustc_hir_typeck/src/expr.rs#L482-L499
+pub fn is_expr_temporary_value(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
+    !expr.is_place_expr(|base| {
+        cx.typeck_results()
+            .adjustments()
+            .get(base.hir_id)
+            .is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
+    })
+}
+
 pub fn std_or_core(cx: &LateContext<'_>) -> Option<&'static str> {
     if !is_no_std_crate(cx) {
         Some("std")