Improve parsing errors and suggestions for bad if statements
This commit is contained in:
parent
3bdec3c8ab
commit
d1ba2d25d4
21 changed files with 325 additions and 143 deletions
|
@ -432,10 +432,23 @@ impl<'a> Parser<'a> {
|
|||
//
|
||||
// which is valid in other languages, but not Rust.
|
||||
match self.parse_stmt_without_recovery(false, ForceCollect::No) {
|
||||
// If the next token is an open brace (e.g., `if a b {`), the place-
|
||||
// inside-a-block suggestion would be more likely wrong than right.
|
||||
// If the next token is an open brace, e.g., we have:
|
||||
//
|
||||
// if expr other_expr {
|
||||
// ^ ^ ^- lookahead(1) is a brace
|
||||
// | |- current token is not "else"
|
||||
// |- (statement we just parsed)
|
||||
//
|
||||
// the place-inside-a-block suggestion would be more likely wrong than right.
|
||||
//
|
||||
// FIXME(compiler-errors): this should probably parse an arbitrary expr and not
|
||||
// just lookahead one token, so we can see if there's a brace after _that_,
|
||||
// since we want to protect against:
|
||||
// `if 1 1 + 1 {` being suggested as `if { 1 } 1 + 1 {`
|
||||
// + +
|
||||
Ok(Some(_))
|
||||
if self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Brace))
|
||||
if (!self.token.is_keyword(kw::Else)
|
||||
&& self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Brace)))
|
||||
|| do_not_suggest_help => {}
|
||||
// Do not suggest `if foo println!("") {;}` (as would be seen in test for #46836).
|
||||
Ok(Some(Stmt { kind: StmtKind::Empty, .. })) => {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue