Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest && and || instead of 'and' and 'or' #54181

Merged
merged 5 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ impl<'a> Parser<'a> {
format!("expected {} here", expect)))
};
let mut err = self.fatal(&msg_exp);
if self.token.is_ident_named("and") {
err.help("Use `&&` instead of `and` for the boolean operator");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the following?

err.span_suggestion_with_applicability(
    self.span,
    "use `&&` instead of `and` for the boolean operator",
    "&&".to_string(),
    Applicability::MaybeIncorrect,
);

This way the output will be presented inline and third party tools will be able to apply the suggestion in their UIs.

Same change in all four cases.

}
if self.token.is_ident_named("or") {
err.help("Use `||` instead of `or` for the boolean operator");
}
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
Expand Down Expand Up @@ -4751,6 +4757,13 @@ impl<'a> Parser<'a> {
e.span_label(sp, "expected `{`");
}

if self.token.is_ident_named("and") {
e.help("Use `&&` instead of `and` for the boolean operator");
}
if self.token.is_ident_named("or") {
e.help("Use `||` instead of `or` for the boolean operator");
}

// Check to see if the user has written something like
//
// if (cond)
Expand Down
48 changes: 48 additions & 0 deletions src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 <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 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 main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
error: expected `{`, found `and`
--> $DIR/issue-54109-and_instead_of_ampersands.rs:14:10
|
LL | if a and b {
| -- ^^^
| |
| this `if` statement has a condition, but no block
|
= help: Use `&&` instead of `and` for the boolean operator

error: expected `{`, found `or`
--> $DIR/issue-54109-and_instead_of_ampersands.rs:23:10
|
LL | if a or b {
| -- ^^
| |
| this `if` statement has a condition, but no block
|
= 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: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: aborting due to 4 previous errors