Skip to content

Commit aba506b

Browse files
authored
Rollup merge of #128782 - RalfJung:raw-addr-of-parens, r=compiler-errors
unused_parens: do not lint against parens around &raw Requested by `@tmandry` in #127679: with `&raw` one somewhat regularly has to write code like `(&raw const (*myptr).field).method()`, so parentheses around the expression are often required. To avoid churn between adding and removing parentheses as method calls appear and disappear, the proposal was made to silence the lint for unnecessary parentheses around `&raw` expressions. This PR implements that.
2 parents 04ab8e3 + c189796 commit aba506b

File tree

4 files changed

+79
-32
lines changed

4 files changed

+79
-32
lines changed

Diff for: compiler/rustc_lint/src/unused.rs

+7
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,13 @@ trait UnusedDelimLint {
675675
return true;
676676
}
677677

678+
// Do not lint against parentheses around `&raw [const|mut] expr`.
679+
// These parentheses will have to be added e.g. when calling a method on the result of this
680+
// expression, and we want to avoid churn wrt adding and removing parentheses.
681+
if matches!(inner.kind, ast::ExprKind::AddrOf(ast::BorrowKind::Raw, ..)) {
682+
return true;
683+
}
684+
678685
// Check if LHS needs parens to prevent false-positives in cases like
679686
// `fn x() -> u8 { ({ 0 } + 1) }`.
680687
//

Diff for: tests/ui/lint/lint-unnecessary-parens.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ run-rustfix
22

33
#![deny(unused_parens)]
4+
#![feature(raw_ref_op)]
45
#![allow(while_true)] // for rustfix
56

67
#[derive(Eq, PartialEq)]
@@ -125,4 +126,11 @@ fn main() {
125126
// FIXME: false positive. This parenthesis is required.
126127
unit! {} - One //~ ERROR unnecessary parentheses around block return value
127128
};
129+
130+
// Do *not* lint around `&raw` (but do lint when `&` creates a reference).
131+
let mut x = 0;
132+
let _r = &x; //~ ERROR unnecessary parentheses
133+
let _r = &mut x; //~ ERROR unnecessary parentheses
134+
let _r = (&raw const x);
135+
let _r = (&raw mut x);
128136
}

Diff for: tests/ui/lint/lint-unnecessary-parens.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ run-rustfix
22

33
#![deny(unused_parens)]
4+
#![feature(raw_ref_op)]
45
#![allow(while_true)] // for rustfix
56

67
#[derive(Eq, PartialEq)]
@@ -125,4 +126,11 @@ fn main() {
125126
// FIXME: false positive. This parenthesis is required.
126127
(unit! {} - One) //~ ERROR unnecessary parentheses around block return value
127128
};
129+
130+
// Do *not* lint around `&raw` (but do lint when `&` creates a reference).
131+
let mut x = 0;
132+
let _r = (&x); //~ ERROR unnecessary parentheses
133+
let _r = (&mut x); //~ ERROR unnecessary parentheses
134+
let _r = (&raw const x);
135+
let _r = (&raw mut x);
128136
}

Diff for: tests/ui/lint/lint-unnecessary-parens.stderr

+56-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnecessary parentheses around `return` value
2-
--> $DIR/lint-unnecessary-parens.rs:13:12
2+
--> $DIR/lint-unnecessary-parens.rs:14:12
33
|
44
LL | return (1);
55
| ^ ^
@@ -16,7 +16,7 @@ LL + return 1;
1616
|
1717

1818
error: unnecessary parentheses around `return` value
19-
--> $DIR/lint-unnecessary-parens.rs:16:12
19+
--> $DIR/lint-unnecessary-parens.rs:17:12
2020
|
2121
LL | return (X { y });
2222
| ^ ^
@@ -28,7 +28,7 @@ LL + return X { y };
2828
|
2929

