Skip to content

Commit b4a3770

Browse files
committed
fix(double_parens): don't lint in proc-macros
1 parent 1f09b33 commit b4a3770

File tree

6 files changed

+141
-15
lines changed

6 files changed

+141
-15
lines changed

clippy_lints/src/double_parens.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::{HasSession, snippet_with_applicability, snippet_with_context};
2+
use clippy_utils::source::{HasSession, SpanRangeExt, snippet_with_applicability, snippet_with_context};
33
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
44
use rustc_errors::Applicability;
55
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -47,8 +47,9 @@ impl EarlyLintPass for DoubleParens {
4747
// ^^^^^^ expr
4848
// ^^^^ inner
4949
ExprKind::Paren(inner) if matches!(inner.kind, ExprKind::Paren(_) | ExprKind::Tup(_)) => {
50-
// suggest removing the outer parens
51-
if expr.span.eq_ctxt(inner.span) {
50+
if expr.span.eq_ctxt(inner.span) && check_source(cx, inner) {
51+
// suggest removing the outer parens
52+
5253
let mut applicability = Applicability::MachineApplicable;
5354
// We don't need to use `snippet_with_context` here, because:
5455
// - if `inner`'s `ctxt` is from macro, we don't lint in the first place (see the check above)
@@ -74,8 +75,9 @@ impl EarlyLintPass for DoubleParens {
7475
if let [arg] = &**args
7576
&& let ExprKind::Paren(inner) = &arg.kind =>
7677
{
77-
// suggest removing the inner parens
78-
if expr.span.eq_ctxt(arg.span) {
78+
if expr.span.eq_ctxt(arg.span) && check_source(cx, arg) {
79+
// suggest removing the inner parens
80+
7981
let mut applicability = Applicability::MachineApplicable;
8082
let sugg = snippet_with_context(cx.sess(), inner.span, arg.span.ctxt(), "_", &mut applicability).0;
8183
span_lint_and_sugg(
@@ -93,3 +95,23 @@ impl EarlyLintPass for DoubleParens {
9395
}
9496
}
9597
}
98+
99+
/// Check that the span does indeed look like `( (..) )`
100+
fn check_source(cx: &EarlyContext<'_>, inner: &Expr) -> bool {
101+
if let Some(sfr) = inner.span.get_source_range(cx)
102+
// this line is copied from `SourceFileRange::as_str`, but
103+
// -- doesn't load `external_src`, to avoid linting in external macros (?)
104+
// -- doesn't apply the range right away, because we're interested in the source code outside it
105+
&& let Some(src) = sfr.sf.src.as_ref().map(|src| src.as_str())
106+
&& let Some((start, outer_after_inner)) = src.split_at_checked(sfr.range.end)
107+
&& let Some((outer_before_inner, inner)) = start.split_at_checked(sfr.range.start)
108+
&& outer_before_inner.trim_end().ends_with('(')
109+
&& inner.starts_with('(')
110+
&& inner.ends_with(')')
111+
&& outer_after_inner.trim_start().starts_with(')')
112+
{
113+
true
114+
} else {
115+
false
116+
}
117+
}

tests/ui/auxiliary/macro_rules.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ macro_rules! bad_transmute {
5757
std::mem::transmute($e)
5858
};
5959
}
60+
61+
#[macro_export]
62+
macro_rules! double_parens {
63+
($a:expr, $b:expr, $c:expr, $d:expr) => {
64+
InterruptMask((($a.union($b).union($c).union($d)).into_bits()) as u32)
65+
};
66+
}

tests/ui/auxiliary/proc_macro_derive.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,14 @@ pub fn allow_lint_same_span_derive(input: TokenStream) -> TokenStream {
230230
span_help(Group::new(Delimiter::Brace, TokenStream::new()).into()),
231231
])
232232
}
233+
234+
#[proc_macro_derive(DoubleParens)]
235+
pub fn derive_double_parens(_: TokenStream) -> TokenStream {
236+
quote! {
237+
fn foo() {
238+
let a = (());
239+
let b = ((5));
240+
let c = std::convert::identity((5));
241+
}
242+
}
243+
}

tests/ui/double_parens.fixed

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
//@aux-build:proc_macros.rs
2+
//@aux-build:proc_macro_derive.rs
3+
//@aux-build:macro_rules.rs
14
#![warn(clippy::double_parens)]
25
#![expect(clippy::eq_op, clippy::no_effect)]
36
#![feature(custom_inner_attributes)]
47
#![rustfmt::skip]
58

9+
use proc_macros::{external, with_span};
10+
611
fn dummy_fn<T>(_: T) {}
712

813
struct DummyStruct;
@@ -96,4 +101,42 @@ fn issue9000(x: DummyStruct) {
96101
//~^ double_parens
97102
}
98103

104+
fn issue15892() {
105+
use macro_rules::double_parens;
106+
107+
#[repr(transparent)]
108+
#[derive(Clone, Copy, PartialEq, Eq)]
109+
pub struct InterruptMask(u32);
110+
111+
impl InterruptMask {
112+
pub const OE: InterruptMask = InterruptMask(1 << 10);
113+
pub const BE: InterruptMask = InterruptMask(1 << 9);
114+
pub const PE: InterruptMask = InterruptMask(1 << 8);
115+
pub const FE: InterruptMask = InterruptMask(1 << 7);
116+
pub const E: InterruptMask = double_parens!(Self::OE, Self::BE, Self::PE, Self::FE);
117+
#[allow(clippy::unnecessary_cast)]
118+
pub const F: InterruptMask = external!(
119+
InterruptMask((((Self::OE.union(Self::BE).union(Self::PE).union(Self::FE))).into_bits()) as u32)
120+
);
121+
#[allow(clippy::unnecessary_cast)]
122+
pub const G: InterruptMask = with_span!(span
123+
InterruptMask((((Self::OE.union(Self::BE).union(Self::PE).union(Self::FE))).into_bits()) as u32)
124+
);
125+
pub const fn into_bits(self) -> u32 {
126+
self.0
127+
}
128+
#[must_use]
129+
pub const fn union(self, rhs: Self) -> Self {
130+
InterruptMask(self.0 | rhs.0)
131+
}
132+
}
133+
}
134+
135+
fn issue15940() {
136+
use proc_macro_derive::DoubleParens;
137+
138+
#[derive(DoubleParens)]
139+
pub struct Person;
140+
}
141+
99142
fn main() {}

tests/ui/double_parens.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
//@aux-build:proc_macros.rs
2+
//@aux-build:proc_macro_derive.rs
3+
//@aux-build:macro_rules.rs
14
#![warn(clippy::double_parens)]
25
#![expect(clippy::eq_op, clippy::no_effect)]
36
#![feature(custom_inner_attributes)]
47
#![rustfmt::skip]
58

9+
use proc_macros::{external, with_span};
10+
611
fn dummy_fn<T>(_: T) {}
712

813
struct DummyStruct;
@@ -96,4 +101,42 @@ fn issue9000(x: DummyStruct) {
96101
//~^ double_parens
97102
}
98103

104+
fn issue15892() {
105+
use macro_rules::double_parens;
106+
107+
#[repr(transparent)]
108+
#[derive(Clone, Copy, PartialEq, Eq)]
109+
pub struct InterruptMask(u32);
110+
111+
impl InterruptMask {
112+
pub const OE: InterruptMask = InterruptMask(1 << 10);
113+
pub const BE: InterruptMask = InterruptMask(1 << 9);
114+
pub const PE: InterruptMask = InterruptMask(1 << 8);
115+
pub const FE: InterruptMask = InterruptMask(1 << 7);
116+
pub const E: InterruptMask = double_parens!(Self::OE, Self::BE, Self::PE, Self::FE);
117+
#[allow(clippy::unnecessary_cast)]
118+
pub const F: InterruptMask = external!(
119+
InterruptMask((((Self::OE.union(Self::BE).union(Self::PE).union(Self::FE))).into_bits()) as u32)
120+
);
121+
#[allow(clippy::unnecessary_cast)]
122+
pub const G: InterruptMask = with_span!(span
123+
InterruptMask((((Self::OE.union(Self::BE).union(Self::PE).union(Self::FE))).into_bits()) as u32)
124+
);
125+
pub const fn into_bits(self) -> u32 {
126+
self.0
127+
}
128+
#[must_use]
129+
pub const fn union(self, rhs: Self) -> Self {
130+
InterruptMask(self.0 | rhs.0)
131+
}
132+
}
133+
}
134+
135+
fn issue15940() {
136+
use proc_macro_derive::DoubleParens;
137+
138+
#[derive(DoubleParens)]
139+
pub struct Person;
140+
}
141+
99142
fn main() {}

tests/ui/double_parens.stderr

Lines changed: 10 additions & 10 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:20:5
33
|
44
LL | ((0))
55
| ^^^^^ help: remove them: `(0)`
@@ -8,37 +8,37 @@ 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:25: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:30: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:35: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:41: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:64:16
3636
|
3737
LL | assert_eq!(((1, 2)), (1, 2), "Error");
3838
| ^^^^^^^^ help: remove them: `(1, 2)`
3939

4040
error: unnecessary parentheses
41-
--> tests/ui/double_parens.rs:84:16
41+
--> tests/ui/double_parens.rs:89:16
4242
|
4343
LL | () => {((100))}
4444
| ^^^^^^^ help: remove them: `(100)`
@@ -49,19 +49,19 @@ LL | bar!();
4949
= note: this error originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)
5050

5151
error: unnecessary parentheses
52-
--> tests/ui/double_parens.rs:91:5
52+
--> tests/ui/double_parens.rs:96:5
5353
|
5454
LL | ((vec![1, 2]));
5555
| ^^^^^^^^^^^^^^ help: remove them: `(vec![1, 2])`
5656

5757
error: unnecessary parentheses
58-
--> tests/ui/double_parens.rs:93:14
58+
--> tests/ui/double_parens.rs:98:14
5959
|
6060
LL | dummy_fn((vec![1, 2]));
6161
| ^^^^^^^^^^^^ help: remove them: `vec![1, 2]`
6262

6363
error: unnecessary parentheses
64-
--> tests/ui/double_parens.rs:95:20
64+
--> tests/ui/double_parens.rs:100:20
6565
|
6666
LL | x.dummy_method((vec![1, 2]));
6767
| ^^^^^^^^^^^^ help: remove them: `vec![1, 2]`

0 commit comments

Comments
 (0)