Skip to content
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

Add colored help to be consistent with Cargo #11495

Merged
merged 1 commit into from
Sep 25, 2023

Conversation

blyxyas
Copy link
Member

@blyxyas blyxyas commented Sep 13, 2023

On rust-lang/cargo#12578, a new colored help message format was introduced. This PR introduces the same styling from that cargo help message to our cargo clippy --help message.

More information is provided in the original PR, fixes #11482. The perfect reviewing process would be that the reviewer installs this branch and checks for themselves, but here are some screenshots, there are some more screenshots in the original issue.

image

(Note that the actual text may change in the actual commit, that screenshot is just to test the colors).
Also note that the color-print version should always be synced with Cargo's color-print version to avoid increasing build times in the rust-lang/rust repo.

changelog:Add colors to the cargo clippy --help output 🎉.

@rustbot
Copy link
Collaborator

rustbot commented Sep 13, 2023

r? @giraffate

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 13, 2023
src/driver.rs Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocker: This doesn't work when piping the output to a file:

LD_LIBRARY_PATH=$(rustc --print=sysroot)/lib ./target/debug/cargo-clippy --help > test

results in:

Checks a package to catch common mistakes and improve your Rust code.

�[32m�[1mUsage�[39m�[22m:
    �[36m�[1mcargo clippy�[39m�[22m �[36m[OPTIONS] [--] [<ARGS>...]�[39m

�[32m�[1mCommon options:�[39m�[22m
    �[36m�[1m--no-deps�[39m�[22m                Run Clippy only on the given crate, without linting the dependencies
    �[36m�[1m--fix�[39m�[22m                    Automatically apply lint suggestions. This flag implies �[36m--no-deps�[39m and �[36m--all-targets�[39m
    �[36m�[1m-h, --help�[39m�[22m               Print this message
    �[36m�[1m-V, --version�[39m�[22m            Print version info and exit
    �[36m�[1m--explain [LINT]�[39m�[22m           Print the documentation for a given lint

See all options with �[36m�[1mcargo check --help�[39m�[22m.

�[32m�[1mAllowing / Denying lints�[39m�[22m

To allow or deny a lint from the command line you can use �[36m�[1mcargo clippy --�[39m�[22m with:

    �[36m�[1m-W�[39m�[22m / �[36m�[1m--warn�[39m�[22m �[36m[LINT]�[39m       Set lint warnings
    �[36m�[1m-A�[39m�[22m / �[36m�[1m--allow�[39m�[22m �[36m[LINT]�[39m      Set lint allowed
    �[36m�[1m-D�[39m�[22m / �[36m�[1m--deny�[39m�[22m �[36m[LINT]�[39m       Set lint denied
    �[36m�[1m-F�[39m�[22m / �[36m�[1m--forbid�[39m�[22m �[36m[LINT]�[39m     Set lint forbidden

You can use tool lints to allow or deny lints from your code, e.g.:

    �[33m�[1m#[allow(clippy::needless_lifetimes)]�[39m�[22m

@blyxyas
Copy link
Member Author

blyxyas commented Sep 14, 2023

OK, this new approach fixes Philipp's comment but introduces kind of a lot of bloat. On the original PR, this bloat isn't there because clap handles a lot of minute things (and removing formatting when piping to a file is one of them, I think). But as we don't use clap, we have to manually check where output is going.

@flip1995
Copy link
Member

flip1995 commented Sep 14, 2023

But as we don't use clap, we have to manually check where output is going.

Should we use clap? Should we nominate this PR for discussion?

@blyxyas
Copy link
Member Author

blyxyas commented Sep 14, 2023

Adding clap as a dependency would add 55Kb, but it adds some QOL improvements and overall more readability. I think nominating this is the best option, as the pros and cons are pretty similar and subjective, so we'll have to know what the team thinks.

Also note that, if we use clap, the kinda clunky syntax of cargo clippy -- -W [LINT] would change to a slightly better cargo clippy -W [LINT]

@blyxyas blyxyas added the I-nominated Issue: Nominated to be discussed at the next Clippy meeting label Sep 14, 2023
@flip1995
Copy link
Member

the kinda clunky syntax of cargo clippy -- -W [LINT] would change to a slightly better cargo clippy -W [LINT]

Sadly the clunky syntax is probably there to stay for backwards compat...

@blyxyas
Copy link
Member Author

blyxyas commented Sep 14, 2023

There's probably a way to maintain this backwards compatibility while also providing a better solution.

@epage
Copy link

epage commented Sep 15, 2023

As an alternative to pulling in all of clap, you can use the anstream library that clap uses that wraps stdout/stderr with the ability to strip ansi escape codes when needed.

@blyxyas
Copy link
Member Author

blyxyas commented Sep 16, 2023

The current commit also checks if we're on a terminal and adjusts the output to that information. Pulling a dependency just to avoid an if check would not be the best decision (it would also hurt runtime performance)

@epage
Copy link

epage commented Sep 16, 2023

Help output performance isn't the biggest deal and the dependency helps with (1) only having one instance of the help to avoid subtle issues with them being out of sync and (2) it covers more situations (NOCOLOR, CLI_COLOR, legacy windows, etc).

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You found a bug in ignored_unit_patterns: Needs an external macro check.

Cargo.toml Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
@blyxyas
Copy link
Member Author

blyxyas commented Sep 20, 2023

Needs an external macro check.

The lint uses check_pat, and sadly we cannot use is_from_proc_macro with hir::Pats yet. I'll add it to the backlog for when the is_from_proc_macro update is merged.

src/driver.rs Outdated Show resolved Hide resolved
src/main.rs Outdated
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still some work to be done on the clippy-driver help message.

src/driver.rs Outdated Show resolved Hide resolved
src/driver.rs Outdated Show resolved Hide resolved
@flip1995
Copy link
Member

r? @flip1995

@rustbot rustbot assigned flip1995 and unassigned giraffate Sep 21, 2023
@flip1995
Copy link
Member

Please fix the alignment of the --explain and the --rustc flags and this should be good to go.

Optionally address this comment: #11495 (comment)

src/driver.rs Outdated
#[must_use]
fn help_message() -> &'static str {
color_print::cstr!(
"Checks a package to catch common mistakes and improve your Rust code. Run <cyan>clippy-driver</> with the same arguments you use for <cyan>rustc</>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Checks a package to catch common mistakes and improve your Rust code. Run <cyan>clippy-driver</> with the same arguments you use for <cyan>rustc</>
"Checks a file to catch common mistakes and improve your Rust code.
Run <cyan>clippy-driver</> with the same arguments you use for <cyan>rustc</>

@blyxyas
Copy link
Member Author

blyxyas commented Sep 22, 2023

Optionally address this comment: #11495 (comment)

#[allow] attributes cannot be applied println! since it's a macro invocation.

error: unused attribute `allow`
   --> src/driver.rs:217:13
    |
217 |             #[allow(clippy::ignored_unit_patterns)]
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: the built-in attribute `allow` will be ignored, since it's applied to the macro invocation `println`
   --> src/driver.rs:218:13
    |
218 |             println!("{version_info}");
    |

Truly a sad cowboy moment.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! r=me after squashing the commits 🚀 :shipit:

@blyxyas
Copy link
Member Author

blyxyas commented Sep 25, 2023

@bors r=flip1995

@bors
Copy link
Contributor

bors commented Sep 25, 2023

📌 Commit 6ad218c has been approved by flip1995

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 25, 2023

⌛ Testing commit 6ad218c with merge 78ddc8d...

@bors
Copy link
Contributor

bors commented Sep 25, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 78ddc8d to master...

@bors bors merged commit 78ddc8d into rust-lang:master Sep 25, 2023
4 checks passed
@flip1995 flip1995 removed the I-nominated Issue: Nominated to be discussed at the next Clippy meeting label Oct 3, 2023
@blyxyas blyxyas deleted the help_message_reformat branch October 5, 2023 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Match nightly cargo's help output for cargo-clippy
7 participants