Skip to content

Commit 412f05c

Browse files
authored
Rollup merge of #104954 - vincenzopalazzo:macros/prinf, r=estebank
make simple check of prinf function Fixes #92898 With this commit we start to make some simple check when the name resolution fails, and we generate some helper messages in case the name is a C name like in the case of the `printf` and suggest the correct rust method. `@rustbot` r? `@pnkfelix` Signed-off-by: Vincenzo Palazzo <[email protected]>
2 parents 60d1360 + ee6f18e commit 412f05c

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)