Fix clap deprecation warnings

This commit is contained in:
Jason Newcomb 2022-06-13 21:10:30 -04:00
parent 17b7ab004f
commit cccc750046
5 changed files with 120 additions and 167 deletions

View File

@ -13,7 +13,7 @@ fn exit_if_err(status: io::Result<ExitStatus>) {
} }
} }
pub fn run<'a>(path: &str, args: impl Iterator<Item = &'a str>) { pub fn run<'a>(path: &str, args: impl Iterator<Item = &'a String>) {
let is_file = match fs::metadata(path) { let is_file = match fs::metadata(path) {
Ok(metadata) => metadata.is_file(), Ok(metadata) => metadata.is_file(),
Err(e) => { Err(e) => {

View File

@ -2,7 +2,7 @@
// warn on lints, that are included in `rust-lang/rust`s bootstrap // warn on lints, that are included in `rust-lang/rust`s bootstrap
#![warn(rust_2018_idioms, unused_lifetimes)] #![warn(rust_2018_idioms, unused_lifetimes)]
use clap::{Arg, ArgMatches, Command}; use clap::{Arg, ArgAction, ArgMatches, Command, PossibleValue};
use clippy_dev::{bless, fmt, lint, new_lint, serve, setup, update_lints}; use clippy_dev::{bless, fmt, lint, new_lint, serve, setup, update_lints};
use indoc::indoc; use indoc::indoc;
fn main() { fn main() {
@ -10,15 +10,15 @@ fn main() {
match matches.subcommand() { match matches.subcommand() {
Some(("bless", matches)) => { Some(("bless", matches)) => {
bless::bless(matches.is_present("ignore-timestamp")); bless::bless(matches.contains_id("ignore-timestamp"));
}, },
Some(("fmt", matches)) => { Some(("fmt", matches)) => {
fmt::run(matches.is_present("check"), matches.is_present("verbose")); fmt::run(matches.contains_id("check"), matches.contains_id("verbose"));
}, },
Some(("update_lints", matches)) => { Some(("update_lints", matches)) => {
if matches.is_present("print-only") { if matches.contains_id("print-only") {
update_lints::print_lints(); update_lints::print_lints();
} else if matches.is_present("check") { } else if matches.contains_id("check") {
update_lints::update(update_lints::UpdateMode::Check); update_lints::update(update_lints::UpdateMode::Check);
} else { } else {
update_lints::update(update_lints::UpdateMode::Change); update_lints::update(update_lints::UpdateMode::Change);
@ -26,10 +26,10 @@ fn main() {
}, },
Some(("new_lint", matches)) => { Some(("new_lint", matches)) => {
match new_lint::create( match new_lint::create(
matches.value_of("pass"), matches.get_one::<String>("pass"),
matches.value_of("name"), matches.get_one::<String>("name"),
matches.value_of("category"), matches.get_one::<String>("category"),
matches.is_present("msrv"), matches.contains_id("msrv"),
) { ) {
Ok(_) => update_lints::update(update_lints::UpdateMode::Change), Ok(_) => update_lints::update(update_lints::UpdateMode::Change),
Err(e) => eprintln!("Unable to create lint: {}", e), Err(e) => eprintln!("Unable to create lint: {}", e),
@ -37,28 +37,28 @@ fn main() {
}, },
Some(("setup", sub_command)) => match sub_command.subcommand() { Some(("setup", sub_command)) => match sub_command.subcommand() {
Some(("intellij", matches)) => { Some(("intellij", matches)) => {
if matches.is_present("remove") { if matches.contains_id("remove") {
setup::intellij::remove_rustc_src(); setup::intellij::remove_rustc_src();
} else { } else {
setup::intellij::setup_rustc_src( setup::intellij::setup_rustc_src(
matches matches
.value_of("rustc-repo-path") .get_one::<String>("rustc-repo-path")
.expect("this field is mandatory and therefore always valid"), .expect("this field is mandatory and therefore always valid"),
); );
} }
}, },
Some(("git-hook", matches)) => { Some(("git-hook", matches)) => {
if matches.is_present("remove") { if matches.contains_id("remove") {
setup::git_hook::remove_hook(); setup::git_hook::remove_hook();
} else { } else {
setup::git_hook::install_hook(matches.is_present("force-override")); setup::git_hook::install_hook(matches.contains_id("force-override"));
} }
}, },
Some(("vscode-tasks", matches)) => { Some(("vscode-tasks", matches)) => {
if matches.is_present("remove") { if matches.contains_id("remove") {
setup::vscode::remove_tasks(); setup::vscode::remove_tasks();
} else { } else {
setup::vscode::install_tasks(matches.is_present("force-override")); setup::vscode::install_tasks(matches.contains_id("force-override"));
} }
}, },
_ => {}, _ => {},
@ -70,19 +70,19 @@ fn main() {
_ => {}, _ => {},
}, },
Some(("serve", matches)) => { Some(("serve", matches)) => {
let port = matches.value_of("port").unwrap().parse().unwrap(); let port = *matches.get_one::<u16>("port").unwrap();
let lint = matches.value_of("lint"); let lint = matches.get_one::<String>("lint");
serve::run(port, lint); serve::run(port, lint);
}, },
Some(("lint", matches)) => { Some(("lint", matches)) => {
let path = matches.value_of("path").unwrap(); let path = matches.get_one::<String>("path").unwrap();
let args = matches.values_of("args").into_iter().flatten(); let args = matches.get_many::<String>("args").into_iter().flatten();
lint::run(path, args); lint::run(path, args);
}, },
Some(("rename_lint", matches)) => { Some(("rename_lint", matches)) => {
let old_name = matches.value_of("old_name").unwrap(); let old_name = matches.get_one::<String>("old_name").unwrap();
let new_name = matches.value_of("new_name").unwrap_or(old_name); let new_name = matches.get_one::<String>("new_name").unwrap_or(old_name);
let uplift = matches.is_present("uplift"); let uplift = matches.contains_id("uplift");
update_lints::rename(old_name, new_name, uplift); update_lints::rename(old_name, new_name, uplift);
}, },
_ => {}, _ => {},
@ -92,98 +92,86 @@ fn main() {
fn get_clap_config() -> ArgMatches { fn get_clap_config() -> ArgMatches {
Command::new("Clippy developer tooling") Command::new("Clippy developer tooling")
.arg_required_else_help(true) .arg_required_else_help(true)
.subcommand( .subcommands([
Command::new("bless").about("bless the test output changes").arg( Command::new("bless").about("bless the test output changes").arg(
Arg::new("ignore-timestamp") Arg::new("ignore-timestamp")
.long("ignore-timestamp") .long("ignore-timestamp")
.help("Include files updated before clippy was built"), .help("Include files updated before clippy was built"),
), ),
)
.subcommand(
Command::new("fmt") Command::new("fmt")
.about("Run rustfmt on all projects and tests") .about("Run rustfmt on all projects and tests")
.arg(Arg::new("check").long("check").help("Use the rustfmt --check option")) .args([
.arg(Arg::new("verbose").short('v').long("verbose").help("Echo commands run")), Arg::new("check").long("check").help("Use the rustfmt --check option"),
) Arg::new("verbose").short('v').long("verbose").help("Echo commands run"),
.subcommand( ]),
Command::new("update_lints") Command::new("update_lints")
.about("Updates lint registration and information from the source code") .about("Updates lint registration and information from the source code")
.long_about( .long_about(
"Makes sure that:\n \ "Makes sure that:\n \
* the lint count in README.md is correct\n \ * the lint count in README.md is correct\n \
* the changelog contains markdown link references at the bottom\n \ * the changelog contains markdown link references at the bottom\n \
* all lint groups include the correct lints\n \ * all lint groups include the correct lints\n \
* lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \ * lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
* all lints are registered in the lint store", * all lints are registered in the lint store",
) )
.arg(Arg::new("print-only").long("print-only").help( .args([
"Print a table of lints to STDOUT. \ Arg::new("print-only").long("print-only").help(
This does not include deprecated and internal lints. \ "Print a table of lints to STDOUT. \
(Does not modify any files)", This does not include deprecated and internal lints. \
)) (Does not modify any files)",
.arg( ),
Arg::new("check") Arg::new("check")
.long("check") .long("check")
.help("Checks that `cargo dev update_lints` has been run. Used on CI."), .help("Checks that `cargo dev update_lints` has been run. Used on CI."),
), ]),
)
.subcommand(
Command::new("new_lint") Command::new("new_lint")
.about("Create new lint and run `cargo dev update_lints`") .about("Create new lint and run `cargo dev update_lints`")
.arg( .args([
Arg::new("pass") Arg::new("pass")
.short('p') .short('p')
.long("pass") .long("pass")
.help("Specify whether the lint runs during the early or late pass") .help("Specify whether the lint runs during the early or late pass")
.takes_value(true) .takes_value(true)
.possible_values(&["early", "late"]) .value_parser([PossibleValue::new("early"), PossibleValue::new("late")])
.required(true), .required(true),
)
.arg(
Arg::new("name") Arg::new("name")
.short('n') .short('n')
.long("name") .long("name")
.help("Name of the new lint in snake case, ex: fn_too_long") .help("Name of the new lint in snake case, ex: fn_too_long")
.takes_value(true) .takes_value(true)
.required(true), .required(true),
)
.arg(
Arg::new("category") Arg::new("category")
.short('c') .short('c')
.long("category") .long("category")
.help("What category the lint belongs to") .help("What category the lint belongs to")
.default_value("nursery") .default_value("nursery")
.possible_values(&[ .value_parser([
"style", PossibleValue::new("style"),
"correctness", PossibleValue::new("correctness"),
"suspicious", PossibleValue::new("suspicious"),
"complexity", PossibleValue::new("complexity"),
"perf", PossibleValue::new("perf"),
"pedantic", PossibleValue::new("pedantic"),
"restriction", PossibleValue::new("restriction"),
"cargo", PossibleValue::new("cargo"),
"nursery", PossibleValue::new("nursery"),
"internal", PossibleValue::new("internal"),
"internal_warn", PossibleValue::new("internal_warn"),
]) ])
.takes_value(true), .takes_value(true),
) Arg::new("msrv").long("msrv").help("Add MSRV config code to the lint"),
.arg(Arg::new("msrv").long("msrv").help("Add MSRV config code to the lint")), ]),
)
.subcommand(
Command::new("setup") Command::new("setup")
.about("Support for setting up your personal development environment") .about("Support for setting up your personal development environment")
.arg_required_else_help(true) .arg_required_else_help(true)
.subcommand( .subcommands([
Command::new("intellij") Command::new("intellij")
.about("Alter dependencies so Intellij Rust can find rustc internals") .about("Alter dependencies so Intellij Rust can find rustc internals")
.arg( .args([
Arg::new("remove") Arg::new("remove")
.long("remove") .long("remove")
.help("Remove the dependencies added with 'cargo dev setup intellij'") .help("Remove the dependencies added with 'cargo dev setup intellij'")
.required(false), .required(false),
)
.arg(
Arg::new("rustc-repo-path") Arg::new("rustc-repo-path")
.long("repo-path") .long("repo-path")
.short('r') .short('r')
@ -192,67 +180,53 @@ fn get_clap_config() -> ArgMatches {
.value_name("path") .value_name("path")
.conflicts_with("remove") .conflicts_with("remove")
.required(true), .required(true),
), ]),
)
.subcommand(
Command::new("git-hook") Command::new("git-hook")
.about("Add a pre-commit git hook that formats your code to make it look pretty") .about("Add a pre-commit git hook that formats your code to make it look pretty")
.arg( .args([
Arg::new("remove") Arg::new("remove")
.long("remove") .long("remove")
.help("Remove the pre-commit hook added with 'cargo dev setup git-hook'") .help("Remove the pre-commit hook added with 'cargo dev setup git-hook'")
.required(false), .required(false),
)
.arg(
Arg::new("force-override") Arg::new("force-override")
.long("force-override") .long("force-override")
.short('f') .short('f')
.help("Forces the override of an existing git pre-commit hook") .help("Forces the override of an existing git pre-commit hook")
.required(false), .required(false),
), ]),
)
.subcommand(
Command::new("vscode-tasks") Command::new("vscode-tasks")
.about("Add several tasks to vscode for formatting, validation and testing") .about("Add several tasks to vscode for formatting, validation and testing")
.arg( .args([
Arg::new("remove") Arg::new("remove")
.long("remove") .long("remove")
.help("Remove the tasks added with 'cargo dev setup vscode-tasks'") .help("Remove the tasks added with 'cargo dev setup vscode-tasks'")
.required(false), .required(false),
)
.arg(
Arg::new("force-override") Arg::new("force-override")
.long("force-override") .long("force-override")
.short('f') .short('f')
.help("Forces the override of existing vscode tasks") .help("Forces the override of existing vscode tasks")
.required(false), .required(false),
), ]),
), ]),
)
.subcommand(
Command::new("remove") Command::new("remove")
.about("Support for undoing changes done by the setup command") .about("Support for undoing changes done by the setup command")
.arg_required_else_help(true) .arg_required_else_help(true)
.subcommand(Command::new("git-hook").about("Remove any existing pre-commit git hook")) .subcommands([
.subcommand(Command::new("vscode-tasks").about("Remove any existing vscode tasks")) Command::new("git-hook").about("Remove any existing pre-commit git hook"),
.subcommand( Command::new("vscode-tasks").about("Remove any existing vscode tasks"),
Command::new("intellij").about("Removes rustc source paths added via `cargo dev setup intellij`"), Command::new("intellij").about("Removes rustc source paths added via `cargo dev setup intellij`"),
), ]),
)
.subcommand(
Command::new("serve") Command::new("serve")
.about("Launch a local 'ALL the Clippy Lints' website in a browser") .about("Launch a local 'ALL the Clippy Lints' website in a browser")
.arg( .args([
Arg::new("port") Arg::new("port")
.long("port") .long("port")
.short('p') .short('p')
.help("Local port for the http server") .help("Local port for the http server")
.default_value("8000") .default_value("8000")
.validator_os(serve::validate_port), .value_parser(clap::value_parser!(u16)),
) Arg::new("lint").help("Which lint's page to load initially (optional)"),
.arg(Arg::new("lint").help("Which lint's page to load initially (optional)")), ]),
)
.subcommand(
Command::new("lint") Command::new("lint")
.about("Manually run clippy on a file or package") .about("Manually run clippy on a file or package")
.after_help(indoc! {" .after_help(indoc! {"
@ -271,37 +245,27 @@ fn get_clap_config() -> ArgMatches {
cargo dev lint file.rs -- -W clippy::pedantic cargo dev lint file.rs -- -W clippy::pedantic
cargo dev lint ~/my-project -- -- -W clippy::pedantic cargo dev lint ~/my-project -- -- -W clippy::pedantic
"}) "})
.arg( .args([
Arg::new("path") Arg::new("path")
.required(true) .required(true)
.help("The path to a file or package directory to lint"), .help("The path to a file or package directory to lint"),
)
.arg(
Arg::new("args") Arg::new("args")
.multiple_occurrences(true) .action(ArgAction::Append)
.help("Pass extra arguments to cargo/clippy-driver"), .help("Pass extra arguments to cargo/clippy-driver"),
), ]),
) Command::new("rename_lint").about("Renames the given lint").args([
.subcommand( Arg::new("old_name")
Command::new("rename_lint") .index(1)
.about("Renames the given lint") .required(true)
.arg( .help("The name of the lint to rename"),
Arg::new("old_name") Arg::new("new_name")
.index(1) .index(2)
.required(true) .required_unless_present("uplift")
.help("The name of the lint to rename"), .help("The new name of the lint"),
) Arg::new("uplift")
.arg( .long("uplift")
Arg::new("new_name") .help("This lint will be uplifted into rustc"),
.index(2) ]),
.required_unless_present("uplift") ])
.help("The new name of the lint"),
)
.arg(
Arg::new("uplift")
.long("uplift")
.help("This lint will be uplifted into rustc"),
),
)
.get_matches() .get_matches()
} }

View File

@ -34,7 +34,12 @@ fn context<C: AsRef<str>>(self, text: C) -> Self {
/// # Errors /// # Errors
/// ///
/// This function errors out if the files couldn't be created or written to. /// This function errors out if the files couldn't be created or written to.
pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>, msrv: bool) -> io::Result<()> { pub fn create(
pass: Option<&String>,
lint_name: Option<&String>,
category: Option<&String>,
msrv: bool,
) -> io::Result<()> {
let lint = LintData { let lint = LintData {
pass: pass.expect("`pass` argument is validated by clap"), pass: pass.expect("`pass` argument is validated by clap"),
name: lint_name.expect("`name` argument is validated by clap"), name: lint_name.expect("`name` argument is validated by clap"),

View File

@ -8,7 +8,7 @@
/// # Panics /// # Panics
/// ///
/// Panics if the python commands could not be spawned /// Panics if the python commands could not be spawned
pub fn run(port: u16, lint: Option<&str>) -> ! { pub fn run(port: u16, lint: Option<&String>) -> ! {
let mut url = Some(match lint { let mut url = Some(match lint {
None => format!("http://localhost:{}", port), None => format!("http://localhost:{}", port),
Some(lint) => format!("http://localhost:{}/#{}", port, lint), Some(lint) => format!("http://localhost:{}/#{}", port, lint),

View File

@ -1,50 +1,40 @@
use clap::{Arg, ArgMatches, Command}; use clap::{Arg, ArgAction, ArgMatches, Command};
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
fn get_clap_config() -> ArgMatches { fn get_clap_config() -> ArgMatches {
Command::new("lintcheck") Command::new("lintcheck")
.about("run clippy on a set of crates and check output") .about("run clippy on a set of crates and check output")
.arg( .args([
Arg::new("only") Arg::new("only")
.takes_value(true) .action(ArgAction::Set)
.value_name("CRATE") .value_name("CRATE")
.long("only") .long("only")
.help("Only process a single crate of the list"), .help("Only process a single crate of the list"),
)
.arg(
Arg::new("crates-toml") Arg::new("crates-toml")
.takes_value(true) .action(ArgAction::Set)
.value_name("CRATES-SOURCES-TOML-PATH") .value_name("CRATES-SOURCES-TOML-PATH")
.long("crates-toml") .long("crates-toml")
.help("Set the path for a crates.toml where lintcheck should read the sources from"), .help("Set the path for a crates.toml where lintcheck should read the sources from"),
)
.arg(
Arg::new("threads") Arg::new("threads")
.takes_value(true) .action(ArgAction::Set)
.value_name("N") .value_name("N")
.value_parser(clap::value_parser!(usize))
.short('j') .short('j')
.long("jobs") .long("jobs")
.help("Number of threads to use, 0 automatic choice"), .help("Number of threads to use, 0 automatic choice"),
)
.arg(
Arg::new("fix") Arg::new("fix")
.long("--fix") .long("fix")
.help("Runs cargo clippy --fix and checks if all suggestions apply"), .help("Runs cargo clippy --fix and checks if all suggestions apply"),
)
.arg(
Arg::new("filter") Arg::new("filter")
.long("--filter") .long("filter")
.takes_value(true) .action(ArgAction::Append)
.multiple_occurrences(true)
.value_name("clippy_lint_name") .value_name("clippy_lint_name")
.help("Apply a filter to only collect specified lints, this also overrides `allow` attributes"), .help("Apply a filter to only collect specified lints, this also overrides `allow` attributes"),
)
.arg(
Arg::new("markdown") Arg::new("markdown")
.long("--markdown") .long("markdown")
.help("Change the reports table to use markdown links"), .help("Change the reports table to use markdown links"),
) ])
.get_matches() .get_matches()
} }
@ -75,13 +65,13 @@ pub fn new() -> Self {
// if not, use the default "lintcheck/lintcheck_crates.toml" // if not, use the default "lintcheck/lintcheck_crates.toml"
let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| { let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| {
clap_config clap_config
.value_of("crates-toml") .get_one::<String>("crates-toml")
.clone() .map(|s| &**s)
.unwrap_or("lintcheck/lintcheck_crates.toml") .unwrap_or("lintcheck/lintcheck_crates.toml")
.to_string() .into()
}); });
let markdown = clap_config.is_present("markdown"); let markdown = clap_config.contains_id("markdown");
let sources_toml_path = PathBuf::from(sources_toml); let sources_toml_path = PathBuf::from(sources_toml);
// for the path where we save the lint results, get the filename without extension (so for // for the path where we save the lint results, get the filename without extension (so for
@ -96,25 +86,19 @@ pub fn new() -> Self {
// look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and // look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and
// use half of that for the physical core count // use half of that for the physical core count
// by default use a single thread // by default use a single thread
let max_jobs = match clap_config.value_of("threads") { let max_jobs = match clap_config.get_one::<usize>("threads") {
Some(threads) => { Some(&0) => {
let threads: usize = threads // automatic choice
.parse() // Rayon seems to return thread count so half that for core count
.unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads)); (rayon::current_num_threads() / 2) as usize
if threads == 0 {
// automatic choice
// Rayon seems to return thread count so half that for core count
(rayon::current_num_threads() / 2) as usize
} else {
threads
}
}, },
Some(&threads) => threads,
// no -j passed, use a single thread // no -j passed, use a single thread
None => 1, None => 1,
}; };
let lint_filter: Vec<String> = clap_config let lint_filter: Vec<String> = clap_config
.values_of("filter") .get_many::<String>("filter")
.map(|iter| { .map(|iter| {
iter.map(|lint_name| { iter.map(|lint_name| {
let mut filter = lint_name.replace('_', "-"); let mut filter = lint_name.replace('_', "-");
@ -131,8 +115,8 @@ pub fn new() -> Self {
max_jobs, max_jobs,
sources_toml_path, sources_toml_path,
lintcheck_results_path, lintcheck_results_path,
only: clap_config.value_of("only").map(String::from), only: clap_config.get_one::<String>("only").map(String::from),
fix: clap_config.is_present("fix"), fix: clap_config.contains_id("fix"),
lint_filter, lint_filter,
markdown, markdown,
} }