diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index f57fca2cfcf60..48e034b117f18 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -732,6 +732,22 @@ impl<'a> Parser<'a> { format!("expected {} here", expect))) }; let mut err = self.fatal(&msg_exp); + if self.token.is_ident_named("and") { + err.span_suggestion_short_with_applicability( + self.span, + "use `&&` instead of `and` for the boolean operator", + "&&".to_string(), + Applicability::MaybeIncorrect, + ); + } + if self.token.is_ident_named("or") { + err.span_suggestion_short_with_applicability( + self.span, + "use `||` instead of `or` for the boolean operator", + "||".to_string(), + Applicability::MaybeIncorrect, + ); + } let sp = if self.token == token::Token::Eof { // This is EOF, don't want to point at the following char, but rather the last token self.prev_span @@ -4751,6 +4767,23 @@ impl<'a> Parser<'a> { e.span_label(sp, "expected `{`"); } + if self.token.is_ident_named("and") { + e.span_suggestion_short_with_applicability( + self.span, + "use `&&` instead of `and` for the boolean operator", + "&&".to_string(), + Applicability::MaybeIncorrect, + ); + } + if self.token.is_ident_named("or") { + e.span_suggestion_short_with_applicability( + self.span, + "use `||` instead of `or` for the boolean operator", + "||".to_string(), + Applicability::MaybeIncorrect, + ); + } + // Check to see if the user has written something like // // if (cond) diff --git a/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.rs b/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.rs new file mode 100644 index 0000000000000..d053b11772cd0 --- /dev/null +++ b/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.rs @@ -0,0 +1,66 @@ +// Copyright 2018 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 test_and() { + let a = true; + let b = false; + if a and b { + //~^ ERROR expected `{`, found `and` + println!("both"); + } +} + +fn test_or() { + let a = true; + let b = false; + if a or b { + //~^ ERROR expected `{`, found `or` + println!("both"); + } +} + +fn test_and_par() { + let a = true; + let b = false; + if (a and b) { + //~^ ERROR expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and` + println!("both"); + } +} + +fn test_or_par() { + let a = true; + let b = false; + if (a or b) { + //~^ ERROR expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `or` + println!("both"); + } +} + +fn test_while_and() { + let a = true; + let b = false; + while a and b { + //~^ ERROR expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `and` + println!("both"); + } +} + +fn test_while_or() { + let a = true; + let b = false; + while a or b { + //~^ ERROR expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `or` + println!("both"); + } +} + +fn main() { +} diff --git a/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr b/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr new file mode 100644 index 0000000000000..22845775aed13 --- /dev/null +++ b/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr @@ -0,0 +1,54 @@ +error: expected `{`, found `and` + --> $DIR/issue-54109-and_instead_of_ampersands.rs:14:10 + | +LL | if a and b { + | -- ^^^ help: use `&&` instead of `and` for the boolean operator + | | + | this `if` statement has a condition, but no block + +error: expected `{`, found `or` + --> $DIR/issue-54109-and_instead_of_ampersands.rs:23:10 + | +LL | if a or b { + | -- ^^ help: use `||` instead of `or` for the boolean operator + | | + | this `if` statement has a condition, but no block + +error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and` + --> $DIR/issue-54109-and_instead_of_ampersands.rs:32:11 + | +LL | if (a and b) { + | ^^^ + | | + | expected one of 8 possible tokens here + | help: use `&&` instead of `and` for the boolean operator + +error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `or` + --> $DIR/issue-54109-and_instead_of_ampersands.rs:41:11 + | +LL | if (a or b) { + | ^^ + | | + | expected one of 8 possible tokens here + | help: use `||` instead of `or` for the boolean operator + +error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `and` + --> $DIR/issue-54109-and_instead_of_ampersands.rs:50:13 + | +LL | while a and b { + | ^^^ + | | + | expected one of `!`, `.`, `::`, `?`, `{`, or an operator here + | help: use `&&` instead of `and` for the boolean operator + +error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `or` + --> $DIR/issue-54109-and_instead_of_ampersands.rs:59:13 + | +LL | while a or b { + | ^^ + | | + | expected one of `!`, `.`, `::`, `?`, `{`, or an operator here + | help: use `||` instead of `or` for the boolean operator + +error: aborting due to 6 previous errors +