Skip to content

Commit ee6f18e

Browse files
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>
1 parent c3a1c02 commit ee6f18e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+8
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
282282
"you may want to use a bool value instead",
283283
format!("{}", item_typo),
284284
))
285+
// FIXME(vicnenzopalazzo): make the check smarter,
286+
// and maybe expand with levenshtein distance checks
287+
} else if item_str.as_str() == "printf" {
288+
Some((
289+
item_span,
290+
"you may have meant to use the `print` macro",
291+
"print!".to_owned(),
292+
))
285293
} else {
286294
suggestion
287295
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Suggest to a user to use the print macros
2+
// instead to use the printf.
3+
4+
fn main() {
5+
let x = 4;
6+
printf("%d", x);
7+
//~^ ERROR cannot find function `printf` in this scope
8+
//~| HELP you may have meant to use the `print` macro
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0425]: cannot find function `printf` in this scope
2+
--> $DIR/seggest_print_over_printf.rs:6:5
3+
|
4+
LL | printf("%d", x);
5+
| ^^^^^^ not found in this scope
6+
|
7+
help: you may have meant to use the `print` macro
8+
|
9+
LL | print!("%d", x);
10+
| ~~~~~~
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)