mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
Add filter
option to lintcheck
This commit is contained in:
parent
460bef22a3
commit
e42f79c0a3
1 changed files with 64 additions and 11 deletions
|
@ -264,6 +264,7 @@ impl Crate {
|
||||||
thread_limit: usize,
|
thread_limit: usize,
|
||||||
total_crates_to_lint: usize,
|
total_crates_to_lint: usize,
|
||||||
fix: bool,
|
fix: bool,
|
||||||
|
lint_filter: &Vec<String>,
|
||||||
) -> Vec<ClippyWarning> {
|
) -> Vec<ClippyWarning> {
|
||||||
// advance the atomic index by one
|
// advance the atomic index by one
|
||||||
let index = target_dir_index.fetch_add(1, Ordering::SeqCst);
|
let index = target_dir_index.fetch_add(1, Ordering::SeqCst);
|
||||||
|
@ -288,9 +289,9 @@ impl Crate {
|
||||||
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
|
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
|
||||||
|
|
||||||
let mut args = if fix {
|
let mut args = if fix {
|
||||||
vec!["--fix", "--allow-no-vcs", "--", "--cap-lints=warn"]
|
vec!["--fix", "--allow-no-vcs", "--"]
|
||||||
} else {
|
} else {
|
||||||
vec!["--", "--message-format=json", "--", "--cap-lints=warn"]
|
vec!["--", "--message-format=json", "--"]
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(options) = &self.options {
|
if let Some(options) = &self.options {
|
||||||
|
@ -301,6 +302,13 @@ impl Crate {
|
||||||
args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
|
args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if lint_filter.is_empty() {
|
||||||
|
args.push("--cap-lints=warn");
|
||||||
|
} else {
|
||||||
|
args.push("--cap-lints=allow");
|
||||||
|
args.extend(lint_filter.iter().map(|filter| filter.as_str()))
|
||||||
|
}
|
||||||
|
|
||||||
let all_output = std::process::Command::new(&cargo_clippy_path)
|
let all_output = std::process::Command::new(&cargo_clippy_path)
|
||||||
// use the looping index to create individual target dirs
|
// use the looping index to create individual target dirs
|
||||||
.env(
|
.env(
|
||||||
|
@ -360,14 +368,16 @@ impl Crate {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct LintcheckConfig {
|
struct LintcheckConfig {
|
||||||
// max number of jobs to spawn (default 1)
|
/// max number of jobs to spawn (default 1)
|
||||||
max_jobs: usize,
|
max_jobs: usize,
|
||||||
// we read the sources to check from here
|
/// we read the sources to check from here
|
||||||
sources_toml_path: PathBuf,
|
sources_toml_path: PathBuf,
|
||||||
// we save the clippy lint results here
|
/// we save the clippy lint results here
|
||||||
lintcheck_results_path: PathBuf,
|
lintcheck_results_path: PathBuf,
|
||||||
// whether to just run --fix and not collect all the warnings
|
/// whether to just run --fix and not collect all the warnings
|
||||||
fix: bool,
|
fix: bool,
|
||||||
|
/// A list of lint that this lintcheck run shound focus on
|
||||||
|
lint_filter: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LintcheckConfig {
|
impl LintcheckConfig {
|
||||||
|
@ -410,12 +420,26 @@ impl LintcheckConfig {
|
||||||
None => 1,
|
None => 1,
|
||||||
};
|
};
|
||||||
let fix: bool = clap_config.is_present("fix");
|
let fix: bool = clap_config.is_present("fix");
|
||||||
|
let lint_filter: Vec<String> = clap_config
|
||||||
|
.values_of("filter")
|
||||||
|
.map(|iter| {
|
||||||
|
iter.map(|lint_name| {
|
||||||
|
let mut filter = lint_name.replace('_', "-");
|
||||||
|
if !filter.starts_with("clippy::") {
|
||||||
|
filter.insert_str(0, "clippy::");
|
||||||
|
}
|
||||||
|
filter
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
LintcheckConfig {
|
LintcheckConfig {
|
||||||
max_jobs,
|
max_jobs,
|
||||||
sources_toml_path,
|
sources_toml_path,
|
||||||
lintcheck_results_path,
|
lintcheck_results_path,
|
||||||
fix,
|
fix,
|
||||||
|
lint_filter,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -682,6 +706,15 @@ pub fn main() {
|
||||||
let old_stats = read_stats_from_file(&config.lintcheck_results_path);
|
let old_stats = read_stats_from_file(&config.lintcheck_results_path);
|
||||||
|
|
||||||
let counter = AtomicUsize::new(1);
|
let counter = AtomicUsize::new(1);
|
||||||
|
let lint_filter: Vec<String> = config
|
||||||
|
.lint_filter
|
||||||
|
.iter()
|
||||||
|
.map(|filter| {
|
||||||
|
let mut filter = filter.clone();
|
||||||
|
filter.insert_str(0, "--force-warn=");
|
||||||
|
filter
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
|
let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
|
||||||
// if we don't have the specified crate in the .toml, throw an error
|
// if we don't have the specified crate in the .toml, throw an error
|
||||||
|
@ -705,7 +738,9 @@ pub fn main() {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|krate| krate.download_and_extract())
|
.map(|krate| krate.download_and_extract())
|
||||||
.filter(|krate| krate.name == only_one_crate)
|
.filter(|krate| krate.name == only_one_crate)
|
||||||
.flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix))
|
.flat_map(|krate| {
|
||||||
|
krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix, &lint_filter)
|
||||||
|
})
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
if config.max_jobs > 1 {
|
if config.max_jobs > 1 {
|
||||||
|
@ -729,7 +764,14 @@ pub fn main() {
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
.map(|krate| krate.download_and_extract())
|
.map(|krate| krate.download_and_extract())
|
||||||
.flat_map(|krate| {
|
.flat_map(|krate| {
|
||||||
krate.run_clippy_lints(&cargo_clippy_path, &counter, num_cpus, num_crates, config.fix)
|
krate.run_clippy_lints(
|
||||||
|
&cargo_clippy_path,
|
||||||
|
&counter,
|
||||||
|
num_cpus,
|
||||||
|
num_crates,
|
||||||
|
config.fix,
|
||||||
|
&lint_filter,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
|
@ -738,7 +780,9 @@ pub fn main() {
|
||||||
crates
|
crates
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|krate| krate.download_and_extract())
|
.map(|krate| krate.download_and_extract())
|
||||||
.flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix))
|
.flat_map(|krate| {
|
||||||
|
krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix, &lint_filter)
|
||||||
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -774,7 +818,7 @@ pub fn main() {
|
||||||
std::fs::create_dir_all(config.lintcheck_results_path.parent().unwrap()).unwrap();
|
std::fs::create_dir_all(config.lintcheck_results_path.parent().unwrap()).unwrap();
|
||||||
write(&config.lintcheck_results_path, text).unwrap();
|
write(&config.lintcheck_results_path, text).unwrap();
|
||||||
|
|
||||||
print_stats(old_stats, new_stats);
|
print_stats(old_stats, new_stats, &config.lint_filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// read the previous stats from the lintcheck-log file
|
/// read the previous stats from the lintcheck-log file
|
||||||
|
@ -807,7 +851,7 @@ fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// print how lint counts changed between runs
|
/// print how lint counts changed between runs
|
||||||
fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>) {
|
fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>, lint_filter: &Vec<String>) {
|
||||||
let same_in_both_hashmaps = old_stats
|
let same_in_both_hashmaps = old_stats
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(old_key, old_val)| new_stats.get::<&String>(&old_key) == Some(old_val))
|
.filter(|(old_key, old_val)| new_stats.get::<&String>(&old_key) == Some(old_val))
|
||||||
|
@ -846,6 +890,7 @@ fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, us
|
||||||
old_stats_deduped
|
old_stats_deduped
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(old_key, _)| new_stats_deduped.get::<&String>(&old_key).is_none())
|
.filter(|(old_key, _)| new_stats_deduped.get::<&String>(&old_key).is_none())
|
||||||
|
.filter(|(old_key, _)| lint_filter.is_empty() || lint_filter.contains(old_key))
|
||||||
.for_each(|(old_key, old_value)| {
|
.for_each(|(old_key, old_value)| {
|
||||||
println!("{} {} => 0", old_key, old_value);
|
println!("{} {} => 0", old_key, old_value);
|
||||||
});
|
});
|
||||||
|
@ -904,6 +949,14 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
|
||||||
.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::with_name("filter")
|
||||||
|
.long("--filter")
|
||||||
|
.takes_value(true)
|
||||||
|
.multiple(true)
|
||||||
|
.value_name("clippy_lint_name")
|
||||||
|
.help("apply a filter to only collect specified lints, this also overrides `allow` attributes"),
|
||||||
|
)
|
||||||
.get_matches()
|
.get_matches()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue