Skip to content

Commit e42f79c

Browse files
committed
Add filter option to lintcheck
1 parent 460bef2 commit e42f79c

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

lintcheck/src/main.rs

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ impl Crate {
264264
thread_limit: usize,
265265
total_crates_to_lint: usize,
266266
fix: bool,
267+
lint_filter: &Vec<String>,
267268
) -> Vec<ClippyWarning> {
268269
// advance the atomic index by one
269270
let index = target_dir_index.fetch_add(1, Ordering::SeqCst);
@@ -288,9 +289,9 @@ impl Crate {
288289
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
289290

290291
let mut args = if fix {
291-
vec!["--fix", "--allow-no-vcs", "--", "--cap-lints=warn"]
292+
vec!["--fix", "--allow-no-vcs", "--"]
292293
} else {
293-
vec!["--", "--message-format=json", "--", "--cap-lints=warn"]
294+
vec!["--", "--message-format=json", "--"]
294295
};
295296

296297
if let Some(options) = &self.options {
@@ -301,6 +302,13 @@ impl Crate {
301302
args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
302303
}
303304

305+
if lint_filter.is_empty() {
306+
args.push("--cap-lints=warn");
307+
} else {
308+
args.push("--cap-lints=allow");
309+
args.extend(lint_filter.iter().map(|filter| filter.as_str()))
310+
}
311+
304312
let all_output = std::process::Command::new(&cargo_clippy_path)
305313
// use the looping index to create individual target dirs
306314
.env(
@@ -360,14 +368,16 @@ impl Crate {
360368

361369
#[derive(Debug)]
362370
struct LintcheckConfig {
363-
// max number of jobs to spawn (default 1)
371+
/// max number of jobs to spawn (default 1)
364372
max_jobs: usize,
365-
// we read the sources to check from here
373+
/// we read the sources to check from here
366374
sources_toml_path: PathBuf,
367-
// we save the clippy lint results here
375+
/// we save the clippy lint results here
368376
lintcheck_results_path: PathBuf,
369-
// whether to just run --fix and not collect all the warnings
377+
/// whether to just run --fix and not collect all the warnings
370378
fix: bool,
379+
/// A list of lint that this lintcheck run shound focus on
380+
lint_filter: Vec<String>,
371381
}
372382

373383
impl LintcheckConfig {
@@ -410,12 +420,26 @@ impl LintcheckConfig {
410420
None => 1,
411421
};
412422
let fix: bool = clap_config.is_present("fix");
423+
let lint_filter: Vec<String> = clap_config
424+
.values_of("filter")
425+
.map(|iter| {
426+
iter.map(|lint_name| {
427+
let mut filter = lint_name.replace('_', "-");
428+
if !filter.starts_with("clippy::") {
429+
filter.insert_str(0, "clippy::");
430+
}
431+
filter
432+
})
433+
.collect()
434+
})
435+
.unwrap_or_default();
413436

414437
LintcheckConfig {
415438
max_jobs,
416439
sources_toml_path,
417440
lintcheck_results_path,
418441
fix,
442+
lint_filter,
419443
}
420444
}
421445
}
@@ -682,6 +706,15 @@ pub fn main() {
682706
let old_stats = read_stats_from_file(&config.lintcheck_results_path);
683707

684708
let counter = AtomicUsize::new(1);
709+
let lint_filter: Vec<String> = config
710+
.lint_filter
711+
.iter()
712+
.map(|filter| {
713+
let mut filter = filter.clone();
714+
filter.insert_str(0, "--force-warn=");
715+
filter
716+
})
717+
.collect();
685718

686719
let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
687720
// if we don't have the specified crate in the .toml, throw an error
@@ -705,7 +738,9 @@ pub fn main() {
705738
.into_iter()
706739
.map(|krate| krate.download_and_extract())
707740
.filter(|krate| krate.name == only_one_crate)
708-
.flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix))
741+
.flat_map(|krate| {
742+
krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix, &lint_filter)
743+
})
709744
.collect()
710745
} else {
711746
if config.max_jobs > 1 {
@@ -729,7 +764,14 @@ pub fn main() {
729764
.into_par_iter()
730765
.map(|krate| krate.download_and_extract())
731766
.flat_map(|krate| {
732-
krate.run_clippy_lints(&cargo_clippy_path, &counter, num_cpus, num_crates, config.fix)
767+
krate.run_clippy_lints(
768+
&cargo_clippy_path,
769+
&counter,
770+
num_cpus,
771+
num_crates,
772+
config.fix,
773+
&lint_filter,
774+
)
733775
})
734776
.collect()
735777
} else {
@@ -738,7 +780,9 @@ pub fn main() {
738780
crates
739781
.into_iter()
740782
.map(|krate| krate.download_and_extract())
741-
.flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix))
783+
.flat_map(|krate| {
784+
krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix, &lint_filter)
785+
})
742786
.collect()
743787
}
744788
};
@@ -774,7 +818,7 @@ pub fn main() {
774818
std::fs::create_dir_all(config.lintcheck_results_path.parent().unwrap()).unwrap();
775819
write(&config.lintcheck_results_path, text).unwrap();
776820

777-
print_stats(old_stats, new_stats);
821+
print_stats(old_stats, new_stats, &config.lint_filter);
778822
}
779823

780824
/// read the previous stats from the lintcheck-log file
@@ -807,7 +851,7 @@ fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
807851
}
808852

809853
/// print how lint counts changed between runs
810-
fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>) {
854+
fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>, lint_filter: &Vec<String>) {
811855
let same_in_both_hashmaps = old_stats
812856
.iter()
813857
.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
846890
old_stats_deduped
847891
.iter()
848892
.filter(|(old_key, _)| new_stats_deduped.get::<&String>(&old_key).is_none())
893+
.filter(|(old_key, _)| lint_filter.is_empty() || lint_filter.contains(old_key))
849894
.for_each(|(old_key, old_value)| {
850895
println!("{} {} => 0", old_key, old_value);
851896
});
@@ -904,6 +949,14 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
904949
.long("--fix")
905950
.help("runs cargo clippy --fix and checks if all suggestions apply"),
906951
)
952+
.arg(
953+
Arg::with_name("filter")
954+
.long("--filter")
955+
.takes_value(true)
956+
.multiple(true)
957+
.value_name("clippy_lint_name")
958+
.help("apply a filter to only collect specified lints, this also overrides `allow` attributes"),
959+
)
907960
.get_matches()
908961
}
909962

0 commit comments

Comments
 (0)