Skip to content

Commit 08a3a95

Browse files
committed
---
yaml --- r: 152901 b: refs/heads/try2 c: 64019e7 h: refs/heads/master i: 152899: 1890590 v: v3
1 parent 9ecbec1 commit 08a3a95

File tree

6 files changed

+80
-105
lines changed

6 files changed

+80
-105
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: d0983872efeea757600031a081a2eff9676fe895
8+
refs/heads/try2: 64019e764f1837a4a19297fc9f7a99595e37cf51
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/doc/guide.md

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -411,64 +411,3 @@ rest of your Rust career.
411411

412412
Next, we'll learn more about Rust itself, by starting to write a more complicated
413413
program. We hope you want to do more with Rust than just print "Hello, world!"
414-
415-
## If
416-
417-
## Functions
418-
419-
return
420-
421-
comments
422-
423-
## Testing
424-
425-
attributes
426-
427-
stability markers
428-
429-
## Crates and Modules
430-
431-
visibility
432-
433-
## Compound Data Types
434-
435-
Tuples
436-
437-
Structs
438-
439-
Enums
440-
441-
## Match
442-
443-
## Looping
444-
445-
for
446-
447-
while
448-
449-
loop
450-
451-
break/continue
452-
453-
iterators
454-
455-
## Lambdas
456-
457-
## Generics
458-
459-
## Traits
460-
461-
## Operators and built-in Traits
462-
463-
## Ownership and Lifetimes
464-
465-
Move vs. Copy
466-
467-
Allocation
468-
469-
## Tasks
470-
471-
## Macros
472-
473-
## Unsafe
474-

branches/try2/src/doc/tutorial.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,11 @@ by an *action* (expression). Each case is separated by commas. It is
493493
often convenient to use a block expression for each case, in which case
494494
the commas are optional as shown below. Literals are valid patterns and
495495
match only their own value. A single arm may match multiple different
496-
patterns by combining them with the pipe operator (`|`), so long as
497-
every pattern binds the same set of variables (see "destructuring"
498-
below). Ranges of numeric literal patterns can be expressed with two
499-
dots, as in `M..N`. The underscore (`_`) is a wildcard pattern that
500-
matches any single value. (`..`) is a different wildcard that can match
501-
one or more fields in an `enum` variant.
496+
patterns by combining them with the pipe operator (`|`), so long as every
497+
pattern binds the same set of variables. Ranges of numeric literal
498+
patterns can be expressed with two dots, as in `M..N`. The underscore
499+
(`_`) is a wildcard pattern that matches any single value. (`..`) is a
500+
different wildcard that can match one or more fields in an `enum` variant.
502501

503502
~~~
504503
# let my_number = 1;

