Skip to content

Commit 1b6561f

Browse files
committed
Auto merge of rust-lang#12630 - mira-eanda:master, r=Manishearth
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.
2 parents 2202493 + 8ae7eae commit 1b6561f

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

clippy_lints/src/dereference.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,9 +1014,18 @@ fn report<'tcx>(
10141014
},
10151015
_ => (0, false),
10161016
};
1017+
let is_in_tuple = matches!(
1018+
get_parent_expr(cx, data.first_expr),
1019+
Some(Expr {
1020+
kind: ExprKind::Tup(..),
1021+
..
1022+
})
1023+
);
1024+
10171025
let sugg = if !snip_is_macro
10181026
&& (calls_field || expr.precedence().order() < precedence)
10191027
&& !has_enclosing_paren(&snip)
1028+
&& !is_in_tuple
10201029
{
10211030
format!("({snip})")
10221031
} else {

tests/ui/needless_borrow.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,11 @@ mod issue_10253 {
251251
(&S).f::<()>();
252252
}
253253
}
254+
255+
fn issue_12268() {
256+
let option = Some((&1,));
257+
let x = (&1,);
258+
option.unwrap_or((x.0,));
259+
//~^ ERROR: this expression creates a reference which is immediately dereferenced by the
260+
// compiler
261+
}

tests/ui/needless_borrow.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,11 @@ mod issue_10253 {
251251
(&S).f::<()>();
252252
}
253253
}
254+
255+
fn issue_12268() {
256+
let option = Some((&1,));
257+
let x = (&1,);
258+
option.unwrap_or((&x.0,));
259+
//~^ ERROR: this expression creates a reference which is immediately dereferenced by the
260+
// compiler
261+
}

tests/ui/needless_borrow.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,11 @@ error: this expression borrows a value the compiler would automatically borrow
163163
LL | let _ = &mut (&mut { x.u }).x;
164164
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`
165165

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

0 commit comments

Comments
 (0)