Skip to content

Commit

Permalink
Rollup merge of rust-lang#128782 - RalfJung:raw-addr-of-parens, r=com…
Browse files Browse the repository at this point in the history
…piler-errors

unused_parens: do not lint against parens around &raw

Requested by `@tmandry` in rust-lang#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.
  • Loading branch information
matthiaskrgr authored Aug 7, 2024
2 parents 04ab8e3 + c189796 commit aba506b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 32 deletions.
7 changes: 7 additions & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,13 @@ trait UnusedDelimLint {
return true;
}

// Do not lint against parentheses around `&raw [const|mut] expr`.
// These parentheses will have to be added e.g. when calling a method on the result of this
// expression, and we want to avoid churn wrt adding and removing parentheses.
if matches!(inner.kind, ast::ExprKind::AddrOf(ast::BorrowKind::Raw, ..)) {
return true;
}

// Check if LHS needs parens to prevent false-positives in cases like
// `fn x() -> u8 { ({ 0 } + 1) }`.
//
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/lint-unnecessary-parens.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ run-rustfix

#![deny(unused_parens)]
#![feature(raw_ref_op)]
#![allow(while_true)] // for rustfix

#[derive(Eq, PartialEq)]
Expand Down Expand Up @@ -125,4 +126,11 @@ fn main() {
// FIXME: false positive. This parenthesis is required.
unit! {} - One //~ ERROR unnecessary parentheses around block return value
};

// Do *not* lint around `&raw` (but do lint when `&` creates a reference).
let mut x = 0;
let _r = &x; //~ ERROR unnecessary parentheses
let _r = &mut x; //~ ERROR unnecessary parentheses
let _r = (&raw const x);
let _r = (&raw mut x);
}
8 changes: 8 additions & 0 deletions tests/ui/lint/lint-unnecessary-parens.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ run-rustfix

#![deny(unused_parens)]
#![feature(raw_ref_op)]
#![allow(while_true)] // for rustfix

#[derive(Eq, PartialEq)]
Expand Down Expand Up @@ -125,4 +126,11 @@ fn main() {
// FIXME: false positive. This parenthesis is required.
(unit! {} - One) //~ ERROR unnecessary parentheses around block return value
};

// Do *not* lint around `&raw` (but do lint when `&` creates a reference).
let mut x = 0;
let _r = (&x); //~ ERROR unnecessary parentheses
let _r = (&mut x); //~ ERROR unnecessary parentheses
let _r = (&raw const x);
let _r = (&raw mut x);
}
88 changes: 56 additions & 32 deletions tests/ui/lint/lint-unnecessary-parens.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unnecessary parentheses around `return` value
--> $DIR/lint-unnecessary-parens.rs:13:12
--> $DIR/lint-unnecessary-parens.rs:14:12
|
LL | return (1);
| ^ ^
Expand All @@ -16,7 +16,7 @@ LL + return 1;
|

error: unnecessary parentheses around `return` value
--> $DIR/lint-unnecessary-parens.rs:16:12
--> $DIR/lint-unnecessary-parens.rs:17:12
|
LL | return (X { y });
| ^ ^
Expand All @@ -28,7 +28,7 @@ LL + return X { y };
|

