Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cbfa7d

Browse files
authoredApr 14, 2020
Rollup merge of rust-lang#70657 - lcnr:unused_delims_try, r=Centril
Allow `try`-blocks in places where an open delim is expected Closes rust-lang#70490 Closes rust-lang#56828 r? @Centril
2 parents bf06312 + 81a3cd7 commit 2cbfa7d

8 files changed

+89
-21
lines changed
 

‎src/librustc_lint/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ trait UnusedDelimLint {
384384
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
385385
followed_by_block
386386
&& match inner.kind {
387-
ast::ExprKind::Ret(_) | ast::ExprKind::Break(..) => true,
387+
ExprKind::Ret(_) | ExprKind::Break(..) => true,
388388
_ => parser::contains_exterior_struct_lit(&inner),
389389
}
390390
}

‎src/librustc_parse/parser/expr.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1846,11 +1846,9 @@ impl<'a> Parser<'a> {
18461846
}
18471847

18481848
fn is_try_block(&self) -> bool {
1849-
self.token.is_keyword(kw::Try) &&
1850-
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
1851-
self.token.uninterpolated_span().rust_2018() &&
1852-
// Prevent `while try {} {}`, `if try {} {} else {}`, etc.
1853-
!self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
1849+
self.token.is_keyword(kw::Try)
1850+
&& self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace))
1851+
&& self.token.uninterpolated_span().rust_2018()
18541852
}
18551853

18561854
/// Parses an `async move? {...}` expression.
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// run-pass
12
// compile-flags: --edition 2018
23

34
#![feature(try_blocks)]
45

56
fn main() {
6-
match try { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `try`
7+
match try { } {
8+
Err(()) => (),
9+
Ok(()) => (),
10+
}
711
}

‎src/test/ui/try-block/try-block-in-match.stderr

-10
This file was deleted.

‎src/test/ui/try-block/try-block-in-while.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
#![feature(try_blocks)]
44

55
fn main() {
6-
while try { false } {} //~ ERROR expected expression, found reserved keyword `try`
6+
while try { false } {}
7+
//~^ ERROR the trait bound `bool: std::ops::Try` is not satisfied
78
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: expected expression, found reserved keyword `try`
2-
--> $DIR/try-block-in-while.rs:6:11
1+
error[E0277]: the trait bound `bool: std::ops::Try` is not satisfied
2+
--> $DIR/try-block-in-while.rs:6:15
33
|
44
LL | while try { false } {}
5-
| ^^^ expected expression
5+
| ^^^^^^^^^ the trait `std::ops::Try` is not implemented for `bool`
6+
|
7+
= note: required by `std::ops::Try::from_ok`
68

79
error: aborting due to previous error
810

11+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// check-pass
2+
// compile-flags: --edition 2018
3+
4+
#![feature(try_blocks)]
5+
#![warn(unused_parens, unused_braces)]
6+
7+
fn consume<T>(_: Result<T, T>) -> T { todo!() }
8+
9+
fn main() {
10+
consume((try {}));
11+
//~^ WARN unnecessary parentheses
12+
13+
consume({ try {} });
14+
//~^ WARN unnecessary braces
15+
16+
match (try {}) {
17+
//~^ WARN unnecessary parentheses
18+
Ok(()) | Err(()) => (),
19+
}
20+
21+
if let Err(()) = (try {}) {}
22+
//~^ WARN unnecessary parentheses
23+
24+
match (try {}) {
25+
//~^ WARN unnecessary parentheses
26+
Ok(()) | Err(()) => (),
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
warning: unnecessary parentheses around function argument
2+
--> $DIR/try-block-unused-delims.rs:10:13
3+
|
4+
LL | consume((try {}));
5+
| ^^^^^^^^ help: remove these parentheses
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/try-block-unused-delims.rs:5:9
9+
|
10+
LL | #![warn(unused_parens, unused_braces)]
11+
| ^^^^^^^^^^^^^
12+
13+
warning: unnecessary braces around function argument
14+
--> $DIR/try-block-unused-delims.rs:13:13
15+
|
16+
LL | consume({ try {} });
17+
| ^^^^^^^^^^ help: remove these braces
18+
|
19+
note: the lint level is defined here
20+
--> $DIR/try-block-unused-delims.rs:5:24
21+
|
22+
LL | #![warn(unused_parens, unused_braces)]
23+
| ^^^^^^^^^^^^^
24+
25+
warning: unnecessary parentheses around `match` scrutinee expression
26+
--> $DIR/try-block-unused-delims.rs:16:11
27+
|
28+
LL | match (try {}) {
29+
| ^^^^^^^^ help: remove these parentheses
30+
31+
warning: unnecessary parentheses around `let` scrutinee expression
32+
--> $DIR/try-block-unused-delims.rs:21:22
33+
|
34+
LL | if let Err(()) = (try {}) {}
35+
| ^^^^^^^^ help: remove these parentheses
36+
37+
warning: unnecessary parentheses around `match` scrutinee expression
38+
--> $DIR/try-block-unused-delims.rs:24:11
39+
|
40+
LL | match (try {}) {
41+
| ^^^^^^^^ help: remove these parentheses
42+
43+
warning: 5 warnings emitted
44+

0 commit comments

Comments
 (0)
Please sign in to comment.