EDIT: Turns out this already exists.
If you want it, you can use cargo fix
, or cargo clippy --fix
.
Thanks to people in the replies for letting me know about this!
I'll leave the idea here for archival purposes.
My idea is to make the compiler be able to self-correct in certain circumstances.
Let's say you're working on a Hello World program, by example.
fn main() {
let hello_world = "Hello world!";
priintln!(hello_world);
This will return an error.
error: format argument must be a string literal
--> test.rs:3:14
|
3 | println!(hello_world);
| ^^^^^^^^^^^
|
help: you might be missing a string literal to format with
|
3 | println!("{}", hello_world);
| +++++
But the compiler already knows the solution and gives it to the user. So, why not expand on this and have the compiler self-correct?
error: format argument must be a string literal
--> test.rs:3:14
|
3 | println!(hello_world);
| ^^^^^^^^^^^
|
help: you may be missing a string literal to format with
|
3 | println!("{}", hello_world);
| +++++
autocorrect: would you like to autocorrect this error? (y/n)
You could either type in 'y' or 'n', and if you type in 'y' it will self-correct. There could also be a compiler flag "--auto-correct" to automatically correct these small errors for you without having to type in y/n.
This could be useful in large projects where compilation takes an extremely long time. You don't want to recompile the whole thing because someone missed a string literal or a symbol. It could also help save the developers a small amount of time - they don't have to go back, find the file, add the missing thing and then recompile.
I don't know in practice how well this would work, so I'm posting this as a concept/pre-rfc here to get feedback.