Skip to content

Commit

Permalink
Auto merge of rust-lang#29441 - Ryman:match_refactor_msg, r=alexcrichton
Browse files Browse the repository at this point in the history
This helps for the case where a match, such as below:
```rust
let foo = match foo {
    Some(x) => x,
    None => 0
};
```
gets refactored to no longer need the match, but the match keyword has been left accidentally: 

```rust
let foo = match foo.unwrap_or(0);
```

This can be hard to spot as the expression grows more complex.

r? @alexcrichton
  • Loading branch information
bors committed Oct 29, 2015
2 parents 3896a00 + 99ecf4e commit 2af9aab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2941,9 +2941,15 @@ impl<'a> Parser<'a> {
}

fn parse_match_expr(&mut self) -> PResult<P<Expr>> {
let match_span = self.last_span;
let lo = self.last_span.lo;
let discriminant = try!(self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL));
try!(self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace)));
if let Err(e) = self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace)) {
if self.token == token::Token::Semi {
self.span_note(match_span, "did you mean to remove this `match` keyword?");
}
return Err(e)
}
let mut arms: Vec<Arm> = Vec::new();
while self.token != token::CloseDelim(token::Brace) {
arms.push(try!(self.parse_arm_nopanic()));
Expand Down
18 changes: 18 additions & 0 deletions src/test/parse-fail/match-refactor-to-expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let foo =
match //~ NOTE did you mean to remove this `match` keyword?
Some(4).unwrap_or_else(5)
; //~ ERROR expected one of `.`, `{`, or an operator, found `;`

println!("{}", foo)
}

0 comments on commit 2af9aab

Please sign in to comment.