Rollup merge of #100817 - vincenzopalazzo:macros/bool_spelling_sugg, r=davidtwco
sugg: suggest the usage of boolean value when there is a typo in the keyword Fixes https://github.com/rust-lang/rust/issues/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 <vincenzopalazzodev@gmail.com>
This commit is contained in:
commit
54744601bf
3 changed files with 59 additions and 5 deletions
|
@ -250,13 +250,30 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||||
.map_or_else(String::new, |res| format!("{} ", res.descr()));
|
.map_or_else(String::new, |res| format!("{} ", res.descr()));
|
||||||
(mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)), None)
|
(mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)), None)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (fallback_label, suggestion) = if path_str == "async"
|
||||||
|
&& expected.starts_with("struct")
|
||||||
|
{
|
||||||
|
("`async` blocks are only allowed in Rust 2018 or later".to_string(), suggestion)
|
||||||
|
} else {
|
||||||
|
// check if we are in situation of typo like `True` instead of `true`.
|
||||||
|
let override_suggestion =
|
||||||
|
if ["true", "false"].contains(&item_str.to_string().to_lowercase().as_str()) {
|
||||||
|
let item_typo = item_str.to_string().to_lowercase();
|
||||||
|
Some((
|
||||||
|
item_span,
|
||||||
|
"you may want to use a bool value instead",
|
||||||
|
format!("{}", item_typo),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
suggestion
|
||||||
|
};
|
||||||
|
(format!("not found in {mod_str}"), override_suggestion)
|
||||||
|
};
|
||||||
|
|
||||||
BaseError {
|
BaseError {
|
||||||
msg: format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
|
msg: format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
|
||||||
fallback_label: if path_str == "async" && expected.starts_with("struct") {
|
fallback_label,
|
||||||
"`async` blocks are only allowed in Rust 2018 or later".to_string()
|
|
||||||
} else {
|
|
||||||
format!("not found in {mod_str}")
|
|
||||||
},
|
|
||||||
span: item_span,
|
span: item_span,
|
||||||
span_label: None,
|
span_label: None,
|
||||||
could_be_expr: false,
|
could_be_expr: false,
|
||||||
|
|
12
src/test/ui/suggestions/bool_typo_err_suggest.rs
Normal file
12
src/test/ui/suggestions/bool_typo_err_suggest.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
// Suggest the boolean value instead of emit a generic error that the value
|
||||||
|
// True is not in the scope.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = True;
|
||||||
|
//~^ ERROR cannot find value `True` in this scope
|
||||||
|
//~| HELP you may want to use a bool value instead
|
||||||
|
|
||||||
|
let y = False;
|
||||||
|
//~^ ERROR cannot find value `False` in this scope
|
||||||
|
//~| HELP you may want to use a bool value instead
|
||||||
|
}
|
25
src/test/ui/suggestions/bool_typo_err_suggest.stderr
Normal file
25
src/test/ui/suggestions/bool_typo_err_suggest.stderr
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
error[E0425]: cannot find value `True` in this scope
|
||||||
|
--> $DIR/bool_typo_err_suggest.rs:5:13
|
||||||
|
|
|
||||||
|
LL | let x = True;
|
||||||
|
| ^^^^ not found in this scope
|
||||||
|
|
|
||||||
|
help: you may want to use a bool value instead
|
||||||
|
|
|
||||||
|
LL | let x = true;
|
||||||
|
| ~~~~
|
||||||
|
|
||||||
|
error[E0425]: cannot find value `False` in this scope
|
||||||
|
--> $DIR/bool_typo_err_suggest.rs:9:13
|
||||||
|
|
|
||||||
|
LL | let y = False;
|
||||||
|
| ^^^^^ not found in this scope
|
||||||
|
|
|
||||||
|
help: you may want to use a bool value instead
|
||||||
|
|
|
||||||
|
LL | let y = false;
|
||||||
|
| ~~~~~
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0425`.
|
Loading…
Add table
Add a link
Reference in a new issue