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

Commit 38b8056

Browse files
committed
Fix: rust-lang#12268: Correct parentheses for needless_borrow suggestion
Clippy no longer adds unnecessary parentheses in suggestion when the expression is a part of a tuple.
1 parent e80ca2f commit 38b8056

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

clippy_lints/src/dereference.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,9 +1014,15 @@ 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+
};
1021+
10171022
let sugg = if !snip_is_macro
10181023
&& (calls_field || expr.precedence().order() < precedence)
10191024
&& !has_enclosing_paren(&snip)
1025+
&& !is_in_tuple
10201026
{
10211027
format!("({snip})")
10221028
} else {

tests/ui/needless_borrow.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,10 @@ mod issue_10253 {
251251
(&S).f::<()>();
252252
}
253253
}
254+
255+
fn issue_12268() {
256+
let option = Some((&1,));
257+
let x = (&1,);
258+
// Lint here.
259+
option.unwrap_or((x.0,));
260+
}

tests/ui/needless_borrow.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,10 @@ mod issue_10253 {
251251
(&S).f::<()>();
252252
}
253253
}
254+
255+
fn issue_12268() {
256+
let option = Some((&1,));
257+
let x = (&1,);
258+
// Lint here.
259+
option.unwrap_or((&x.0,));
260+
}

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:259: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)