unused_parens: do not lint against parens around &raw

This commit is contained in:
Ralf Jung 2024-08-07 15:29:00 +02:00
parent 6a2cd0d50c
commit c1897960c0
4 changed files with 79 additions and 32 deletions

View File

@ -675,6 +675,13 @@ trait UnusedDelimLint {
return true; 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 // Check if LHS needs parens to prevent false-positives in cases like
// `fn x() -> u8 { ({ 0 } + 1) }`. // `fn x() -> u8 { ({ 0 } + 1) }`.
// //

View File

@ -1,6 +1,7 @@
//@ run-rustfix //@ run-rustfix
#![deny(unused_parens)] #![deny(unused_parens)]
#![feature(raw_ref_op)]
#![allow(while_true)] // for rustfix #![allow(while_true)] // for rustfix
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
@ -125,4 +126,11 @@ fn main() {
// FIXME: false positive. This parenthesis is required. // FIXME: false positive. This parenthesis is required.
unit! {} - One //~ ERROR unnecessary parentheses around block return value 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);
} }

View File

@ -1,6 +1,7 @@
//@ run-rustfix //@ run-rustfix
#![deny(unused_parens)] #![deny(unused_parens)]
#![feature(raw_ref_op)]
#![allow(while_true)] // for rustfix #![allow(while_true)] // for rustfix
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
@ -125,4 +126,11 @@ fn main() {
// FIXME: false positive. This parenthesis is required. // FIXME: false positive. This parenthesis is required.
(unit! {} - One) //~ ERROR unnecessary parentheses around block return value (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);
} }

View File

