From 99ecf4e2c94a72a68294111ecdf6a82c150c378a Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Tue, 27 Oct 2015 13:41:55 +0000 Subject: [PATCH] libsyntax: improve error message when a statement is prefixed with a match keyword --- src/libsyntax/parse/parser.rs | 8 +++++++- src/test/parse-fail/match-refactor-to-expr.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/test/parse-fail/match-refactor-to-expr.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6cee7b86a61..d1fc8ac2002 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2941,9 +2941,15 @@ impl<'a> Parser<'a> { } fn parse_match_expr(&mut self) -> PResult> { + 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 = Vec::new(); while self.token != token::CloseDelim(token::Brace) { arms.push(try!(self.parse_arm_nopanic())); diff --git a/src/test/parse-fail/match-refactor-to-expr.rs b/src/test/parse-fail/match-refactor-to-expr.rs new file mode 100644 index 00000000000..e85fb3c9dd5 --- /dev/null +++ b/src/test/parse-fail/match-refactor-to-expr.rs @@ -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 or the MIT license +// , 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) +}