diff options
| author | Mariana Miranda <mariana.almeida.miranda@tecnico.ulisboa.pt> | 2024-04-04 10:24:25 +0100 |
|---|---|---|
| committer | Mariana Miranda <mariana.almeida.miranda@tecnico.ulisboa.pt> | 2024-04-04 10:24:25 +0100 |
| commit | 38b8056fc652ed822c959d45486683147e24f103 (patch) | |
| tree | d8cdd7d631f816a3ff4d3c44fbc1f1ce796997a1 | |
| parent | e80ca2f3816ed6f4584480d40b8671a15b2225fc (diff) | |
| download | rust-38b8056fc652ed822c959d45486683147e24f103.tar.gz rust-38b8056fc652ed822c959d45486683147e24f103.zip | |
Fix: #12268: Correct parentheses for needless_borrow suggestion
Clippy no longer adds unnecessary parentheses in suggestion when the expression is a part of a tuple.
| -rw-r--r-- | clippy_lints/src/dereference.rs | 6 | ||||
| -rw-r--r-- | tests/ui/needless_borrow.fixed | 7 | ||||
| -rw-r--r-- | tests/ui/needless_borrow.rs | 7 | ||||
| -rw-r--r-- | tests/ui/needless_borrow.stderr | 8 |
4 files changed, 27 insertions, 1 deletions
diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 560b2acc1c7..f63413bd575 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -1014,9 +1014,15 @@ 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 sugg = if !snip_is_macro && (calls_field || expr.precedence().order() < precedence) && !has_enclosing_paren(&snip) + && !is_in_tuple { format!("({snip})") } else { diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed index 998f5430fdf..adc92be0e1a 100644 --- a/tests/ui/needless_borrow.fixed +++ b/tests/ui/needless_borrow.fixed @@ -251,3 +251,10 @@ mod issue_10253 { (&S).f::<()>(); } } + +fn issue_12268() { + let option = Some((&1,)); + let x = (&1,); + // Lint here. + option.unwrap_or((x.0,)); +} diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs index acb2c74d849..b1f51a29978 100644 --- a/tests/ui/needless_borrow.rs +++ b/tests/ui/needless_borrow.rs @@ -251,3 +251,10 @@ mod issue_10253 { (&S).f::<()>(); } } + +fn issue_12268() { + let option = Some((&1,)); + let x = (&1,); + // Lint here. + option.unwrap_or((&x.0,)); +} diff --git a/tests/ui/needless_borrow.stderr b/tests/ui/needless_borrow.stderr index 5f028338764..56fcf1268a1 100644 --- a/tests/ui/needless_borrow.stderr +++ b/tests/ui/needless_borrow.stderr @@ -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:259:23 + | +LL | option.unwrap_or((&x.0,)); + | ^^^^ help: change this to: `x.0` + +error: aborting due to 28 previous errors |