@ -1,5 +1,5 @@
error: unnecessary parentheses around `return` value error: unnecessary parentheses around `return` value
--> $DIR/lint-unnecessary-parens.rs:13:12 --> $DIR/lint-unnecessary-parens.rs:14:12
| |
LL | return (1); LL | return (1);
| ^ ^ | ^ ^
@ -16,7 +16,7 @@ LL + return 1;
| |
error: unnecessary parentheses around `return` value error: unnecessary parentheses around `return` value
--> $DIR/lint-unnecessary-parens.rs:16:12 --> $DIR/lint-unnecessary-parens.rs:17:12
| |
LL | return (X { y }); LL | return (X { y });
| ^ ^ | ^ ^
@ -28,7 +28,7 @@ LL + return X { y };
| |
error: unnecessary parentheses around type 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) { LL | pub fn unused_parens_around_return_type() -> (u32) {
| ^ ^ | ^ ^
@ -40,7 +40,7 @@ LL + pub fn unused_parens_around_return_type() -> u32 {
| |
error: unnecessary parentheses around block return value error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:25:9 --> $DIR/lint-unnecessary-parens.rs:26:9
| |
LL | (5) LL | (5)
| ^ ^ | ^ ^
@ -52,7 +52,7 @@ LL + 5
| |
error: unnecessary parentheses around block return value error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:27:5 --> $DIR/lint-unnecessary-parens.rs:28:5
| |
LL | (5) LL | (5)
| ^ ^ | ^ ^
@ -64,7 +64,7 @@ LL + 5
| |
error: unnecessary parentheses around `if` condition error: unnecessary parentheses around `if` condition
--> $DIR/lint-unnecessary-parens.rs:39:7 --> $DIR/lint-unnecessary-parens.rs:40:7
| |
LL | if(true) {} LL | if(true) {}
| ^ ^ | ^ ^
@ -76,7 +76,7 @@ LL + if true {}
| |
error: unnecessary parentheses around `while` condition error: unnecessary parentheses around `while` condition
--> $DIR/lint-unnecessary-parens.rs:40:10 --> $DIR/lint-unnecessary-parens.rs:41:10
| |
LL | while(true) {} LL | while(true) {}
| ^ ^ | ^ ^
@ -88,7 +88,7 @@ LL + while true {}
| |
error: unnecessary parentheses around `for` iterator expression 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) {} LL | for _ in(e) {}
| ^ ^ | ^ ^
@ -100,7 +100,7 @@ LL + for _ in e {}
| |
error: unnecessary parentheses around `match` scrutinee expression error: unnecessary parentheses around `match` scrutinee expression
--> $DIR/lint-unnecessary-parens.rs:42:10 --> $DIR/lint-unnecessary-parens.rs:43:10
| |
LL | match(1) { _ => ()} LL | match(1) { _ => ()}
| ^ ^ | ^ ^
@ -112,7 +112,7 @@ LL + match 1 { _ => ()}
| |
error: unnecessary parentheses around `return` value error: unnecessary parentheses around `return` value
--> $DIR/lint-unnecessary-parens.rs:43:11 --> $DIR/lint-unnecessary-parens.rs:44:11
| |
LL | return(1); LL | return(1);
| ^ ^ | ^ ^
@ -124,7 +124,7 @@ LL + return 1;
| |
error: unnecessary parentheses around assigned value 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); LL | pub const CONST_ITEM: usize = (10);
| ^ ^ | ^ ^
@ -136,7 +136,7 @@ LL + pub const CONST_ITEM: usize = 10;
| |
error: unnecessary parentheses around assigned value 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); LL | pub static STATIC_ITEM: usize = (10);
| ^ ^ | ^ ^
@ -148,7 +148,7 @@ LL + pub static STATIC_ITEM: usize = 10;
| |
error: unnecessary parentheses around function argument error: unnecessary parentheses around function argument
--> $DIR/lint-unnecessary-parens.rs:79:9 --> $DIR/lint-unnecessary-parens.rs:80:9
| |
LL | bar((true)); LL | bar((true));
| ^ ^ | ^ ^
@ -160,7 +160,7 @@ LL + bar(true);
| |
error: unnecessary parentheses around `if` condition error: unnecessary parentheses around `if` condition
--> $DIR/lint-unnecessary-parens.rs:81:8 --> $DIR/lint-unnecessary-parens.rs:82:8
| |
LL | if (true) {} LL | if (true) {}
| ^ ^ | ^ ^
@ -172,7 +172,7 @@ LL + if true {}
| |
error: unnecessary parentheses around `while` condition error: unnecessary parentheses around `while` condition
--> $DIR/lint-unnecessary-parens.rs:82:11 --> $DIR/lint-unnecessary-parens.rs:83:11
| |
LL | while (true) {} LL | while (true) {}
| ^ ^ | ^ ^
@ -184,7 +184,7 @@ LL + while true {}
| |
error: unnecessary parentheses around `match` scrutinee expression error: unnecessary parentheses around `match` scrutinee expression
--> $DIR/lint-unnecessary-parens.rs:83:11 --> $DIR/lint-unnecessary-parens.rs:84:11
| |
LL | match (true) { LL | match (true) {
| ^ ^ | ^ ^
@ -196,7 +196,7 @@ LL + match true {
| |
error: unnecessary parentheses around `let` scrutinee expression 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) {} LL | if let 1 = (1) {}
| ^ ^ | ^ ^
@ -208,7 +208,7 @@ LL + if let 1 = 1 {}
| |
error: unnecessary parentheses around `let` scrutinee expression 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) {} LL | while let 1 = (2) {}
| ^ ^ | ^ ^
@ -220,7 +220,7 @@ LL + while let 1 = 2 {}
| |
error: unnecessary parentheses around method argument 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)); LL | X { y: false }.foo((true));
| ^ ^ | ^ ^
@ -232,7 +232,7 @@ LL + X { y: false }.foo(true);
| |
error: unnecessary parentheses around assigned value 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); LL | let mut _a = (0);
| ^ ^ | ^ ^
@ -244,7 +244,7 @@ LL + let mut _a = 0;
| |
error: unnecessary parentheses around assigned value error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:106:10 --> $DIR/lint-unnecessary-parens.rs:107:10
| |
LL | _a = (0); LL | _a = (0);
| ^ ^ | ^ ^
@ -256,7 +256,7 @@ LL + _a = 0;
| |
error: unnecessary parentheses around assigned value error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:107:11 --> $DIR/lint-unnecessary-parens.rs:108:11
| |
LL | _a += (1); LL | _a += (1);
| ^ ^ | ^ ^
@ -268,7 +268,7 @@ LL + _a += 1;
| |
error: unnecessary parentheses around pattern error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:109:8 --> $DIR/lint-unnecessary-parens.rs:110:8
| |
LL | let(mut _a) = 3; LL | let(mut _a) = 3;
| ^ ^ | ^ ^
@ -280,7 +280,7 @@ LL + let mut _a = 3;
| |
error: unnecessary parentheses around pattern error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:110:9 --> $DIR/lint-unnecessary-parens.rs:111:9
| |
LL | let (mut _a) = 3; LL | let (mut _a) = 3;
| ^ ^ | ^ ^
@ -292,7 +292,7 @@ LL + let mut _a = 3;
| |
error: unnecessary parentheses around pattern error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:111:8 --> $DIR/lint-unnecessary-parens.rs:112:8
| |
LL | let( mut _a) = 3; LL | let( mut _a) = 3;
| ^^ ^ | ^^ ^
@ -304,7 +304,7 @@ LL + let mut _a = 3;
| |
error: unnecessary parentheses around pattern error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:113:8 --> $DIR/lint-unnecessary-parens.rs:114:8
| |
LL | let(_a) = 3; LL | let(_a) = 3;
| ^ ^ | ^ ^
@ -316,7 +316,7 @@ LL + let _a = 3;
| |
error: unnecessary parentheses around pattern error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:114:9 --> $DIR/lint-unnecessary-parens.rs:115:9
| |
LL | let (_a) = 3; LL | let (_a) = 3;
| ^ ^ | ^ ^
@ -328,7 +328,7 @@ LL + let _a = 3;
| |
error: unnecessary parentheses around pattern error: unnecessary parentheses around pattern
--> $DIR/lint-unnecessary-parens.rs:115:8 --> $DIR/lint-unnecessary-parens.rs:116:8
| |
LL | let( _a) = 3; LL | let( _a) = 3;
| ^^ ^ | ^^ ^
@ -340,7 +340,7 @@ LL + let _a = 3;
| |
error: unnecessary parentheses around block return value error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:121:9 --> $DIR/lint-unnecessary-parens.rs:122:9
| |
LL | (unit!() - One) LL | (unit!() - One)
| ^ ^ | ^ ^
@ -352,7 +352,7 @@ LL + unit!() - One
| |
error: unnecessary parentheses around block return value error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:123:9 --> $DIR/lint-unnecessary-parens.rs:124:9
| |
LL | (unit![] - One) LL | (unit![] - One)
| ^ ^ | ^ ^
@ -364,7 +364,7 @@ LL + unit![] - One
| |
error: unnecessary parentheses around block return value error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:126:9 --> $DIR/lint-unnecessary-parens.rs:127:9
| |
LL | (unit! {} - One) LL | (unit! {} - One)
| ^ ^ | ^ ^
@ -375,5 +375,29 @@ LL - (unit! {} - One)
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