-
Notifications
You must be signed in to change notification settings - Fork 29
bevy_lint --fix
#505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bevy_lint --fix
#505
Conversation
We don't use this, as all of our flags are booleans, not options with values. If that were to ever change in the future, I'd re-enable this feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this is such a big win!
// `anstream` automatically removes ANSI escape codes if the terminal does not support it. This | ||
// text is formatted to look like `bevy -h`, `cargo clippy --help`, and `cargo check -h`. Keep | ||
// the printed text no wider than 80 characters, so it fits on most terminals without wrapping. | ||
anstream::println!( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this looks kinda scary ^^ I think it will be completely fine. We probably won't need to edit it often.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that's my hope. If it becomes unbearable, we'll switch to clap
and call it a day!
This PR adds support for automatically fixing any machine-applicable suggestions
bevy_lint
emits with the--fix
flag. In the process, it also implements CLI argument parsing and a custom--help
screen.How does
--fix
work?It's surprisingly simple! Cargo supports automatically fixing builtin lints by running
cargo fix
. We can makecargo fix
work withbevy_lint
by settingRUSTC_WORKSPACE_WRAPPER=bevy_lint_driver
, which is the same thing we do forcargo check
.So the end result is that, when
--fix
is passed, we runcargo fix
instead ofcargo check
. Nothing else changes :)Why
pico_args
?The biggest thing I wanted to avoid was
clap
. It's a wonderful crate, but using it could significantly hurtbevy_lint
's fast compile times. It's overkill for the simple stuff that the linter needs!When looking for an alternative, I consulted blessed.rs and found
pico_args
. It's a small, zero-dependency library for quick and simple CLI parsing, and was exactly what I was looking for!pico_args
is essentially stable, with no additional features planned. The author appears trustworthy, having contributed to quite a few open-source Rust projects in the past, although they look to currently be inactive. Given howpico_args
is unlikely to change in the future, this shouldn't be a problem. Should we run into an issue, we can switch to another library (likeclap
if need be).Why a custom help screen?
Before,
bevy_lint --help
just printedcargo check --help
.cargo check
doesn't support--fix
, however, so we need to make our own help screen instead.The approach I used to make the help screen is simple and fast, but somewhat rigid. Instead of computing the help screen dynamically, like
clap
, we just embed a string literal with the contents. I also useanstyle
to make the help screen colorful, andanstream
so strip out the ANSI color codes for terminals and streams that don't support them:The main benefits to this approach is that it:
There are a few drawbacks, however. This approach:
--help
text{LITERAL}
template blocksI think the pros outweigh the cons here. My bet is that the
--help
screen will be rarely edited, so we won't run into the issues as much as other projects might. (For example, I would never do this for the Bevy CLI. Its CLI interface changes far too much!)Testing
First, install this branch's version of the linter:
Next, setup a new project with the follow in
main.rs
:commands.spawn(())
will cause thebevy::insert_unit_bundle
lint to be upset. You can verify it by runningbevy_lint
:Now, fix the lint by running
bevy_lint --fix
.commands.spawn(())
should now be replaced withcommands.spawn_empty()
!