about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincenzo Palazzo <vincenzopalazzodev@gmail.com>2022-08-20 20:47:31 +0000
committerVincenzo Palazzo <vincenzopalazzodev@gmail.com>2022-08-22 22:53:40 +0000
commit69715c903312c3e734bfd9098a5e8773f2e19d01 (patch)
tree0ee1869b86dad60ecb23c2693c57454af271c86c
parentdd01122b5c62a04e64b4109c5576eeea9ae4145b (diff)
downloadrust-69715c903312c3e734bfd9098a5e8773f2e19d01.tar.gz
rust-69715c903312c3e734bfd9098a5e8773f2e19d01.zip
sugg: suggest the usage of boolean value when there is a typo in the keyword
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs27
-rw-r--r--src/test/ui/suggestions/bool_typo_err_suggest.rs12
-rw-r--r--src/test/ui/suggestions/bool_typo_err_suggest.stderr25
3 files changed, 59 insertions, 5 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index 4175527da47..9bb6e464580 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -250,13 +250,30 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
                 .map_or_else(String::new, |res| format!("{} ", res.descr()));
                 (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 {
                 msg: format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
-                fallback_label: if path_str == "async" && expected.starts_with("struct") {
-                    "`async` blocks are only allowed in Rust 2018 or later".to_string()
-                } else {
-                    format!("not found in {mod_str}")
-                },
+                fallback_label,
                 span: item_span,
                 span_label: None,
                 could_be_expr: false,
diff --git a/src/test/ui/suggestions/bool_typo_err_suggest.rs b/src/test/ui/suggestions/bool_typo_err_suggest.rs
new file mode 100644
index 00000000000..deab0fb05b7
--- /dev/null
+++ b/src/test/ui/suggestions/bool_typo_err_suggest.rs
@@ -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
+}
diff --git a/src/test/ui/suggestions/bool_typo_err_suggest.stderr b/src/test/ui/suggestions/bool_typo_err_suggest.stderr
new file mode 100644
index 00000000000..52bde07ca07
--- /dev/null
+++ b/src/test/ui/suggestions/bool_typo_err_suggest.stderr
@@ -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`.