Skip to content

Commit 55cfe1f

Browse files
authored
Rollup merge of rust-lang#100817 - vincenzopalazzo:macros/bool_spelling_sugg, r=davidtwco
sugg: suggest the usage of boolean value when there is a typo in the keyword Fixes rust-lang#100686 This adds a new suggestion when there is a well-known typo With the following program ```rust fn main() { let x = True; } ``` Now we have the following suggestion ``` error[E0425]: cannot find value `True` in this scope --> test.rs:2:13 | 2 | let x = True; | ^^^^ not found in this scope | help: you may want to use a bool value instead | 2 | let x = true; | ~~~~ error: aborting due to previous error ``` Signed-off-by: Vincenzo Palazzo <[email protected]>
2 parents 10b1822 + 69715c9 commit 55cfe1f

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,30 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
250250
.map_or_else(String::new, |res| format!("{} ", res.descr()));
251251
(mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)), None)
252252
};
253+
254+
let (fallback_label, suggestion) = if path_str == "async"
255+
&& expected.starts_with("struct")
256+
{
257+
("`async` blocks are only allowed in Rust 2018 or later".to_string(), suggestion)
258+
} else {
259+
// check if we are in situation of typo like `True` instead of `true`.
260+
let override_suggestion =
261+
if ["true", "false"].contains(&item_str.to_string().to_lowercase().as_str()) {
262+
let item_typo = item_str.to_string().to_lowercase();
263+
Some((
264+
item_span,
265+
"you may want to use a bool value instead",
266+
format!("{}", item_typo),
267+
))
268+
} else {
269+
suggestion
270+
};
271+
(format!("not found in {mod_str}"), override_suggestion)
272+
};
273+
253274
BaseError {
254275
msg: format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
255-
fallback_label: if path_str == "async" && expected.starts_with("struct") {
256-
"`async` blocks are only allowed in Rust 2018 or later".to_string()
257-
} else {
258-
format!("not found in {mod_str}")
259-
},
276+
fallback_label,
260277
span: item_span,
261278
span_label: None,
262279
could_be_expr: false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Suggest the boolean value instead of emit a generic error that the value
2+
// True is not in the scope.
3+
4+
fn main() {
5+
let x = True;
6+
//~^ ERROR cannot find value `True` in this scope
7+
//~| HELP you may want to use a bool value instead
8+
9+
let y = False;
10+
//~^ ERROR cannot find value `False` in this scope
11+
//~| HELP you may want to use a bool value instead
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0425]: cannot find value `True` in this scope
2+
--> $DIR/bool_typo_err_suggest.rs:5:13
3+
|
4+
LL | let x = True;
5+
| ^^^^ not found in this scope
6+
|
7+
help: you may want to use a bool value instead
8+
|
9+
LL | let x = true;
10+
| ~~~~
11+
12+
error[E0425]: cannot find value `False` in this scope
13+
--> $DIR/bool_typo_err_suggest.rs:9:13
14+
|
15+
LL | let y = False;
16+
| ^^^^^ not found in this scope
17+
|
18+
help: you may want to use a bool value instead
19+
|
20+
LL | let y = false;
21+
| ~~~~~
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)