From 8ae7eaefdc27b224c074649aea13955c21593777 Mon Sep 17 00:00:00 2001 From: Mariana Miranda Date: Mon, 8 Apr 2024 23:35:19 +0100 Subject: [PATCH] fix: Refactor dereference code and fix test --- clippy_lints/src/dereference.rs | 11 +++++++---- tests/ui/needless_borrow.fixed | 3 ++- tests/ui/needless_borrow.rs | 3 ++- tests/ui/needless_borrow.stderr | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index f63413bd575ab..3fd4d6383b112 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -1014,10 +1014,13 @@ fn report<'tcx>( }, _ => (0, false), }; - let is_in_tuple = match cx.tcx.parent_hir_node(data.first_expr.hir_id) { - Node::Expr(e) => matches!(e.kind, ExprKind::Tup(_)), - _ => false, - }; + let is_in_tuple = matches!( + get_parent_expr(cx, data.first_expr), + Some(Expr { + kind: ExprKind::Tup(..), + .. + }) + ); let sugg = if !snip_is_macro && (calls_field || expr.precedence().order() < precedence) diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed index adc92be0e1ab3..5121077b4cabb 100644 --- a/tests/ui/needless_borrow.fixed +++ b/tests/ui/needless_borrow.fixed @@ -255,6 +255,7 @@ mod issue_10253 { fn issue_12268() { let option = Some((&1,)); let x = (&1,); - // Lint here. option.unwrap_or((x.0,)); + //~^ ERROR: this expression creates a reference which is immediately dereferenced by the + // compiler } diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs index b1f51a2997874..e3a5cb280bada 100644 --- a/tests/ui/needless_borrow.rs +++ b/tests/ui/needless_borrow.rs @@ -255,6 +255,7 @@ mod issue_10253 { fn issue_12268() { let option = Some((&1,)); let x = (&1,); - // Lint here. option.unwrap_or((&x.0,)); + //~^ ERROR: this expression creates a reference which is immediately dereferenced by the + // compiler } diff --git a/tests/ui/needless_borrow.stderr b/tests/ui/needless_borrow.stderr index 56fcf1268a17a..4b2b17e7e570a 100644 --- a/tests/ui/needless_borrow.stderr +++ b/tests/ui/needless_borrow.stderr @@ -164,7 +164,7 @@ LL | let _ = &mut (&mut { x.u }).x; | ^^^^^^^^^^^^^^ help: change this to: `{ x.u }` error: this expression creates a reference which is immediately dereferenced by the compiler - --> tests/ui/needless_borrow.rs:259:23 + --> tests/ui/needless_borrow.rs:258:23 | LL | option.unwrap_or((&x.0,)); | ^^^^ help: change this to: `x.0`