Skip to content

Commit 8708a26

Browse files
committed
Some lintcheck cleanup
1 parent 43756b6 commit 8708a26

File tree

4 files changed

+263
-352
lines changed

4 files changed

+263
-352
lines changed

lintcheck/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ readme = "README.md"
66
license = "MIT OR Apache-2.0"
77
repository = "https://github.com/rust-lang/rust-clippy"
88
categories = ["development-tools"]
9-
edition = "2018"
9+
edition = "2021"
1010
publish = false
1111

1212
[dependencies]
13+
cargo_metadata = "0.14"
1314
clap = "2.33"
1415
flate2 = "1.0"
15-
fs_extra = "1.2"
1616
rayon = "1.5.1"
1717
serde = { version = "1.0", features = ["derive"] }
18-
serde_json = "1.0"
1918
tar = "0.4"
2019
toml = "0.5"
2120
ureq = "2.2"

lintcheck/lintcheck_crates.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ unicode-xid = {name = "unicode-xid", versions = ['0.2.1']}
2424
anyhow = {name = "anyhow", versions = ['1.0.38']}
2525
async-trait = {name = "async-trait", versions = ['0.1.42']}
2626
cxx = {name = "cxx", versions = ['1.0.32']}
27-
ryu = {name = "ryu", version = ['1.0.5']}
27+
ryu = {name = "ryu", versions = ['1.0.5']}
2828
serde_yaml = {name = "serde_yaml", versions = ['0.8.17']}
2929
thiserror = {name = "thiserror", versions = ['1.0.24']}
3030
# some embark crates, there are other interesting crates but

lintcheck/src/config.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use clap::{App, Arg, ArgMatches};
2+
use std::env;
3+
use std::path::PathBuf;
4+
5+
fn get_clap_config<'a>() -> ArgMatches<'a> {
6+
App::new("lintcheck")
7+
.about("run clippy on a set of crates and check output")
8+
.arg(
9+
Arg::with_name("only")
10+
.takes_value(true)
11+
.value_name("CRATE")
12+
.long("only")
13+
.help("Only process a single crate of the list"),
14+
)
15+
.arg(
16+
Arg::with_name("crates-toml")
17+
.takes_value(true)
18+
.value_name("CRATES-SOURCES-TOML-PATH")
19+
.long("crates-toml")
20+
.help("Set the path for a crates.toml where lintcheck should read the sources from"),
21+
)
22+
.arg(
23+
Arg::with_name("threads")
24+
.takes_value(true)
25+
.value_name("N")
26+
.short("j")
27+
.long("jobs")
28+
.help("Number of threads to use, 0 automatic choice"),
29+
)
30+
.arg(
31+
Arg::with_name("fix")
32+
.long("--fix")
33+
.help("Runs cargo clippy --fix and checks if all suggestions apply"),
34+
)
35+
.arg(
36+
Arg::with_name("filter")
37+
.long("--filter")
38+
.takes_value(true)
39+
.multiple(true)
40+
.value_name("clippy_lint_name")
41+
.help("Apply a filter to only collect specified lints, this also overrides `allow` attributes"),
42+
)
43+
.arg(
44+
Arg::with_name("markdown")
45+
.long("--markdown")
46+
.help("Change the reports table to use markdown links"),
47+
)
48+
.get_matches()
49+
}
50+
51+
#[derive(Debug)]
52+
pub(crate) struct LintcheckConfig {
53+
/// max number of jobs to spawn (default 1)
54+
pub max_jobs: usize,
55+
/// we read the sources to check from here
56+
pub sources_toml_path: PathBuf,
57+
/// we save the clippy lint results here
58+
pub lintcheck_results_path: PathBuf,
59+
/// Check only a specified package
60+
pub only: Option<String>,
61+
/// whether to just run --fix and not collect all the warnings
62+
pub fix: bool,
63+
/// A list of lints that this lintcheck run should focus on
64+
pub lint_filter: Vec<String>,
65+
/// Indicate if the output should support markdown syntax
66+
pub markdown: bool,
67+
}
68+
69+
impl LintcheckConfig {
70+
pub fn new() -> Self {
71+
let clap_config = get_clap_config();
72+
73+
// first, check if we got anything passed via the LINTCHECK_TOML env var,
74+
// if not, ask clap if we got any value for --crates-toml <foo>
75+
// if not, use the default "lintcheck/lintcheck_crates.toml"
76+
let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| {
77+
clap_config
78+
.value_of("crates-toml")
79+
.clone()
80+
.unwrap_or("lintcheck/lintcheck_crates.toml")
81+
.to_string()
82+
});
83+
84+
let markdown = clap_config.is_present("markdown");
85+
let sources_toml_path = PathBuf::from(sources_toml);
86+
87+
// for the path where we save the lint results, get the filename without extension (so for
88+
// wasd.toml, use "wasd"...)
89+
let filename: PathBuf = sources_toml_path.file_stem().unwrap().into();
90+
let lintcheck_results_path = PathBuf::from(format!(
91+
"lintcheck-logs/{}_logs.{}",
92+
filename.display(),
93+
if markdown { "md" } else { "txt" }
94+
));
95+
96+
// look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and
97+
// use half of that for the physical core count
98+
// by default use a single thread
99+
let max_jobs = match clap_config.value_of("threads") {
100+
Some(threads) => {
101+
let threads: usize = threads
102+
.parse()
103+
.unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads));
104+
if threads == 0 {
105+
// automatic choice
106+
// Rayon seems to return thread count so half that for core count
107+
(rayon::current_num_threads() / 2) as usize
108+
} else {
109+
threads
110+
}
111+
},
112+
// no -j passed, use a single thread
113+
None => 1,
114+
};
115+
116+
let lint_filter: Vec<String> = clap_config
117+
.values_of("filter")
118+
.map(|iter| {
119+
iter.map(|lint_name| {
120+
let mut filter = lint_name.replace('_', "-");
121+
if !filter.starts_with("clippy::") {
122+
filter.insert_str(0, "clippy::");
123+
}
124+
filter
125+
})
126+
.collect()
127+
})
128+
.unwrap_or_default();
129+
130+
LintcheckConfig {
131+
max_jobs,
132+
sources_toml_path,
133+
lintcheck_results_path,
134+
only: clap_config.value_of("only").map(String::from),
135+
fix: clap_config.is_present("fix"),
136+
lint_filter,
137+
markdown,
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)