Skip to content

Commit 54a1d2b

Browse files
Rollup merge of rust-lang#54181 - vi:hint_and_or, r=estebank
Suggest && and || instead of 'and' and 'or' Resolves rust-lang#54109. Note: competing pull reqeust: rust-lang#54179 r? @csmoe
2 parents 3466d80 + bc63a4a commit 54a1d2b

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,22 @@ impl<'a> Parser<'a> {
732732
format!("expected {} here", expect)))
733733
};
734734
let mut err = self.fatal(&msg_exp);
735+
if self.token.is_ident_named("and") {
736+
err.span_suggestion_short_with_applicability(
737+
self.span,
738+
"use `&&` instead of `and` for the boolean operator",
739+
"&&".to_string(),
740+
Applicability::MaybeIncorrect,
741+
);
742+
}
743+
if self.token.is_ident_named("or") {
744+
err.span_suggestion_short_with_applicability(
745+
self.span,
746+
"use `||` instead of `or` for the boolean operator",
747+
"||".to_string(),
748+
Applicability::MaybeIncorrect,
749+
);
750+
}
735751
let sp = if self.token == token::Token::Eof {
736752
// This is EOF, don't want to point at the following char, but rather the last token
737753
self.prev_span
@@ -4751,6 +4767,23 @@ impl<'a> Parser<'a> {
47514767
e.span_label(sp, "expected `{`");
47524768
}
47534769

4770+
if self.token.is_ident_named("and") {
4771+
e.span_suggestion_short_with_applicability(
4772+
self.span,
4773+
"use `&&` instead of `and` for the boolean operator",
4774+
"&&".to_string(),
4775+
Applicability::MaybeIncorrect,
4776+
);
4777+
}
4778+
if self.token.is_ident_named("or") {
4779+
e.span_suggestion_short_with_applicability(
4780+
self.span,
4781+
"use `||` instead of `or` for the boolean operator",
4782+
"||".to_string(),
4783+
Applicability::MaybeIncorrect,
4784+
);
4785+
}
4786+
47544787
// Check to see if the user has written something like
47554788
//
47564789
// if (cond)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2018 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 test_and() {
12+
let a = true;
13+
let b = false;
14+
if a and b {
15+
//~^ ERROR expected `{`, found `and`
16+
println!("both");
17+
}
18+
}
19+
20+
fn test_or() {
21+
let a = true;
22+
let b = false;
23+
if a or b {
24+
//~^ ERROR expected `{`, found `or`
25+
println!("both");
26+
}
27+
}
28+
29+
fn test_and_par() {
30+
let a = true;
31+
let b = false;
32+
if (a and b) {
33+
//~^ ERROR expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and`
34+
println!("both");
35+
}
36+
}
37+
38+
fn test_or_par() {
39+
let a = true;
40+
let b = false;
41+
if (a or b) {
42+
//~^ ERROR expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `or`
43+
println!("both");
44+
}
45+
}
46+
47+
fn test_while_and() {
48+
let a = true;
49+
let b = false;
50+
while a and b {
51+
//~^ ERROR expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `and`
52+
println!("both");
53+
}
54+
}
55+
56+
fn test_while_or() {
57+
let a = true;
58+
let b = false;
59+
while a or b {
60+
//~^ ERROR expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `or`
61+
println!("both");
62+
}
63+
}
64+
65+
fn main() {
66+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: expected `{`, found `and`
2+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:14:10
3+
|
4+
LL | if a and b {
5+
| -- ^^^ help: use `&&` instead of `and` for the boolean operator
6+
| |
7+
| this `if` statement has a condition, but no block
8+
9+
error: expected `{`, found `or`
10+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:23:10
11+
|
12+
LL | if a or b {
13+
| -- ^^ help: use `||` instead of `or` for the boolean operator
14+
| |
15+
| this `if` statement has a condition, but no block
16+
17+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and`
18+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:32:11
19+
|
20+
LL | if (a and b) {
21+
| ^^^
22+
| |
23+
| expected one of 8 possible tokens here
24+
| help: use `&&` instead of `and` for the boolean operator
25+
26+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `or`
27+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:41:11
28+
|
29+
LL | if (a or b) {
30+
| ^^
31+
| |
32+
| expected one of 8 possible tokens here
33+
| help: use `||` instead of `or` for the boolean operator
34+
35+
error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `and`
36+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:50:13
37+
|
38+
LL | while a and b {
39+
| ^^^
40+
| |
41+
| expected one of `!`, `.`, `::`, `?`, `{`, or an operator here
42+
| help: use `&&` instead of `and` for the boolean operator
43+
44+
error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `or`
45+
--> $DIR/issue-54109-and_instead_of_ampersands.rs:59:13
46+
|
47+
LL | while a or b {
48+
| ^^
49+
| |
50+
| expected one of `!`, `.`, `::`, `?`, `{`, or an operator here
51+
| help: use `||` instead of `or` for the boolean operator
52+
53+
error: aborting due to 6 previous errors
54+

0 commit comments

Comments
 (0)