Skip to content

Commit

Permalink
fix: Refactor dereference code and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
mira-eanda committed Apr 8, 2024
1 parent 38b8056 commit 8ae7eae
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
11 changes: 7 additions & 4 deletions clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/needless_borrow.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion tests/ui/needless_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/ui/needless_borrow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 8ae7eae

Please sign in to comment.