3030
error: unnecessary parentheses around type
31-
--> $DIR/lint-unnecessary-parens.rs:19:46
31+
--> $DIR/lint-unnecessary-parens.rs:20:46
3232
|
3333
LL | pub fn unused_parens_around_return_type() -> (u32) {
3434
| ^ ^
@@ -40,7 +40,7 @@ LL + pub fn unused_parens_around_return_type() -> u32 {
4040
|
4141

4242
error: unnecessary parentheses around block return value
43-
--> $DIR/lint-unnecessary-parens.rs:25:9
43+
--> $DIR/lint-unnecessary-parens.rs:26:9
4444
|
4545
LL | (5)
4646
| ^ ^
@@ -52,7 +52,7 @@ LL + 5
5252
|
5353

5454
error: unnecessary parentheses around block return value
55-
--> $DIR/lint-unnecessary-parens.rs:27:5
55+
--> $DIR/lint-unnecessary-parens.rs:28:5
5656
|
5757
LL | (5)
5858
| ^ ^
@@ -64,7 +64,7 @@ LL + 5
6464
|
6565

6666
error: unnecessary parentheses around `if` condition
67-
--> $DIR/lint-unnecessary-parens.rs:39:7
67+
--> $DIR/lint-unnecessary-parens.rs:40:7
6868
|
6969
LL | if(true) {}
7070
| ^ ^
@@ -76,7 +76,7 @@ LL + if true {}
7676
|
7777

7878
error: unnecessary parentheses around `while` condition
79-
--> $DIR/lint-unnecessary-parens.rs:40:10
79+
--> $DIR/lint-unnecessary-parens.rs:41:10
8080
|
8181
LL | while(true) {}
8282
| ^ ^
@@ -88,7 +88,7 @@ LL + while true {}
8888
|
8989

9090
error: unnecessary parentheses around `for` iterator expression
91-
--> $DIR/lint-unnecessary-parens.rs:41:13
91+
--> $DIR/lint-unnecessary-parens.rs:42:13
9292
|
9393
LL | for _ in(e) {}
9494
| ^ ^
@@ -100,7 +100,7 @@ LL + for _ in e {}
100100
|
101101

102102
error: unnecessary parentheses around `match` scrutinee expression
103-
--> $DIR/lint-unnecessary-parens.rs:42:10
103+
--> $DIR/lint-unnecessary-parens.rs:43:10
104104
|
105105
LL | match(1) { _ => ()}
106106
| ^ ^
@@ -112,7 +112,7 @@ LL + match 1 { _ => ()}
112112
|
113113

114114
error: unnecessary parentheses around `return` value
115-
--> $DIR/lint-unnecessary-parens.rs:43:11
115+
--> $DIR/lint-unnecessary-parens.rs:44:11
116116
|
117117
LL | return(1);
118118
| ^ ^
@@ -124,7 +124,7 @@ LL + return 1;
124124
|
125125

126126
error: unnecessary parentheses around assigned value
127-
--> $DIR/lint-unnecessary-parens.rs:74:31
127+
--> $DIR/lint-unnecessary-parens.rs:75: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:75:33
139+
--> $DIR/lint-unnecessary-parens.rs:76: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:79:9
151+
--> $DIR/lint-unnecessary-parens.rs:80: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:81:8
163+
--> $DIR/lint-unnecessary-parens.rs:82: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:82:11
175+
--> $DIR/lint-unnecessary-parens.rs:83: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:83:11
187+
--> $DIR/lint-unnecessary-parens.rs:84: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:86:16
199+
--> $DIR/lint-unnecessary-parens.rs:87: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:87:19
211+
--> $DIR/lint-unnecessary-parens.rs:88: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:103:24
223+
--> $DIR/lint-unnecessary-parens.rs:104: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:105:18
235+
--> $DIR/lint-unnecessary-parens.rs:106: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:106:10
247+
--> $DIR/lint-unnecessary-parens.rs:107: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:107:11
259+
--> $DIR/lint-unnecessary-parens.rs:108: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:109:8
271+
--> $DIR/lint-unnecessary-parens.rs:110: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:110:9
283+
--> $DIR/lint-unnecessary-parens.rs:111: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:111:8
295+
--> $DIR/lint-unnecessary-parens.rs:112: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:113:8
307+
--> $DIR/lint-unnecessary-parens.rs:114: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:114:9
319+
--> $DIR/lint-unnecessary-parens.rs:115: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:115:8
331+
--> $DIR/lint-unnecessary-parens.rs:116:8
332332
|
333333
LL | let( _a) = 3;
334334
| ^^ ^
@@ -340,7 +340,7 @@ LL + let _a = 3;
340340
|
341341

342342
error: unnecessary parentheses around block return value
343-
--> $DIR/lint-unnecessary-parens.rs:121:9
343+
--> $DIR/lint-unnecessary-parens.rs:122:9
344344
|
345345
LL | (unit!() - One)
346346
| ^ ^
@@ -352,7 +352,7 @@ LL + unit!() - One
352352
|
353353

354354
error: unnecessary parentheses around block return value
355-
--> $DIR/lint-unnecessary-parens.rs:123:9
355+
--> $DIR/lint-unnecessary-parens.rs:124:9
356356
|
357357
LL | (unit![] - One)
358358
| ^ ^
@@ -364,7 +364,7 @@ LL + unit![] - One
364364
|
365365

366366
error: unnecessary parentheses around block return value
367-
--> $DIR/lint-unnecessary-parens.rs:126:9
367+
--> $DIR/lint-unnecessary-parens.rs:127:9
368368
|
369369
LL | (unit! {} - One)
370370
| ^ ^
@@ -375,5 +375,29 @@ LL - (unit! {} - One)
375375
LL + unit! {} - One
376376
|
377377

378-
error: aborting due to 31 previous errors
378+
error: unnecessary parentheses around assigned value
379+
--> $DIR/lint-unnecessary-parens.rs:132:14
380+
|
381+
LL | let _r = (&x);
382+
| ^ ^
383+
|
384+
help: remove these parentheses
385+
|
386+
LL - let _r = (&x);
387+
LL + let _r = &x;
388+
|
389+
390+
error: unnecessary parentheses around assigned value
391+
--> $DIR/lint-unnecessary-parens.rs:133:14
392+
|
393+
LL | let _r = (&mut x);
394+
| ^ ^
395+
|
396+
help: remove these parentheses
397+
|
398+
LL - let _r = (&mut x);
399+
LL + let _r = &mut x;
400+
|
401+
402+
error: aborting due to 33 previous errors
379403

0 commit comments

Comments
 (0)