Skip to content

Commit

Permalink
Auto merge of rust-lang#12630 - mira-eanda:master, r=Manishearth
Browse files Browse the repository at this point in the history
Correct parentheses for [`needless_borrow`] suggestion

This fixes rust-lang#12268

Clippy no longer adds unnecessary parentheses in suggestions when the expression is part of a tuple.

---

changelog: Fix [`needless_borrow`] unnecessary parentheses in suggestion.
  • Loading branch information
bors committed Apr 8, 2024
2 parents 2202493 + 8ae7eae commit 1b6561f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
9 changes: 9 additions & 0 deletions clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,9 +1014,18 @@ fn report<'tcx>(
},
_ => (0, 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)
&& !has_enclosing_paren(&snip)
&& !is_in_tuple
{
format!("({snip})")
} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/needless_borrow.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,11 @@ mod issue_10253 {
(&S).f::<()>();
}
}

fn issue_12268() {
let option = Some((&1,));
let x = (&1,);
option.unwrap_or((x.0,));
//~^ ERROR: this expression creates a reference which is immediately dereferenced by the
// compiler
}
8 changes: 8 additions & 0 deletions tests/ui/needless_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,11 @@ mod issue_10253 {
(&S).f::<()>();
}
}

fn issue_12268() {
let option = Some((&1,));
let x = (&1,);
option.unwrap_or((&x.0,));
//~^ ERROR: this expression creates a reference which is immediately dereferenced by the
// compiler
}
8 changes: 7 additions & 1 deletion tests/ui/needless_borrow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,11 @@ error: this expression borrows a value the compiler would automatically borrow
LL | let _ = &mut (&mut { x.u }).x;
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`

error: aborting due to 27 previous errors
error: this expression creates a reference which is immediately dereferenced by the compiler
--> tests/ui/needless_borrow.rs:258:23
|
LL | option.unwrap_or((&x.0,));
| ^^^^ help: change this to: `x.0`

error: aborting due to 28 previous errors

0 comments on commit 1b6561f

Please sign in to comment.