Skip to content

Commit acc44e4

Browse files
committed
Use span_suggestion_with_applicability for "and/or" hinter
Advised by @estebank.
1 parent 888b8c9 commit acc44e4

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,20 @@ impl<'a> Parser<'a> {
733733
};
734734
let mut err = self.fatal(&msg_exp);
735735
if self.token.is_ident_named("and") {
736-
err.help("Use `&&` instead of `and` for the boolean operator");
736+
err.span_suggestion_with_applicability(
737+
self.span,
738+
"use `&&` instead of `and` for the boolean operator",
739+
"&&".to_string(),
740+
Applicability::MaybeIncorrect,
741+
);
737742
}
738743
if self.token.is_ident_named("or") {
739-
err.help("Use `||` instead of `or` for the boolean operator");
744+
err.span_suggestion_with_applicability(
745+
self.span,
746+
"use `||` instead of `or` for the boolean operator",
747+
"||".to_string(),
748+
Applicability::MaybeIncorrect,
749+
);
740750
}
741751
let sp = if self.token == token::Token::Eof {
742752
// This is EOF, don't want to point at the following char, but rather the last token
@@ -4758,10 +4768,20 @@ impl<'a> Parser<'a> {
47584768
}
47594769

47604770
if self.token.is_ident_named("and") {
4761-
e.help("Use `&&` instead of `and` for the boolean operator");
4771+
e.span_suggestion_with_applicability(
4772+
self.span,
4773+
"use `&&` instead of `and` for the boolean operator",
4774+
"&&".to_string(),
4775+
Applicability::MaybeIncorrect,
4776+
);
47624777
}
47634778
if self.token.is_ident_named("or") {
4764-
e.help("Use `||` instead of `or` for the boolean operator");
4779+
e.span_suggestion_with_applicability(
4780+
self.span,
4781+
"use `||` instead of `or` for the boolean operator",
4782+
"||".to_string(),
4783+
Applicability::MaybeIncorrect,
4784+
);
47654785
}
47664786

47674787
// Check to see if the user has written something like

src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,34 @@ error: expected `{`, found `and`
22
--> $DIR/issue-54109-and_instead_of_ampersands.rs:14:10
33
|
44
LL | if a and b {
5-
| -- ^^^
5+
| -- ^^^ help: use `&&` instead of `and` for the boolean operator: `&&`
66
| |
77
| this `if` statement has a condition, but no block
8-
|
9-
= help: Use `&&` instead of `and` for the boolean operator
108

119
error: expected `{`, found `or`
1210
--> $DIR/issue-54109-and_instead_of_ampersands.rs:23:10
1311
|
1412
LL | if a or b {
15-
| -- ^^
13+
| -- ^^ help: use `||` instead of `or` for the boolean operator: `||`
1614
| |
1715
| this `if` statement has a condition, but no block
18-
|
19-
= help: Use `||` instead of `or` for the boolean operator
2016

2117
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and`
2218
--> $DIR/issue-54109-and_instead_of_ampersands.rs:32:11
2319
|
2420
LL | if (a and b) {
25-
| ^^^ expected one of 8 possible tokens here
26-
|
27-
= help: Use `&&` instead of `and` for the boolean operator
21+
| ^^^
22+
| |
23+
| expected one of 8 possible tokens here
24+
| help: use `&&` instead of `and` for the boolean operator: `&&`
2825

2926
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `or`
3027
--> $DIR/issue-54109-and_instead_of_ampersands.rs:41:11
3128
|
3229
LL | if (a or b) {
33-
| ^^ expected one of 8 possible tokens here
34-
|
35-
= help: Use `||` instead of `or` for the boolean operator
30+
| ^^
31+
| |
32+
| expected one of 8 possible tokens here
33+
| help: use `||` instead of `or` for the boolean operator: `||`
3634

3735
error: aborting due to 4 previous errors
38-

0 commit comments

Comments
 (0)