Skip to content

Commit 8b6085f

Browse files
committed
Let anstream handle output piping
1 parent 2d30337 commit 8b6085f

File tree

3 files changed

+74
-127
lines changed

3 files changed

+74
-127
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rustc_tools_util = "0.3.0"
2626
tempfile = { version = "3.2", optional = true }
2727
termize = "0.1"
2828
color-print = "0.3.4" # Sync version with Cargo
29+
anstream = { version = "0.5.0" }
2930

3031
[dev-dependencies]
3132
ui_test = "0.20"

src/driver.rs

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ use rustc_session::EarlyErrorHandler;
2222
use rustc_span::symbol::Symbol;
2323

2424
use std::env;
25-
use std::io::IsTerminal;
2625
use std::ops::Deref;
2726
use std::path::Path;
2827
use std::process::exit;
2928

29+
use anstream::println;
30+
3031
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
3132
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
3233
fn arg_value<'a, T: Deref<Target = str>>(
@@ -163,67 +164,8 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
163164
}
164165
}
165166

166-
fn display_help(is_terminal: bool) {
167-
if is_terminal {
168-
println!("{}", color_print::cstr!(
169-
"Checks a package to catch common mistakes and improve your Rust code.
170-
171-
<green,bold>Usage</>:
172-
<cyan,bold>cargo clippy</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</>
173-
174-
<green,bold>Common options:</>
175-
<cyan,bold>--no-deps</> Run Clippy only on the given crate, without linting the dependencies
176-
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
177-
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
178-
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
179-
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
180-
181-
See all options with <cyan,bold>cargo check --help</>.
182-
183-
<green,bold>Allowing / Denying lints</>
184-
185-
To allow or deny a lint from the command line you can use <cyan,bold>cargo clippy --</> with:
186-
187-
<cyan,bold>-W</> / <cyan,bold>--warn</> <cyan>[LINT]</> Set lint warnings
188-
<cyan,bold>-A</> / <cyan,bold>--allow</> <cyan>[LINT]</> Set lint allowed
189-
<cyan,bold>-D</> / <cyan,bold>--deny</> <cyan>[LINT]</> Set lint denied
190-
<cyan,bold>-F</> / <cyan,bold>--forbid</> <cyan>[LINT]</> Set lint forbidden
191-
192-
You can use tool lints to allow or deny lints from your code, e.g.:
193-
194-
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
195-
"
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-
}
167+
fn display_help() {
168+
println!("{}", help_message())
227169
}
228170

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

286228
if !wrapper_mode && (orig_args.iter().any(|a| a == "--help" || a == "-h") || orig_args.len() == 1) {
287-
display_help(std::io::stdout().is_terminal());
229+
display_help();
288230
exit(0);
289231
}
290232

@@ -326,3 +268,35 @@ pub fn main() {
326268
}
327269
}))
328270
}
271+
272+
fn help_message() -> &'static str {
273+
color_print::cstr!(
274+
"Checks a package to catch common mistakes and improve your Rust code.
275+
276+
<green,bold>Usage</>:
277+
<cyan,bold>cargo clippy</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</>
278+
279+
<green,bold>Common options:</>
280+
<cyan,bold>--no-deps</> Run Clippy only on the given crate, without linting the dependencies
281+
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
282+
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
283+
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
284+
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
285+
286+
See all options with <cyan,bold>cargo check --help</>.
287+
288+
<green,bold>Allowing / Denying lints</>
289+
290+
To allow or deny a lint from the command line you can use <cyan,bold>cargo clippy --</> with:
291+
292+
<cyan,bold>-W</> / <cyan,bold>--warn</> <cyan>[LINT]</> Set lint warnings
293+
<cyan,bold>-A</> / <cyan,bold>--allow</> <cyan>[LINT]</> Set lint allowed
294+
<cyan,bold>-D</> / <cyan,bold>--deny</> <cyan>[LINT]</> Set lint denied
295+
<cyan,bold>-F</> / <cyan,bold>--forbid</> <cyan>[LINT]</> Set lint forbidden
296+
297+
You can use tool lints to allow or deny lints from your code, e.g.:
298+
299+
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
300+
"
301+
)
302+
}

src/main.rs

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

55
use std::env;
6-
use std::io::IsTerminal;
76
use std::path::PathBuf;
87
use std::process::{self, Command};
98

10-
fn show_help(is_terminal: bool) {
11-
if is_terminal {
12-
println!("{}", color_print::cstr!(
13-
"Checks a package to catch common mistakes and improve your Rust code.
14-
15-
<green,bold>Usage</>:
16-
<cyan,bold>cargo clippy</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</>
17-
18-
<green,bold>Common options:</>
19-
<cyan,bold>--no-deps</> Run Clippy only on the given crate, without linting the dependencies
20-
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
21-
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
22-
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
23-
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
24-
25-
See all options with <cyan,bold>cargo check --help</>.
26-
27-
<green,bold>Allowing / Denying lints</>
28-
29-
To allow or deny a lint from the command line you can use <cyan,bold>cargo clippy --</> with:
30-
31-
<cyan,bold>-W</> / <cyan,bold>--warn</> <cyan>[LINT]</> Set lint warnings
32-
<cyan,bold>-A</> / <cyan,bold>--allow</> <cyan>[LINT]</> Set lint allowed
33-
<cyan,bold>-D</> / <cyan,bold>--deny</> <cyan>[LINT]</> Set lint denied
34-
<cyan,bold>-F</> / <cyan,bold>--forbid</> <cyan>[LINT]</> Set lint forbidden
35-
36-
You can use tool lints to allow or deny lints from your code, e.g.:
37-
38-
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
39-
"
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:
9+
use anstream::println;
6010

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-
}
11+
fn show_help() {
12+
println!("{}", help_message());
7113
}
7214

7315
fn show_version() {
@@ -76,10 +18,9 @@ fn show_version() {
7618
}
7719

7820
pub fn main() {
79-
let in_terminal = std::io::stdout().is_terminal();
8021
// Check for version and help flags even when invoked as 'cargo-clippy'
8122
if env::args().any(|a| a == "--help" || a == "-h") {
82-
show_help(in_terminal);
23+
show_help();
8324
return;
8425
}
8526

@@ -95,7 +36,7 @@ pub fn main() {
9536
&lint.strip_prefix("clippy::").unwrap_or(&lint).replace('-', "_"),
9637
));
9738
} else {
98-
show_help(in_terminal);
39+
show_help();
9940
}
10041
return;
10142
}
@@ -202,6 +143,37 @@ where
202143
}
203144
}
204145

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

0 commit comments

Comments
 (0)