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

Commit 8ae7eae

Browse files
committed
fix: Refactor dereference code and fix test
1 parent 38b8056 commit 8ae7eae

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

clippy_lints/src/dereference.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,10 +1014,13 @@ fn report<'tcx>(
10141014
},
10151015
_ => (0, false),
10161016
};
1017-
let is_in_tuple = match cx.tcx.parent_hir_node(data.first_expr.hir_id) {
1018-
Node::Expr(e) => matches!(e.kind, ExprKind::Tup(_)),
1019-
_ => false,
1020-
};
1017+
let is_in_tuple = matches!(
1018+
get_parent_expr(cx, data.first_expr),
1019+
Some(Expr {
1020+
kind: ExprKind::Tup(..),
1021+
..
1022+
})
1023+
);
10211024

10221025
let sugg = if !snip_is_macro
10231026
&& (calls_field || expr.precedence().order() < precedence)

tests/ui/needless_borrow.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ mod issue_10253 {
255255
fn issue_12268() {
256256
let option = Some((&1,));
257257
let x = (&1,);
258-
// Lint here.
259258
option.unwrap_or((x.0,));
259+
//~^ ERROR: this expression creates a reference which is immediately dereferenced by the
260+
// compiler
260261
}

tests/ui/needless_borrow.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ mod issue_10253 {
255255
fn issue_12268() {
256256
let option = Some((&1,));
257257
let x = (&1,);
258-
// Lint here.
259258
option.unwrap_or((&x.0,));
259+
//~^ ERROR: this expression creates a reference which is immediately dereferenced by the
260+
// compiler
260261
}

tests/ui/needless_borrow.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ LL | let _ = &mut (&mut { x.u }).x;
164164
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`
165165

166166
error: this expression creates a reference which is immediately dereferenced by the compiler
167-
--> tests/ui/needless_borrow.rs:259:23
167+
--> tests/ui/needless_borrow.rs:258:23
168168
|
169169
LL | option.unwrap_or((&x.0,));
170170
| ^^^^ help: change this to: `x.0`

0 commit comments

Comments
 (0)