From ee6f18ef595df21cfe167834f59c768985d74d4c Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 26 Nov 2022 22:23:27 +0100 Subject: [PATCH] make simple check of prinf function. With this commit we start to make some simple check when the name resolution fails, and we generate some helper message in case the name is a C name like in the case of the `printf` and suggest the correct rust method. Signed-off-by: Vincenzo Palazzo --- compiler/rustc_resolve/src/late/diagnostics.rs | 8 ++++++++ .../ui/suggestions/seggest_print_over_printf.rs | 9 +++++++++ .../suggestions/seggest_print_over_printf.stderr | 14 ++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/test/ui/suggestions/seggest_print_over_printf.rs create mode 100644 src/test/ui/suggestions/seggest_print_over_printf.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 9c95adc628bc6..5b9513ebc0da4 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -282,6 +282,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { "you may want to use a bool value instead", format!("{}", item_typo), )) + // FIXME(vicnenzopalazzo): make the check smarter, + // and maybe expand with levenshtein distance checks + } else if item_str.as_str() == "printf" { + Some(( + item_span, + "you may have meant to use the `print` macro", + "print!".to_owned(), + )) } else { suggestion }; diff --git a/src/test/ui/suggestions/seggest_print_over_printf.rs b/src/test/ui/suggestions/seggest_print_over_printf.rs new file mode 100644 index 0000000000000..25566cd7f2aeb --- /dev/null +++ b/src/test/ui/suggestions/seggest_print_over_printf.rs @@ -0,0 +1,9 @@ +// Suggest to a user to use the print macros +// instead to use the printf. + +fn main() { + let x = 4; + printf("%d", x); + //~^ ERROR cannot find function `printf` in this scope + //~| HELP you may have meant to use the `print` macro +} diff --git a/src/test/ui/suggestions/seggest_print_over_printf.stderr b/src/test/ui/suggestions/seggest_print_over_printf.stderr new file mode 100644 index 0000000000000..7b1ce047a9274 --- /dev/null +++ b/src/test/ui/suggestions/seggest_print_over_printf.stderr @@ -0,0 +1,14 @@ +error[E0425]: cannot find function `printf` in this scope + --> $DIR/seggest_print_over_printf.rs:6:5 + | +LL | printf("%d", x); + | ^^^^^^ not found in this scope + | +help: you may have meant to use the `print` macro + | +LL | print!("%d", x); + | ~~~~~~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0425`.