branches/try2/src/librustc/lint/builtin.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,52 @@ declare_lint!(UNNECESSARY_PARENS, Warn,
975975
pub struct UnnecessaryParens;
976976

977977
impl UnnecessaryParens {
978-
fn check_unnecessary_parens_core(&self, cx: &Context, value: &ast::Expr, msg: &str) {
978+
fn check_unnecessary_parens_core(&self, cx: &Context, value: &ast::Expr, msg: &str,
979+
struct_lit_needs_parens: bool) {
979980
match value.node {
980-
ast::ExprParen(_) => {
981-
cx.span_lint(UNNECESSARY_PARENS, value.span,
982-
format!("unnecessary parentheses around {}", msg).as_slice())
981+
ast::ExprParen(ref inner) => {
982+
let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&**inner);
983+
if !necessary {
984+
cx.span_lint(UNNECESSARY_PARENS, value.span,
985+
format!("unnecessary parentheses around {}",
986+
msg).as_slice())
987+
}
983988
}
984989
_ => {}
985990
}
991+
992+
/// Expressions that syntatically contain an "exterior" struct
993+
/// literal i.e. not surrounded by any parens or other
994+
/// delimiters, e.g. `X { y: 1 }`, `X { y: 1 }.method()`, `foo
995+
/// == X { y: 1 }` and `X { y: 1 } == foo` all do, but `(X {
996+
/// y: 1 }) == foo` does not.
997+
fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
998+
match value.node {
999+
ast::ExprStruct(..) => true,
1000+
1001+
ast::ExprAssign(ref lhs, ref rhs) |
1002+
ast::ExprAssignOp(_, ref lhs, ref rhs) |
1003+
ast::ExprBinary(_, ref lhs, ref rhs) => {
1004+
// X { y: 1 } + X { y: 2 }
1005+
contains_exterior_struct_lit(&**lhs) ||
1006+
contains_exterior_struct_lit(&**rhs)
1007+
}
1008+
ast::ExprUnary(_, ref x) |
1009+
ast::ExprCast(ref x, _) |
1010+
ast::ExprField(ref x, _, _) |
1011+
ast::ExprIndex(ref x, _) => {
1012+
// &X { y: 1 }, X { y: 1 }.y
1013+
contains_exterior_struct_lit(&**x)
1014+
}
1015+
1016+
ast::ExprMethodCall(_, _, ref exprs) => {
1017+
// X { y: 1 }.bar(...)
1018+
contains_exterior_struct_lit(&**exprs.get(0))
1019+
}
1020+
1021+
_ => false
1022+
}
1023+
}
9861024
}
9871025
}
9881026

@@ -992,16 +1030,16 @@ impl LintPass for UnnecessaryParens {
9921030
}
9931031

9941032
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
995-
let (value, msg) = match e.node {
996-
ast::ExprIf(cond, _, _) => (cond, "`if` condition"),
997-
ast::ExprWhile(cond, _) => (cond, "`while` condition"),
998-
ast::ExprMatch(head, _) => (head, "`match` head expression"),
999-
ast::ExprRet(Some(value)) => (value, "`return` value"),
1000-
ast::ExprAssign(_, value) => (value, "assigned value"),
1001-
ast::ExprAssignOp(_, _, value) => (value, "assigned value"),
1033+
let (value, msg, struct_lit_needs_parens) = match e.node {
1034+
ast::ExprIf(cond, _, _) => (cond, "`if` condition", true),
1035+
ast::ExprWhile(cond, _) => (cond, "`while` condition", true),
1036+
ast::ExprMatch(head, _) => (head, "`match` head expression", true),
1037+
ast::ExprRet(Some(value)) => (value, "`return` value", false),
1038+
ast::ExprAssign(_, value) => (value, "assigned value", false),
1039+
ast::ExprAssignOp(_, _, value) => (value, "assigned value", false),
10021040
_ => return
10031041
};
1004-
self.check_unnecessary_parens_core(cx, &*value, msg);
1042+
self.check_unnecessary_parens_core(cx, &*value, msg, struct_lit_needs_parens);
10051043
}
10061044

10071045
fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
@@ -1015,7 +1053,7 @@ impl LintPass for UnnecessaryParens {
10151053
},
10161054
_ => return
10171055
};
1018-
self.check_unnecessary_parens_core(cx, &*value, msg);
1056+
self.check_unnecessary_parens_core(cx, &*value, msg, false);
10191057
}
10201058
}
10211059

branches/try2/src/test/compile-fail/lint-unnecessary-parens.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,41 @@
1010

1111
#![deny(unnecessary_parens)]
1212

13+
#[deriving(Eq, PartialEq)]
14+
struct X { y: bool }
15+
impl X {
16+
fn foo(&self) -> bool { self.y }
17+
}
18+
1319
fn foo() -> int {
1420
return (1); //~ ERROR unnecessary parentheses around `return` value
1521
}
22+
fn bar() -> X {
23+
return (X { y: true }); //~ ERROR unnecessary parentheses around `return` value
24+
}
1625

1726
fn main() {
1827
foo();
28+
bar();
1929

2030
if (true) {} //~ ERROR unnecessary parentheses around `if` condition
2131
while (true) {} //~ ERROR unnecessary parentheses around `while` condition
2232
match (true) { //~ ERROR unnecessary parentheses around `match` head expression
2333
_ => {}
2434
}
35+
let v = X { y: false };
36+
// struct lits needs parens, so these shouldn't warn.
37+
if (v == X { y: true }) {}
38+
if (X { y: true } == v) {}
39+
if (X { y: false }.y) {}
40+
41+
while (X { y: false }.foo()) {}
42+
while (true | X { y: false }.y) {}
43+
44+
match (X { y: false }) {
45+
_ => {}
46+
}
47+
2548
let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
2649
_a = (0); //~ ERROR unnecessary parentheses around assigned value
2750
_a += (1); //~ ERROR unnecessary parentheses around assigned value

branches/try2/src/test/run-pass/issue-11677.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)