@@ -78,6 +78,7 @@ pub enum Lint {
78
78
NonCamelCaseTypes ,
79
79
NonUppercaseStatics ,
80
80
NonUppercasePatternStatics ,
81
+ UnnecessaryParens ,
81
82
TypeLimits ,
82
83
TypeOverflow ,
83
84
UnusedUnsafe ,
@@ -162,7 +163,7 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
162
163
( "while_true" ,
163
164
LintSpec {
164
165
lint : WhileTrue ,
165
- desc : "suggest using loop { } instead of while( true) { }" ,
166
+ desc : "suggest using ` loop { }` instead of ` while true { }` " ,
166
167
default : warn
167
168
} ) ,
168
169
@@ -201,6 +202,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
201
202
default : warn
202
203
} ) ,
203
204
205
+ ( "unnecessary_parens" ,
206
+ LintSpec {
207
+ lint : UnnecessaryParens ,
208
+ desc : "`if`, `match`, `while` and `return` do not need parentheses" ,
209
+ default : warn
210
+ } ) ,
211
+
204
212
( "managed_heap_memory" ,
205
213
LintSpec {
206
214
lint : ManagedHeapMemory ,
@@ -1080,6 +1088,24 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
1080
1088
}
1081
1089
}
1082
1090
1091
+ fn check_unnecessary_parens ( cx : & Context , e : & ast:: Expr ) {
1092
+ let ( value, msg) = match e. node {
1093
+ ast:: ExprIf ( cond, _, _) => ( cond, "`if` condition" ) ,
1094
+ ast:: ExprWhile ( cond, _) => ( cond, "`while` condition" ) ,
1095
+ ast:: ExprMatch ( head, _) => ( head, "`match` head expression" ) ,
1096
+ ast:: ExprRet ( Some ( value) ) => ( value, "`return` value" ) ,
1097
+ _ => return
1098
+ } ;
1099
+
1100
+ match value. node {
1101
+ ast:: ExprParen ( _) => {
1102
+ cx. span_lint ( UnnecessaryParens , value. span ,
1103
+ format ! ( "unnecessary parentheses around {}" , msg) )
1104
+ }
1105
+ _ => { }
1106
+ }
1107
+ }
1108
+
1083
1109
fn check_unused_unsafe ( cx : & Context , e : & ast:: Expr ) {
1084
1110
match e. node {
1085
1111
// Don't warn about generated blocks, that'll just pollute the output.
@@ -1438,6 +1464,7 @@ impl<'a> Visitor<()> for Context<'a> {
1438
1464
1439
1465
check_while_true_expr ( self , e) ;
1440
1466
check_stability ( self , e) ;
1467
+ check_unnecessary_parens ( self , e) ;
1441
1468
check_unused_unsafe ( self , e) ;
1442
1469
check_unsafe_block ( self , e) ;
1443
1470
check_unnecessary_allocation ( self , e) ;
0 commit comments