Skip to content

Commit fba02d8

Browse files
committed
prelim arg parse
1 parent 153a988 commit fba02d8

File tree

3 files changed

+139
-25
lines changed

3 files changed

+139
-25
lines changed

clippy_dev/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ fn update_lints(update_mode: &UpdateMode) {
100100
"\
101101
//! This file is managed by util/dev update_lints. Do not edit.
102102
103-
mod lint;
104-
use lint::Lint;
103+
pub mod lint;
104+
pub use lint::Lint;
105105
106106
pub const ALL_LINTS: [Lint; {}] = {:#?};\n",
107107
sorted_usable_lints.len(),

src/driver.rs

+135-21
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,108 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
110110
}
111111
}
112112

113-
pub fn main() {
114-
rustc_driver::init_rustc_env_logger();
115-
exit(
116-
rustc_driver::report_ices_to_stderr_if_any(move || {
117-
use std::env;
113+
fn describe_lints() {
114+
use lintlist::*;
118115

119-
if std::env::args().any(|a| a == "--version" || a == "-V") {
120-
let version_info = rustc_tools_util::get_version_info!();
121-
println!("{}", version_info);
122-
exit(0);
123-
}
116+
println!(
117+
"
118+
Available lint options:
119+
-W <foo> Warn about <foo>
120+
-A <foo> Allow <foo>
121+
-D <foo> Deny <foo>
122+
-F <foo> Forbid <foo> (deny <foo> and all attempts to override)
124123
125-
if std::env::args().any(|a| a == "--help" || a == "-h") {
126-
println!(
127-
"\
124+
"
125+
);
126+
127+
let mut lints: Vec<_> = ALL_LINTS.iter().collect();
128+
// The sort doesn't case-fold but it's doubtful we care.
129+
lints.sort_by_cached_key(|x: &&Lint| ("unknown", x.name));
130+
131+
let max_name_len = lints
132+
.iter()
133+
.map(|lint| lint.name.len())
134+
.map(|len| len + "clippy::".len())
135+
.max()
136+
.unwrap_or(0);
137+
138+
let padded = |x: &str| {
139+
let mut s = " ".repeat(max_name_len - x.chars().count());
140+
s.push_str(x);
141+
s
142+
};
143+
144+
let scoped = |x: &str| format!("clippy::{}", x);
145+
146+
println!("Lint checks provided by clippy:\n");
147+
println!(" {} {:7.7} meaning", padded("name"), "default");
148+
println!(" {} {:7.7} -------", padded("----"), "-------");
149+
150+
let print_lints = |lints: Vec<&Lint>| {
151+
for lint in lints {
152+
let name = lint.name.replace("_", "-");
153+
println!(" {} {:7.7} {}", padded(&scoped(&name)), "unknown", lint.desc);
154+
}
155+
println!("\n");
156+
};
157+
158+
print_lints(lints);
159+
160+
// let max_name_len = max("warnings".len(),
161+
// plugin_groups.iter()
162+
// .chain(&builtin_groups)
163+
// .map(|&(s, _)| s.chars().count())
164+
// .max()
165+
// .unwrap_or(0));
166+
167+
// let padded = |x: &str| {
168+
// let mut s = " ".repeat(max_name_len - x.chars().count());
169+
// s.push_str(x);
170+
// s
171+
// };
172+
173+
// println!("Lint groups provided by rustc:\n");
174+
// println!(" {} {}", padded("name"), "sub-lints");
175+
// println!(" {} {}", padded("----"), "---------");
176+
// println!(" {} {}", padded("warnings"), "all lints that are set to issue warnings");
177+
178+
// let print_lint_groups = |lints: Vec<(&'static str, Vec<lint::LintId>)>| {
179+
// for (name, to) in lints {
180+
// let name = name.to_lowercase().replace("_", "-");
181+
// let desc = to.into_iter()
182+
// .map(|x| x.to_string().replace("_", "-"))
183+
// .collect::<Vec<String>>()
184+
// .join(", ");
185+
// println!(" {} {}", padded(&name), desc);
186+
// }
187+
// println!("\n");
188+
// };
189+
190+
// print_lint_groups(builtin_groups);
191+
192+
// match (loaded_plugins, plugin.len(), plugin_groups.len()) {
193+
// (false, 0, _) | (false, _, 0) => {
194+
// println!("Compiler plugins can provide additional lints and lint groups. To see a \
195+
// listing of these, re-run `rustc -W help` with a crate filename.");
196+
// }
197+
// (false, ..) => panic!("didn't load lint plugins but got them anyway!"),
198+
// (true, 0, 0) => println!("This crate does not load any lint plugins or lint groups."),
199+
// (true, l, g) => {
200+
// if l > 0 {
201+
// println!("Lint checks provided by plugins loaded by this crate:\n");
202+
// print_lints(plugin);
203+
// }
204+
// if g > 0 {
205+
// println!("Lint groups provided by plugins loaded by this crate:\n");
206+
// print_lint_groups(plugin_groups);
207+
// }
208+
// }
209+
// }
210+
}
211+
212+
fn display_help() {
213+
println!(
214+
"\
128215
Checks a package to catch common mistakes and improve your Rust code.
129216
130217
Usage:
@@ -148,11 +235,18 @@ You can use tool lints to allow or deny lints from your code, eg.:
148235
149236
#[allow(clippy::needless_lifetimes)]
150237
"
151-
);
238+
);
239+
}
152240

153-
for lint in &lintlist::ALL_LINTS[..] {
154-
println!("clippy::{},", lint.name);
155-
}
241+
pub fn main() {
242+
rustc_driver::init_rustc_env_logger();
243+
exit(
244+
rustc_driver::report_ices_to_stderr_if_any(move || {
245+
use std::env;
246+
247+
if std::env::args().any(|a| a == "--version" || a == "-V") {
248+
let version_info = rustc_tools_util::get_version_info!();
249+
println!("{}", version_info);
156250
exit(0);
157251
}
158252

@@ -189,13 +283,33 @@ You can use tool lints to allow or deny lints from your code, eg.:
189283

190284
// Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
191285
// We're invoking the compiler programmatically, so we ignore this/
192-
if orig_args.len() <= 1 {
193-
std::process::exit(1);
194-
}
195-
if Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref()) {
286+
let wrapper_mode = Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref());
287+
288+
if wrapper_mode {
196289
// we still want to be able to invoke it normally though
197290
orig_args.remove(1);
198291
}
292+
293+
if !wrapper_mode && std::env::args().any(|a| a == "--help" || a == "-h") {
294+
display_help();
295+
exit(0);
296+
}
297+
298+
let args: Vec<_> = std::env::args().collect();
299+
300+
if !wrapper_mode
301+
&& args.windows(2).any(|args| {
302+
args[1] == "help"
303+
&& match args[0].as_str() {
304+
"-W" | "-A" | "-D" | "-F" => true,
305+
_ => false,
306+
}
307+
})
308+
{
309+
describe_lints();
310+
exit(0);
311+
}
312+
199313
// this conditional check for the --sysroot flag is there so users can call
200314
// `clippy_driver` directly
201315
// without having to pass --sysroot or anything

src/lintlist/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This file is managed by util/dev update_lints. Do not edit.
22
3-
mod lint;
4-
use lint::Lint;
3+
pub mod lint;
4+
pub use lint::Lint;
55

66
pub const ALL_LINTS: [Lint; 304] = [
77
Lint {

0 commit comments

Comments
 (0)