error: unnecessary parentheses around type
--> $DIR/lint-unnecessary-parens.rs:19:46
--> $DIR/lint-unnecessary-parens.rs:20:46
|
LL | pub fn unused_parens_around_return_type() -> (u32) {
| ^ ^
Expand All @@ -40,7 +40,7 @@ LL + pub fn unused_parens_around_return_type() -> u32 {
|

error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:25:9
--> $DIR/lint-unnecessary-parens.rs:26:9
|
LL | (5)
| ^ ^
Expand All @@ -52,7 +52,7 @@ LL + 5
|

error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:27:5
--> $DIR/lint-unnecessary-parens.rs:28:5
|
LL | (5)
| ^ ^
Expand All @@ -64,7 +64,7 @@ LL + 5
|

error: unnecessary parentheses around `if` condition
--> $DIR/lint-unnecessary-parens.rs:39:7
--> $DIR/lint-unnecessary-parens.rs:40:7
|
LL | if(true) {}
| ^ ^
Expand All @@ -76,7 +76,7 @@ LL + if true {}
|

error: unnecessary parentheses around `while` condition
--> $DIR/lint-unnecessary-parens.rs:40:10
--> $DIR/lint-unnecessary-parens.rs:41:10
|
LL | while(true) {}
| ^ ^
Expand All @@ -88,7 +88,7 @@ LL + while true {}
|

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

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

error: unnecessary parentheses around `return` value
--> $DIR/lint-unnecessary-parens.rs:43:11
--> $DIR/lint-unnecessary-parens.rs:44:11
|
LL | return(1);
| ^ ^
Expand All @@ -124,7 +124,7 @@ LL + return 1;
|

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:74:31
--> $DIR/lint-unnecessary-parens.rs:75:31
|
LL | pub const CONST_ITEM: usize = (10);
| ^ ^
Expand All @@ -136,7 +136,7 @@ LL + pub const CONST_ITEM: usize = 10;
|

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:75:33
--> $DIR/lint-unnecessary-parens.rs:76:33
|
LL | pub static STATIC_ITEM: usize = (10);
| ^ ^
Expand All @@ -148,7 +148,7 @@ LL + pub static STATIC_ITEM: usize = 10;
|

error: unnecessary parentheses around function argument
--> $DIR/lint-unnecessary-parens.rs:79:9
--> $DIR/lint-unnecessary-parens.rs:80:9
|
LL | bar((true));
| ^ ^
Expand All @@ -160,7 +160,7 @@ LL + bar(true);
|

error: unnecessary parentheses around `if` condition
--> $DIR/lint-unnecessary-parens.rs:81:8
--> $DIR/lint-unnecessary-parens.rs:82:8
|
LL | if (true) {}
| ^ ^
Expand All @@ -172,7 +172,7 @@ LL + if true {}
|

error: unnecessary parentheses around `while` condition
--> $DIR/lint-unnecessary-parens.rs:82:11
--> $DIR/lint-unnecessary-parens.rs:83:11
|
LL | while (true) {}
| ^ ^
Expand All @@ -184,7 +184,7 @@ LL + while true {}
|

error: unnecessary parentheses around `match` scrutinee expression
--> $DIR/lint-unnecessary-parens.rs:83:11
--> $DIR/lint-unnecessary-parens.rs:84:11
|
LL | match (true) {
| ^ ^
Expand All @@ -196,7 +196,7 @@ LL + match true {
|

error: unnecessary parentheses around `let` scrutinee expression
--> $DIR/lint-unnecessary-parens.rs:86:16
--> $DIR/lint-unnecessary-parens.rs:87:16
|
LL | if let 1 = (1) {}
| ^ ^
Expand All @@ -208,7 +208,7 @@ LL + if let 1 = 1 {}
|

error: unnecessary parentheses around `let` scrutinee expression
--> $DIR/lint-unnecessary-parens.rs:87:19
--> $DIR/lint-unnecessary-parens.rs:88:19
|
LL | while let 1 = (2) {}
| ^ ^
Expand All @@ -220,7 +220,7 @@ LL + while let 1 = 2 {}
|

error: unnecessary parentheses around method argument
--> $DIR/lint-unnecessary-parens.rs:103:24
--> $DIR/lint-unnecessary-parens.rs:104:24
|
LL | X { y: false }.foo((true));
| ^ ^
Expand All @@ -232,7 +232,7 @@ LL + X { y: false }.foo(true);
|

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:105:18
--> $DIR/lint-unnecessary-parens.rs:106:18
|
LL | let mut _a = (0);
| ^ ^
Expand All @@ -244,7 +244,7 @@ LL + let mut _a = 0;
|

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:106:10
--> $DIR/lint-unnecessary-parens.rs:107:10
|
LL | _a = (0);
| ^ ^
Expand All @@ -256,7 +256,7 @@ LL + _a = 0;
|

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:107:11
--> $DIR/lint-unnecessary-parens.rs:108:11
|
LL | _a += (1);
| ^ ^
Expand All @@ -268,7 +268,7 @@ LL + _a += 1;
|

error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:109:8
--> $DIR/lint-unnecessary-parens.rs:110:8
|
LL | let(mut _a) = 3;
| ^ ^
Expand All @@ -280,7 +280,7 @@ LL + let mut _a = 3;
|

error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:110:9
--> $DIR/lint-unnecessary-parens.rs:111:9
|
LL | let (mut _a) = 3;
| ^ ^
Expand All @@ -292,7 +292,7 @@ LL + let mut _a = 3;
|

error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:111:8
--> $DIR/lint-unnecessary-parens.rs:112:8
|
LL | let( mut _a) = 3;
| ^^ ^
Expand All @@ -304,7 +304,7 @@ LL + let mut _a = 3;
|

error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:113:8
--> $DIR/lint-unnecessary-parens.rs:114:8
|
LL | let(_a) = 3;
| ^ ^
Expand All @@ -316,7 +316,7 @@ LL + let _a = 3;
|

error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:114:9
--> $DIR/lint-unnecessary-parens.rs:115:9
|
LL | let (_a) = 3;
| ^ ^
Expand All @@ -328,7 +328,7 @@ LL + let _a = 3;
|

error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:115:8
--> $DIR/lint-unnecessary-parens.rs:116:8
|
LL | let( _a) = 3;
| ^^ ^
Expand All @@ -340,7 +340,7 @@ LL + let _a = 3;
|

error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:121:9
--> $DIR/lint-unnecessary-parens.rs:122:9
|
LL | (unit!() - One)
| ^ ^
Expand All @@ -352,7 +352,7 @@ LL + unit!() - One
|

error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:123:9
--> $DIR/lint-unnecessary-parens.rs:124:9
|
LL | (unit![] - One)
| ^ ^
Expand All @@ -364,7 +364,7 @@ LL + unit![] - One
|

error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:126:9
--> $DIR/lint-unnecessary-parens.rs:127:9
|
LL | (unit! {} - One)
| ^ ^
Expand All @@ -375,5 +375,29 @@ LL - (unit! {} - One)
LL + unit! {} - One
|

error: aborting due to 31 previous errors
error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:132:14
|
LL | let _r = (&x);
| ^ ^
|
help: remove these parentheses
|
LL - let _r = (&x);
LL + let _r = &x;
|

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:133:14
|
LL | let _r = (&mut x);
| ^ ^
|
help: remove these parentheses
|
LL - let _r = (&mut x);
LL + let _r = &mut x;
|

error: aborting due to 33 previous errors

0 comments on commit aba506b

Please sign in to comment.