Skip to content

Commit 3901228

Browse files
committed
rustc: add lint for parens in if, while, match and return.
The parens in `if (true) {}` are not not necessary, so we'll warn about them.
1 parent 40df5a2 commit 3901228

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/librustc/middle/lint.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub enum Lint {
7878
NonCamelCaseTypes,
7979
NonUppercaseStatics,
8080
NonUppercasePatternStatics,
81+
UnnecessaryParens,
8182
TypeLimits,
8283
TypeOverflow,
8384
UnusedUnsafe,
@@ -162,7 +163,7 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
162163
("while_true",
163164
LintSpec {
164165
lint: WhileTrue,
165-
desc: "suggest using loop { } instead of while(true) { }",
166+
desc: "suggest using `loop { }` instead of `while true { }`",
166167
default: warn
167168
}),
168169

@@ -201,6 +202,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
201202
default: warn
202203
}),
203204

205+
("unnecessary_parens",
206+
LintSpec {
207+
lint: UnnecessaryParens,
208+
desc: "`if`, `match`, `while` and `return` do not need parentheses",
209+
default: warn
210+
}),
211+
204212
("managed_heap_memory",
205213
LintSpec {
206214
lint: ManagedHeapMemory,
@@ -1080,6 +1088,24 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
10801088
}
10811089
}
10821090

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+
10831109
fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
10841110
match e.node {
10851111
// Don't warn about generated blocks, that'll just pollute the output.
@@ -1438,6 +1464,7 @@ impl<'a> Visitor<()> for Context<'a> {
14381464

14391465
check_while_true_expr(self, e);
14401466
check_stability(self, e);
1467+
check_unnecessary_parens(self, e);
14411468
check_unused_unsafe(self, e);
14421469
check_unsafe_block(self, e);
14431470
check_unnecessary_allocation(self, e);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[deny(unnecessary_parens)];
12+
13+
fn foo() -> int {
14+
return (1); //~ ERROR unnecessary parentheses around `return` value
15+
}
16+
17+
fn main() {
18+
foo();
19+
20+
if (true) {} //~ ERROR unnecessary parentheses around `if` condition
21+
while (true) {} //~ ERROR unnecessary parentheses around `while` condition
22+
match (true) { //~ ERROR unnecessary parentheses around `match` head expression
23+
_ => {}
24+
}
25+
}

0 commit comments

Comments
 (0)