Skip to content

Commit c8490a0

Browse files
committed
skip unused_parens's suggestion for Paren in macro.
fixes #120642
1 parent b14d8b2 commit c8490a0

File tree

3 files changed

+94
-9
lines changed

3 files changed

+94
-9
lines changed

compiler/rustc_lint/src/unused.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,14 @@ impl UnusedParens {
10741074
// Otherwise proceed with linting.
10751075
_ => {}
10761076
}
1077-
let spans = inner
1078-
.span
1079-
.find_ancestor_inside(value.span)
1080-
.map(|inner| (value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi())));
1077+
let spans = if !value.span.from_expansion() {
1078+
inner
1079+
.span
1080+
.find_ancestor_inside(value.span)
1081+
.map(|inner| (value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi())))
1082+
} else {
1083+
None
1084+
};
10811085
self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false);
10821086
}
10831087
}
@@ -1233,11 +1237,13 @@ impl EarlyLintPass for UnusedParens {
12331237
if self.with_self_ty_parens && b.generic_params.len() > 0 => {}
12341238
ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
12351239
_ => {
1236-
let spans = r
1237-
.span
1238-
.find_ancestor_inside(ty.span)
1239-
.map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())));
1240-
1240+
let spans = if !ty.span.from_expansion() {
1241+
r.span
1242+
.find_ancestor_inside(ty.span)
1243+
.map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
1244+
} else {
1245+
None
1246+
};
12411247
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false), false);
12421248
}
12431249
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ run-pass
2+
3+
#![warn(unused_parens)]
4+
#![allow(dead_code)]
5+
6+
trait Foo {
7+
fn bar();
8+
fn tar();
9+
}
10+
11+
macro_rules! unused_parens {
12+
($ty:ident) => {
13+
impl<$ty: Foo> Foo for ($ty,) {
14+
fn bar() { <$ty>::bar() }
15+
fn tar() {}
16+
}
17+
};
18+
19+
($ty:ident $(, $rest:ident)*) => {
20+
impl<$ty: Foo, $($rest: Foo),*> Foo for ($ty, $($rest),*) {
21+
fn bar() {
22+
<$ty>::bar();
23+
<($($rest),*)>::bar() //~WARN unnecessary parentheses around type
24+
}
25+
fn tar() {
26+
let (_t) = 1; //~WARN unnecessary parentheses around pattern
27+
//~| WARN unnecessary parentheses around pattern
28+
let (_t1,) = (1,);
29+
let (_t2, _t3) = (1, 2);
30+
}
31+
}
32+
33+
unused_parens!($($rest),*);
34+
}
35+
}
36+
37+
unused_parens!(T1, T2, T3);
38+
39+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
warning: unnecessary parentheses around pattern
2+
--> $DIR/unused-parens-in-macro-issue-120642.rs:26:19
3+
|
4+
LL | let (_t) = 1;
5+
| ^^^^
6+
...
7+
LL | unused_parens!(T1, T2, T3);
8+
| -------------------------- in this macro invocation
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/unused-parens-in-macro-issue-120642.rs:3:9
12+
|
13+
LL | #![warn(unused_parens)]
14+
| ^^^^^^^^^^^^^
15+
= note: this warning originates in the macro `unused_parens` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
warning: unnecessary parentheses around type
18+
--> $DIR/unused-parens-in-macro-issue-120642.rs:23:18
19+
|
20+
LL | <($($rest),*)>::bar()
21+
| ^^^^^^^^^^^^
22+
...
23+
LL | unused_parens!(T1, T2, T3);
24+
| -------------------------- in this macro invocation
25+
|
26+
= note: this warning originates in the macro `unused_parens` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
28+
warning: unnecessary parentheses around pattern
29+
--> $DIR/unused-parens-in-macro-issue-120642.rs:26:19
30+
|
31+
LL | let (_t) = 1;
32+
| ^^^^
33+
...
34+
LL | unused_parens!(T1, T2, T3);
35+
| -------------------------- in this macro invocation
36+
|
37+
= note: this warning originates in the macro `unused_parens` (in Nightly builds, run with -Z macro-backtrace for more info)
38+
39+
warning: 3 warnings emitted
40+

0 commit comments

Comments
 (0)