Skip to content

Commit 377d35b

Browse files
committed
Add test of unused_parens lint involving macro calls
1 parent b67a803 commit 377d35b

File tree

3 files changed

+117
-19
lines changed

3 files changed

+117
-19
lines changed

tests/ui/lint/lint-unnecessary-parens.fixed

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ pub fn parens_with_keyword(e: &[()]) -> i32 {
4646
macro_rules! baz {
4747
($($foo:expr),+) => {
4848
($($foo),*)
49+
};
50+
}
51+
52+
macro_rules! unit {
53+
() => {
54+
()
55+
};
56+
}
57+
58+
struct One;
59+
60+
impl std::ops::Sub<One> for () {
61+
type Output = i32;
62+
fn sub(self, _: One) -> Self::Output {
63+
-1
64+
}
65+
}
66+
67+
impl std::ops::Neg for One {
68+
type Output = i32;
69+
fn neg(self) -> Self::Output {
70+
-1
4971
}
5072
}
5173

@@ -94,4 +116,13 @@ fn main() {
94116

95117
let _a = baz!(3, 4);
96118
let _b = baz!(3);
119+
120+
let _ = {
121+
unit!() - One //~ ERROR unnecessary parentheses around block return value
122+
} + {
123+
unit![] - One //~ ERROR unnecessary parentheses around block return value
124+
} + {
125+
// FIXME: false positive. This parenthesis is required.
126+
unit! {} - One //~ ERROR unnecessary parentheses around block return value
127+
};
97128
}

tests/ui/lint/lint-unnecessary-parens.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ pub fn parens_with_keyword(e: &[()]) -> i32 {
4646
macro_rules! baz {
4747
($($foo:expr),+) => {
4848
($($foo),*)
49+
};
50+
}
51+
52+
macro_rules! unit {
53+
() => {
54+
()
55+
};
56+
}
57+
58+
struct One;
59+
60+
impl std::ops::Sub<One> for () {
61+
type Output = i32;
62+
fn sub(self, _: One) -> Self::Output {
63+
-1
64+
}
65+
}
66+
67+
impl std::ops::Neg for One {
68+
type Output = i32;
69+
fn neg(self) -> Self::Output {
70+
-1
4971
}
5072
}
5173

@@ -94,4 +116,13 @@ fn main() {
94116

95117
let _a = baz!(3, 4);
96118
let _b = baz!(3);
119+
120+
let _ = {
121+
(unit!() - One) //~ ERROR unnecessary parentheses around block return value
122+
} + {
123+
(unit![] - One) //~ ERROR unnecessary parentheses around block return value
124+
} + {
125+
// FIXME: false positive. This parenthesis is required.
126+
(unit! {} - One) //~ ERROR unnecessary parentheses around block return value
127+
};
97128
}

tests/ui/lint/lint-unnecessary-parens.stderr

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ LL + return 1;
124124
|
125125

126126
error: unnecessary parentheses around assigned value
127-
--> $DIR/lint-unnecessary-parens.rs:52:31
127+
--> $DIR/lint-unnecessary-parens.rs:74:31
128128
|
129129
LL | pub const CONST_ITEM: usize = (10);
130130
| ^ ^
@@ -136,7 +136,7 @@ LL + pub const CONST_ITEM: usize = 10;
136136
|
137137

138138
error: unnecessary parentheses around assigned value
139-
--> $DIR/lint-unnecessary-parens.rs:53:33
139+
--> $DIR/lint-unnecessary-parens.rs:75:33
140140
|
141141
LL | pub static STATIC_ITEM: usize = (10);
142142
| ^ ^
@@ -148,7 +148,7 @@ LL + pub static STATIC_ITEM: usize = 10;
148148
|
149149

150150
error: unnecessary parentheses around function argument
151-
--> $DIR/lint-unnecessary-parens.rs:57:9
151+
--> $DIR/lint-unnecessary-parens.rs:79:9
152152
|
153153
LL | bar((true));
154154
| ^ ^
@@ -160,7 +160,7 @@ LL + bar(true);
160160
|
161161

162162
error: unnecessary parentheses around `if` condition
163-
--> $DIR/lint-unnecessary-parens.rs:59:8
163+
--> $DIR/lint-unnecessary-parens.rs:81:8
164164
|
165165
LL | if (true) {}
166166
| ^ ^
@@ -172,7 +172,7 @@ LL + if true {}
172172
|
173173

174174
error: unnecessary parentheses around `while` condition
175-
--> $DIR/lint-unnecessary-parens.rs:60:11
175+
--> $DIR/lint-unnecessary-parens.rs:82:11
176176
|
177177
LL | while (true) {}
178178
| ^ ^
@@ -184,7 +184,7 @@ LL + while true {}
184184
|
185185

186186
error: unnecessary parentheses around `match` scrutinee expression
187-
--> $DIR/lint-unnecessary-parens.rs:61:11
187+
--> $DIR/lint-unnecessary-parens.rs:83:11
188188
|
189189
LL | match (true) {
190190
| ^ ^
@@ -196,7 +196,7 @@ LL + match true {
196196
|
197197

198198
error: unnecessary parentheses around `let` scrutinee expression
199-
--> $DIR/lint-unnecessary-parens.rs:64:16
199+
--> $DIR/lint-unnecessary-parens.rs:86:16
200200
|
201201
LL | if let 1 = (1) {}
202202
| ^ ^
@@ -208,7 +208,7 @@ LL + if let 1 = 1 {}
208208
|
209209

210210
error: unnecessary parentheses around `let` scrutinee expression
211-
--> $DIR/lint-unnecessary-parens.rs:65:19
211+
--> $DIR/lint-unnecessary-parens.rs:87:19
212212
|
213213
LL | while let 1 = (2) {}
214214
| ^ ^
@@ -220,7 +220,7 @@ LL + while let 1 = 2 {}
220220
|
221221

222222
error: unnecessary parentheses around method argument
223-
--> $DIR/lint-unnecessary-parens.rs:81:24
223+
--> $DIR/lint-unnecessary-parens.rs:103:24
224224
|
225225
LL | X { y: false }.foo((true));
226226
| ^ ^
@@ -232,7 +232,7 @@ LL + X { y: false }.foo(true);
232232
|
233233

234234
error: unnecessary parentheses around assigned value
235-
--> $DIR/lint-unnecessary-parens.rs:83:18
235+
--> $DIR/lint-unnecessary-parens.rs:105:18
236236
|
237237
LL | let mut _a = (0);
238238
| ^ ^
@@ -244,7 +244,7 @@ LL + let mut _a = 0;
244244
|
245245

246246
error: unnecessary parentheses around assigned value
247-
--> $DIR/lint-unnecessary-parens.rs:84:10
247+
--> $DIR/lint-unnecessary-parens.rs:106:10
248248
|
249249
LL | _a = (0);
250250
| ^ ^
@@ -256,7 +256,7 @@ LL + _a = 0;
256256
|
257257

258258
error: unnecessary parentheses around assigned value
259-
--> $DIR/lint-unnecessary-parens.rs:85:11
259+
--> $DIR/lint-unnecessary-parens.rs:107:11
260260
|
261261
LL | _a += (1);
262262
| ^ ^
@@ -268,7 +268,7 @@ LL + _a += 1;
268268
|
269269

270270
error: unnecessary parentheses around pattern
271-
--> $DIR/lint-unnecessary-parens.rs:87:8
271+
--> $DIR/lint-unnecessary-parens.rs:109:8
272272
|
273273
LL | let(mut _a) = 3;
274274
| ^ ^
@@ -280,7 +280,7 @@ LL + let mut _a = 3;
280280
|
281281

282282
error: unnecessary parentheses around pattern
283-
--> $DIR/lint-unnecessary-parens.rs:88:9
283+
--> $DIR/lint-unnecessary-parens.rs:110:9
284284
|
285285
LL | let (mut _a) = 3;
286286
| ^ ^
@@ -292,7 +292,7 @@ LL + let mut _a = 3;
292292
|
293293

294294
error: unnecessary parentheses around pattern
295-
--> $DIR/lint-unnecessary-parens.rs:89:8
295+
--> $DIR/lint-unnecessary-parens.rs:111:8
296296
|
297297
LL | let( mut _a) = 3;
298298
| ^^ ^
@@ -304,7 +304,7 @@ LL + let mut _a = 3;
304304
|
305305

306306
error: unnecessary parentheses around pattern
307-
--> $DIR/lint-unnecessary-parens.rs:91:8
307+
--> $DIR/lint-unnecessary-parens.rs:113:8
308308
|
309309
LL | let(_a) = 3;
310310
| ^ ^
@@ -316,7 +316,7 @@ LL + let _a = 3;
316316
|
317317

318318
error: unnecessary parentheses around pattern
319-
--> $DIR/lint-unnecessary-parens.rs:92:9
319+
--> $DIR/lint-unnecessary-parens.rs:114:9
320320
|
321321
LL | let (_a) = 3;
322322
| ^ ^
@@ -328,7 +328,7 @@ LL + let _a = 3;
328328
|
329329

330330
error: unnecessary parentheses around pattern
331-
--> $DIR/lint-unnecessary-parens.rs:93:8
331+
--> $DIR/lint-unnecessary-parens.rs:115:8
332332
|
333333
LL | let( _a) = 3;
334334
| ^^ ^
@@ -339,5 +339,41 @@ LL - let( _a) = 3;
339339
LL + let _a = 3;
340340
|
341341

342-
error: aborting due to 28 previous errors
342+
error: unnecessary parentheses around block return value
343+
--> $DIR/lint-unnecessary-parens.rs:121:9
344+
|
345+
LL | (unit!() - One)
346+
| ^ ^
347+
|
348+
help: remove these parentheses
349+
|
350+
LL - (unit!() - One)
351+
LL + unit!() - One
352+
|
353+
354+
error: unnecessary parentheses around block return value
355+
--> $DIR/lint-unnecessary-parens.rs:123:9
356+
|
357+
LL | (unit![] - One)
358+
| ^ ^
359+
|
360+
help: remove these parentheses
361+
|
362+
LL - (unit![] - One)
363+
LL + unit![] - One
364+
|
365+
366+
error: unnecessary parentheses around block return value
367+
--> $DIR/lint-unnecessary-parens.rs:126:9
368+
|
369+
LL | (unit! {} - One)
370+
| ^ ^
371+
|
372+
help: remove these parentheses
373+
|
374+
LL - (unit! {} - One)
375+
LL + unit! {} - One
376+
|
377+
378+
error: aborting due to 31 previous errors
343379

0 commit comments

Comments
 (0)