Skip to content

Commit 80eb154

Browse files
committed
Fix output when piping to files
1 parent 34f90b5 commit 80eb154

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

src/driver.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_session::EarlyErrorHandler;
2222
use rustc_span::symbol::Symbol;
2323

2424
use std::env;
25+
use std::io::IsTerminal;
2526
use std::ops::Deref;
2627
use std::path::Path;
2728
use std::process::exit;
@@ -162,8 +163,9 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
162163
}
163164
}
164165

165-
fn display_help() {
166-
println!("{}", color_print::cstr!(
166+
fn display_help(is_terminal: bool) {
167+
if is_terminal {
168+
println!("{}", color_print::cstr!(
167169
"Checks a package to catch common mistakes and improve your Rust code.
168170
169171
<green,bold>Usage</>:
@@ -191,7 +193,37 @@ You can use tool lints to allow or deny lints from your code, e.g.:
191193
192194
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
193195
"
194-
));
196+
));
197+
} else {
198+
println!(
199+
"Checks a package to catch common mistakes and improve your Rust code.
200+
201+
Usage:
202+
cargo clippy [OPTIONS] [--] [<ARGS>...]
203+
204+
Common options:
205+
--no-deps Run Clippy only on the given crate, without linting the dependencies
206+
--fix Automatically apply lint suggestions. This flag implies `--no-deps` and `--all-targets`
207+
-h, --help Print this message
208+
-V, --version Print version info and exit
209+
--explain [LINT] Print the documentation for a given lint
210+
211+
See all options with `cargo check --help`.
212+
213+
Allowing / Denying lints:
214+
215+
To allow or deny a lint from the command line you can use `cargo clippy --` with:
216+
217+
-W / --warn [LINT] Set lint warnings
218+
-A / --allow [LINT] Set lint allowed
219+
-D / --deny [LINT] Set lint denied
220+
-F / --forbid [LINT] Set lint forbidden
221+
222+
You can use tool lints to allow or deny lints from your code, e.g.:
223+
224+
#[allow(clippy::needless_lifetimes)]"
225+
);
226+
}
195227
}
196228

197229
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/new?template=ice.yml";
@@ -252,7 +284,7 @@ pub fn main() {
252284
}
253285

254286
if !wrapper_mode && (orig_args.iter().any(|a| a == "--help" || a == "-h") || orig_args.len() == 1) {
255-
display_help();
287+
display_help(std::io::stdout().is_terminal());
256288
exit(0);
257289
}
258290

src/main.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#![warn(rust_2018_idioms, unused_lifetimes)]
44

55
use std::env;
6+
use std::io::IsTerminal;
67
use std::path::PathBuf;
78
use std::process::{self, Command};
89

9-
fn show_help() {
10-
println!("{}", color_print::cstr!(
10+
fn show_help(is_terminal: bool) {
11+
if is_terminal {
12+
println!("{}", color_print::cstr!(
1113
"Checks a package to catch common mistakes and improve your Rust code.
1214
1315
<green,bold>Usage</>:
@@ -35,7 +37,37 @@ You can use tool lints to allow or deny lints from your code, e.g.:
3537
3638
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
3739
"
38-
));
40+
));
41+
} else {
42+
println!(
43+
"Checks a package to catch common mistakes and improve your Rust code.
44+
45+
Usage:
46+
cargo clippy [OPTIONS] [--] [<ARGS>...]
47+
48+
Common options:
49+
--no-deps Run Clippy only on the given crate, without linting the dependencies
50+
--fix Automatically apply lint suggestions. This flag implies `--no-deps` and `--all-targets`
51+
-h, --help Print this message
52+
-V, --version Print version info and exit
53+
--explain [LINT] Print the documentation for a given lint
54+
55+
See all options with `cargo check --help`.
56+
57+
Allowing / Denying lints:
58+
59+
To allow or deny a lint from the command line you can use `cargo clippy --` with:
60+
61+
-W / --warn [LINT] Set lint warnings
62+
-A / --allow [LINT] Set lint allowed
63+
-D / --deny [LINT] Set lint denied
64+
-F / --forbid [LINT] Set lint forbidden
65+
66+
You can use tool lints to allow or deny lints from your code, e.g.:
67+
68+
#[allow(clippy::needless_lifetimes)]"
69+
);
70+
}
3971
}
4072

4173
fn show_version() {
@@ -44,9 +76,10 @@ fn show_version() {
4476
}
4577

4678
pub fn main() {
79+
let in_terminal = std::io::stdout().is_terminal();
4780
// Check for version and help flags even when invoked as 'cargo-clippy'
4881
if env::args().any(|a| a == "--help" || a == "-h") {
49-
show_help();
82+
show_help(in_terminal);
5083
return;
5184
}
5285

@@ -62,7 +95,7 @@ pub fn main() {
6295
&lint.strip_prefix("clippy::").unwrap_or(&lint).replace('-', "_"),
6396
));
6497
} else {
65-
show_help();
98+
show_help(in_terminal);
6699
}
67100
return;
68101
}

0 commit comments

Comments
 (0)