Skip to content

Commit d496644

Browse files
committed
fix a bug while we're at it
1 parent 2cde339 commit d496644

File tree

4 files changed

+96
-33
lines changed

4 files changed

+96
-33
lines changed

clippy_lints/src/double_parens.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,25 @@ declare_lint_pass!(DoubleParens => [DOUBLE_PARENS]);
4242

4343
impl EarlyLintPass for DoubleParens {
4444
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
45-
if expr.span.from_expansion() {
46-
return;
47-
}
48-
4945
match &expr.kind {
5046
// ((..))
5147
// ^^^^^^ expr
5248
// ^^^^ inner
5349
ExprKind::Paren(inner) if matches!(inner.kind, ExprKind::Paren(_) | ExprKind::Tup(_)) => {
5450
// suggest removing the outer parens
55-
let mut applicability = Applicability::MachineApplicable;
56-
let sugg = snippet_with_applicability(cx.sess(), inner.span, "_", &mut applicability);
57-
span_lint_and_sugg(
58-
cx,
59-
DOUBLE_PARENS,
60-
expr.span,
61-
"unnecessary parentheses",
62-
"remove them",
63-
sugg.to_string(),
64-
applicability,
65-
);
51+
if expr.span.eq_ctxt(inner.span) {
52+
let mut applicability = Applicability::MachineApplicable;
53+
let sugg = snippet_with_applicability(cx.sess(), inner.span, "_", &mut applicability);
54+
span_lint_and_sugg(
55+
cx,
56+
DOUBLE_PARENS,
57+
expr.span,
58+
"unnecessary parentheses",
59+
"remove them",
60+
sugg.to_string(),
61+
applicability,
62+
);
63+
}
6664
return;
6765
},
6866

@@ -75,17 +73,19 @@ impl EarlyLintPass for DoubleParens {
7573
&& let ExprKind::Paren(inner) = &arg.kind =>
7674
{
7775
// suggest removing the inner parens
78-
let mut applicability = Applicability::MachineApplicable;
79-
let sugg = snippet_with_applicability(cx.sess(), inner.span, "_", &mut applicability);
80-
span_lint_and_sugg(
81-
cx,
82-
DOUBLE_PARENS,
83-
arg.span,
84-
"unnecessary parentheses",
85-
"remove them",
86-
sugg.to_string(),
87-
applicability,
88-
);
76+
if expr.span.eq_ctxt(arg.span) && arg.span.eq_ctxt(inner.span) {
77+
let mut applicability = Applicability::MachineApplicable;
78+
let sugg = snippet_with_applicability(cx.sess(), inner.span, "_", &mut applicability);
79+
span_lint_and_sugg(
80+
cx,
81+
DOUBLE_PARENS,
82+
arg.span,
83+
"unnecessary parentheses",
84+
"remove them",
85+
sugg.to_string(),
86+
applicability,
87+
);
88+
}
8989
},
9090
_ => {},
9191
};

tests/ui/double_parens.fixed

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
fn dummy_fn<T>(_: T) {}
77

8+
#[derive(Clone, Copy)]
89
struct DummyStruct;
910

1011
impl DummyStruct {
@@ -60,4 +61,29 @@ fn inside_macro() {
6061
//~^ double_parens
6162
}
6263

64+
fn issue9000(x: DummyStruct) {
65+
macro_rules! foo {
66+
() => {(100)}
67+
}
68+
// don't lint: the inner paren comes from the macro expansion
69+
(foo!());
70+
dummy_fn(foo!());
71+
x.dummy_method(foo!());
72+
73+
macro_rules! baz {
74+
($n:literal) => {($n)}
75+
}
76+
// don't lint: don't get confused by the expression inside the inner paren
77+
// having the same context as the overall expression
78+
dummy_fn(baz!(100));
79+
x.dummy_method(baz!(100));
80+
81+
// should lint: both parens are inside the macro
82+
macro_rules! bar {
83+
() => {(100)}
84+
//~^ double_parens
85+
}
86+
bar!();
87+
}
88+
6389
fn main() {}

tests/ui/double_parens.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
fn dummy_fn<T>(_: T) {}
77

8+
#[derive(Clone, Copy)]
89
struct DummyStruct;
910

1011
impl DummyStruct {
@@ -60,4 +61,29 @@ fn inside_macro() {
6061
//~^ double_parens
6162
}
6263

64+
fn issue9000(x: DummyStruct) {
65+
macro_rules! foo {
66+
() => {(100)}
67+
}
68+
// don't lint: the inner paren comes from the macro expansion
69+
(foo!());
70+
dummy_fn(foo!());
71+
x.dummy_method(foo!());
72+
73+
macro_rules! baz {
74+
($n:literal) => {($n)}
75+
}
76+
// don't lint: don't get confused by the expression inside the inner paren
77+
// having the same context as the overall expression
78+
dummy_fn(baz!(100));
79+
x.dummy_method(baz!(100));
80+
81+
// should lint: both parens are inside the macro
82+
macro_rules! bar {
83+
() => {((100))}
84+
//~^ double_parens
85+
}
86+
bar!();
87+
}
88+
6389
fn main() {}

tests/ui/double_parens.stderr

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnecessary parentheses
2-
--> tests/ui/double_parens.rs:15:5
2+
--> tests/ui/double_parens.rs:16:5
33
|
44
LL | ((0))
55
| ^^^^^ help: remove them: `(0)`
@@ -8,34 +8,45 @@ LL | ((0))
88
= help: to override `-D warnings` add `#[allow(clippy::double_parens)]`
99

1010
error: unnecessary parentheses
11-
--> tests/ui/double_parens.rs:20:14
11+
--> tests/ui/double_parens.rs:21:14
1212
|
1313
LL | dummy_fn((0));
1414
| ^^^ help: remove them: `0`
1515

1616
error: unnecessary parentheses
17-
--> tests/ui/double_parens.rs:25:20
17+
--> tests/ui/double_parens.rs:26:20
1818
|
1919
LL | x.dummy_method((0));
2020
| ^^^ help: remove them: `0`
2121

2222
error: unnecessary parentheses
23-
--> tests/ui/double_parens.rs:30:5
23+
--> tests/ui/double_parens.rs:31:5
2424
|
2525
LL | ((1, 2))
2626
| ^^^^^^^^ help: remove them: `(1, 2)`
2727

2828
error: unnecessary parentheses
29-
--> tests/ui/double_parens.rs:36:5
29+
--> tests/ui/double_parens.rs:37:5
3030
|
3131
LL | (())
3232
| ^^^^ help: remove them: `()`
3333

3434
error: unnecessary parentheses
35-
--> tests/ui/double_parens.rs:59:16
35+
--> tests/ui/double_parens.rs:60:16
3636
|
3737
LL | assert_eq!(((1, 2)), (1, 2), "Error");
3838
| ^^^^^^^^ help: remove them: `(1, 2)`
3939

40-
error: aborting due to 6 previous errors
40+
error: unnecessary parentheses
41+
--> tests/ui/double_parens.rs:83:16
42+
|
43+
LL | () => {((100))}
44+
| ^^^^^^^ help: remove them: `(100)`
45+
...
46+
LL | bar!();
47+
| ------ in this macro invocation
48+
|
49+
= note: this error originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)
50+
51+
error: aborting due to 7 previous errors
4152

0 commit comments

Comments
 (0)