Skip to content

Commit 4f64560

Browse files
Rollup merge of #165948 - hardikxk:lint/unused-parens, r=folkertdev
lint: Avoid unused_parens warning for ref patterns binding an identifier For reference patterns like `&(v @ S{a, b})` the compiler emits the unused parens warning. Without the parenthesis the pattern becomes `&v @ S{a, b}` which makes the expression look a little confusing and it seems like `v` is being binded by reference. Add an explicit pattern matcher to catch all such patterns that would involve an identifier binding with any pattern and avoid emitting the `unused_parens` diagnostic for this. closes #165636
2 parents e069349 + 4ec3020 commit 4f64560

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

‎compiler/rustc_lint/src/unused.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,15 @@ impl EarlyLintPass for UnusedParens {
943943
// FIXME(pin_ergonomics): Remove this gate once pinned patterns are stable.
944944
|| cx.builder.features().pin_ergonomics() =>
945945
{
946+
// Avoid linting on patterns like `&(x @ _)`, as without parens `&x @ _`
947+
// it creates confusion and seems like `x` is binding by reference, #165636.
948+
if let Paren(ref pat) = p.kind
949+
&& *pinned == Pinnedness::Not
950+
&& let Ident(_, _, Some(..)) = pat.kind
951+
{
952+
return;
953+
}
954+
// Check other cases.
946955
self.check_unused_parens_pat(
947956
cx,
948957
p,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-rustfix
2+
//@ check-pass
3+
#![allow(dead_code)]
4+
#![warn(unused_parens)]
5+
6+
pub struct S {}
7+
pub struct TS ();
8+
9+
fn main() {
10+
let _x @ S{}; //~ WARN: unnecessary parentheses around pattern
11+
12+
let &_y: &i32; //~ WARN: unnecessary parentheses around pattern
13+
14+
let &(_x @ S{});
15+
16+
let &(_x @ ()): &();
17+
18+
let &(_x @ TS());
19+
20+
let &(_x @ _nums): &[i32; 0];
21+
22+
let &(_x @ _y): &i32;
23+
24+
let &(mut _x @ S{});
25+
26+
let &(ref _x @ S{});
27+
28+
let &(ref mut _x @ S{});
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-rustfix
2+
//@ check-pass
3+
#![allow(dead_code)]
4+
#![warn(unused_parens)]
5+
6+
pub struct S {}
7+
pub struct TS ();
8+
9+
fn main() {
10+
let (_x @ S{}); //~ WARN: unnecessary parentheses around pattern
11+
12+
let &(_y): &i32; //~ WARN: unnecessary parentheses around pattern
13+
14+
let &(_x @ S{});
15+
16+
let &(_x @ ()): &();
17+
18+
let &(_x @ TS());
19+
20+
let &(_x @ _nums): &[i32; 0];
21+
22+
let &(_x @ _y): &i32;
23+
24+
let &(mut _x @ S{});
25+
26+
let &(ref _x @ S{});
27+
28+
let &(ref mut _x @ S{});
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
warning: unnecessary parentheses around pattern
2+
--> $DIR/unused_parens_reference_binding.rs:10:9
3+
|
4+
LL | let (_x @ S{});
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused_parens_reference_binding.rs:4:9
9+
|
10+
LL | #![warn(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - let (_x @ S{});
15+
LL + let _x @ S{};
16+
|
17+
18+
warning: unnecessary parentheses around pattern
19+
--> $DIR/unused_parens_reference_binding.rs:12:10
20+
|
21+
LL | let &(_y): &i32;
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - let &(_y): &i32;
27+
LL + let &_y: &i32;
28+
|
29+
30+
warning: 2 warnings emitted
31+

0 commit comments

Comments
 (0)