Skip to content

Commit 78ddc8d

Browse files
committed
Auto merge of rust-lang#11495 - blyxyas:help_message_reformat, r=flip1995
Add colored help to be consistent with Cargo 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 rust-lang#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](https://github.com/rust-lang/rust-clippy/assets/73757586/0c4d1b6d-5aa2-4146-a401-9ae88f6dedf5) (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 🎉.
2 parents 7671c28 + 6ad218c commit 78ddc8d

File tree

3 files changed

+66
-54
lines changed

3 files changed

+66
-54
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ clippy_lints = { path = "clippy_lints" }
2525
rustc_tools_util = "0.3.0"
2626
tempfile = { version = "3.2", optional = true }
2727
termize = "0.1"
28+
color-print = "0.3.4" # Sync version with Cargo
29+
anstream = "0.5.0"
2830

2931
[dev-dependencies]
3032
ui_test = "0.20"

src/driver.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use std::ops::Deref;
2626
use std::path::Path;
2727
use std::process::exit;
2828

29+
use anstream::println;
30+
2931
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
3032
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
3133
fn arg_value<'a, T: Deref<Target = str>>(
@@ -162,39 +164,15 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
162164
}
163165
}
164166

167+
#[allow(clippy::ignored_unit_patterns)]
165168
fn display_help() {
166-
println!(
167-
"\
168-
Checks a package to catch common mistakes and improve your Rust code.
169-
170-
Usage:
171-
cargo clippy [options] [--] [<opts>...]
172-
173-
Common options:
174-
-h, --help Print this message
175-
--rustc Pass all args to rustc
176-
-V, --version Print version info and exit
177-
178-
For the other options see `cargo check --help`.
179-
180-
To allow or deny a lint from the command line you can use `cargo clippy --`
181-
with:
182-
183-
-W --warn OPT Set lint warnings
184-
-A --allow OPT Set lint allowed
185-
-D --deny OPT Set lint denied
186-
-F --forbid OPT Set lint forbidden
187-
188-
You can use tool lints to allow or deny lints from your code, eg.:
189-
190-
#[allow(clippy::needless_lifetimes)]
191-
"
192-
);
169+
println!("{}", help_message());
193170
}
194171

195172
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/new?template=ice.yml";
196173

197174
#[allow(clippy::too_many_lines)]
175+
#[allow(clippy::ignored_unit_patterns)]
198176
pub fn main() {
199177
let handler = EarlyErrorHandler::new(ErrorOutputType::default());
200178

@@ -236,6 +214,7 @@ pub fn main() {
236214

237215
if orig_args.iter().any(|a| a == "--version" || a == "-V") {
238216
let version_info = rustc_tools_util::get_version_info!();
217+
239218
println!("{version_info}");
240219
exit(0);
241220
}
@@ -292,3 +271,25 @@ pub fn main() {
292271
}
293272
}))
294273
}
274+
275+
#[must_use]
276+
fn help_message() -> &'static str {
277+
color_print::cstr!(
278+
"Checks a file to catch common mistakes and improve your Rust code.
279+
Run <cyan>clippy-driver</> with the same arguments you use for <cyan>rustc</>
280+
281+
<green,bold>Usage</>:
282+
<cyan,bold>clippy-driver</> <cyan>[OPTIONS] INPUT</>
283+
284+
<green,bold>Common options:</>
285+
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
286+
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
287+
<cyan,bold>--rustc</> Pass all arguments to <cyan>rustc</>
288+
289+
<green,bold>Allowing / Denying lints</>
290+
You can use tool lints to allow or deny lints from your code, e.g.:
291+
292+
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
293+
"
294+
)
295+
}

src/main.rs

+36-27
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,14 @@ use std::env;
66
use std::path::PathBuf;
77
use std::process::{self, Command};
88

9-
const CARGO_CLIPPY_HELP: &str = "Checks a package to catch common mistakes and improve your Rust code.
10-
11-
Usage:
12-
cargo clippy [options] [--] [<opts>...]
13-
14-
Common options:
15-
--no-deps Run Clippy only on the given crate, without linting the dependencies
16-
--fix Automatically apply lint suggestions. This flag implies `--no-deps` and `--all-targets`
17-
-h, --help Print this message
18-
-V, --version Print version info and exit
19-
--explain LINT Print the documentation for a given lint
20-
21-
For the other options see `cargo check --help`.
22-
23-
To allow or deny a lint from the command line you can use `cargo clippy --`
24-
with:
25-
26-
-W --warn OPT Set lint warnings
27-
-A --allow OPT Set lint allowed
28-
-D --deny OPT Set lint denied
29-
-F --forbid OPT Set lint forbidden
30-
31-
You can use tool lints to allow or deny lints from your code, e.g.:
32-
33-
#[allow(clippy::needless_lifetimes)]
34-
";
9+
use anstream::println;
3510

11+
#[allow(clippy::ignored_unit_patterns)]
3612
fn show_help() {
37-
println!("{CARGO_CLIPPY_HELP}");
13+
println!("{}", help_message());
3814
}
3915

16+
#[allow(clippy::ignored_unit_patterns)]
4017
fn show_version() {
4118
let version_info = rustc_tools_util::get_version_info!();
4219
println!("{version_info}");
@@ -168,6 +145,38 @@ where
168145
}
169146
}
170147

148+
#[must_use]
149+
pub fn help_message() -> &'static str {
150+
color_print::cstr!(
151+
"Checks a package to catch common mistakes and improve your Rust code.
152+
153+
<green,bold>Usage</>:
154+
<cyan,bold>cargo clippy</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</>
155+
156+
<green,bold>Common options:</>
157+
<cyan,bold>--no-deps</> Run Clippy only on the given crate, without linting the dependencies
158+
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
159+
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
160+
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
161+
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
162+
163+
See all options with <cyan,bold>cargo check --help</>.
164+
165+
<green,bold>Allowing / Denying lints</>
166+
167+
To allow or deny a lint from the command line you can use <cyan,bold>cargo clippy --</> with:
168+
169+
<cyan,bold>-W</> / <cyan,bold>--warn</> <cyan>[LINT]</> Set lint warnings
170+
<cyan,bold>-A</> / <cyan,bold>--allow</> <cyan>[LINT]</> Set lint allowed
171+
<cyan,bold>-D</> / <cyan,bold>--deny</> <cyan>[LINT]</> Set lint denied
172+
<cyan,bold>-F</> / <cyan,bold>--forbid</> <cyan>[LINT]</> Set lint forbidden
173+
174+
You can use tool lints to allow or deny lints from your code, e.g.:
175+
176+
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
177+
"
178+
)
179+
}
171180
#[cfg(test)]
172181
mod tests {
173182
use super::ClippyCmd;

0 commit comments

Comments
 (0)