Skip to content

Commit 2af9aab

Browse files
committed
Auto merge of #29441 - Ryman:match_refactor_msg, r=alexcrichton
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
2 parents 3896a00 + 99ecf4e commit 2af9aab

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/libsyntax/parse/parser.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2941,9 +2941,15 @@ impl<'a> Parser<'a> {
29412941
}
29422942

29432943
fn parse_match_expr(&mut self) -> PResult<P<Expr>> {
2944+
let match_span = self.last_span;
29442945
let lo = self.last_span.lo;
29452946
let discriminant = try!(self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL));
2946-
try!(self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace)));
2947+
if let Err(e) = self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace)) {
2948+
if self.token == token::Token::Semi {
2949+
self.span_note(match_span, "did you mean to remove this `match` keyword?");
2950+
}
2951+
return Err(e)
2952+
}
29472953
let mut arms: Vec<Arm> = Vec::new();
29482954
while self.token != token::CloseDelim(token::Brace) {
29492955
arms.push(try!(self.parse_arm_nopanic()));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
fn main() {
12+
let foo =
13+
match //~ NOTE did you mean to remove this `match` keyword?
14+
Some(4).unwrap_or_else(5)
15+
; //~ ERROR expected one of `.`, `{`, or an operator, found `;`
16+
17+
println!("{}", foo)
18+
}

0 commit comments

Comments
 (0)