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 <vincenzopalazzodev@gmail.com>
This commit is contained in:
Vincenzo Palazzo 2022-11-26 22:23:27 +01:00
parent c3a1c023c0
commit ee6f18ef59
No known key found for this signature in database
GPG key ID: 8B6DC2B870B80D5F
3 changed files with 31 additions and 0 deletions

View file

@ -282,6 +282,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
"you may want to use a bool value instead", "you may want to use a bool value instead",
format!("{}", item_typo), 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 { } else {
suggestion suggestion
}; };

View file

@ -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
}

View file

@ -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`.