Skip to content

Commit 2a28ea0

Browse files
committed
Add command line options option to lintcheck crates config
1 parent e2753f9 commit 2a28ea0

File tree

2 files changed

+79
-36
lines changed

2 files changed

+79
-36
lines changed

clippy_dev/README.md

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Clippy Dev Tool
1+
# Clippy Dev Tool
22

33
The Clippy Dev Tool is a tool to ease Clippy development, similar to `rustc`s `x.py`.
44

@@ -28,25 +28,39 @@ The results will then be saved to `lintcheck-logs/custom_logs.toml`.
2828

2929
### Configuring the Crate Sources
3030

31-
The sources to check are saved in a `toml` file.
32-
There are three types of sources.
33-
A crates-io source:
34-
````toml
35-
bitflags = {name = "bitflags", versions = ['1.2.1']}
36-
````
37-
Requires a "name" and one or multiple "versions" to be checked.
31+
The sources to check are saved in a `toml` file.
32+
There are three types of sources.
3833

39-
A git source:
40-
````toml
41-
puffin = {name = "puffin", git_url = "https://github.com/EmbarkStudios/puffin", git_hash = "02dd4a3"}
42-
````
43-
Requires a name, the url to the repo and unique identifier of a commit,
44-
branch or tag which is checked out before linting.
45-
There is no way to always check `HEAD` because that would lead to changing lint-results as the repo would get updated.
46-
If `git_url` or `git_hash` is missing, an error will be thrown.
47-
48-
A local dependency:
49-
````toml
50-
clippy = {name = "clippy", path = "/home/user/clippy"}
51-
````
52-
For when you want to add a repository that is not published yet.
34+
1. Crates-io Source
35+
36+
````toml
37+
bitflags = {name = "bitflags", versions = ['1.2.1']}
38+
````
39+
Requires a "name" and one or multiple "versions" to be checked.
40+
41+
2. `git` Source
42+
````toml
43+
puffin = {name = "puffin", git_url = "https://github.com/EmbarkStudios/puffin", git_hash = "02dd4a3"}
44+
````
45+
Requires a name, the url to the repo and unique identifier of a commit,
46+
branch or tag which is checked out before linting.
47+
There is no way to always check `HEAD` because that would lead to changing lint-results as the repo would get updated.
48+
If `git_url` or `git_hash` is missing, an error will be thrown.
49+
50+
3. Local Dependency
51+
````toml
52+
clippy = {name = "clippy", path = "/home/user/clippy"}
53+
````
54+
For when you want to add a repository that is not published yet.
55+
56+
#### Command Line Options (optional)
57+
58+
```toml
59+
bitflags = {name = "bitflags", versions = ['1.2.1'], options = ['-Wclippy::pedantic', '-Wclippy::cargo']}
60+
```
61+
62+
It is possible to specify command line options for each crate. This makes it
63+
possible to only check a crate for certain lint groups. If no options are
64+
specified, the lint groups `clippy::all`, `clippy::pedantic`, and
65+
`clippy::cargo` are checked. If an empty array is specified only `clippy::all`
66+
is checked.

clippy_dev/src/lintcheck.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,29 @@ struct TomlCrate {
3232
git_url: Option<String>,
3333
git_hash: Option<String>,
3434
path: Option<String>,
35+
options: Option<Vec<String>>,
3536
}
3637

3738
/// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
3839
/// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
3940
#[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
4041
enum CrateSource {
41-
CratesIo { name: String, version: String },
42-
Git { name: String, url: String, commit: String },
43-
Path { name: String, path: PathBuf },
42+
CratesIo {
43+
name: String,
44+
version: String,
45+
options: Option<Vec<String>>,
46+
},
47+
Git {
48+
name: String,
49+
url: String,
50+
commit: String,
51+
options: Option<Vec<String>>,
52+
},
53+
Path {
54+
name: String,
55+
path: PathBuf,
56+
options: Option<Vec<String>>,
57+
},
4458
}
4559

4660
/// Represents the actual source code of a crate that we ran "cargo clippy" on
@@ -50,6 +64,7 @@ struct Crate {
5064
name: String,
5165
// path to the extracted sources that clippy can check
5266
path: PathBuf,
67+
options: Option<Vec<String>>,
5368
}
5469

5570
/// A single warning that clippy issued while checking a `Crate`
@@ -81,7 +96,7 @@ impl CrateSource {
8196
/// copies a local folder
8297
fn download_and_extract(&self) -> Crate {
8398
match self {
84-
CrateSource::CratesIo { name, version } => {
99+
CrateSource::CratesIo { name, version, options } => {
85100
let extract_dir = PathBuf::from("target/lintcheck/crates");
86101
let krate_download_dir = PathBuf::from("target/lintcheck/downloads");
87102

@@ -113,9 +128,15 @@ impl CrateSource {
113128
version: version.clone(),
114129
name: name.clone(),
115130
path: extract_dir.join(format!("{}-{}/", name, version)),
131+
options: options.clone(),
116132
}
117133
},
118-
CrateSource::Git { name, url, commit } => {
134+
CrateSource::Git {
135+
name,
136+
url,
137+
commit,
138+
options,
139+
} => {
119140
let repo_path = {
120141
let mut repo_path = PathBuf::from("target/lintcheck/crates");
121142
// add a -git suffix in case we have the same crate from crates.io and a git repo
@@ -152,9 +173,10 @@ impl CrateSource {
152173
version: commit.clone(),
153174
name: name.clone(),
154175
path: repo_path,
176+
options: options.clone(),
155177
}
156178
},
157-
CrateSource::Path { name, path } => {
179+
CrateSource::Path { name, path, options } => {
158180
use fs_extra::dir;
159181

160182
// simply copy the entire directory into our target dir
@@ -183,6 +205,7 @@ impl CrateSource {
183205
version: String::from("local"),
184206
name: name.clone(),
185207
path: crate_root,
208+
options: options.clone(),
186209
}
187210
},
188211
}
@@ -198,18 +221,21 @@ impl Crate {
198221

199222
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir/");
200223

224+
let mut args = vec!["--", "--message-format=json", "--", "--cap-lints=warn"];
225+
226+
if let Some(options) = &self.options {
227+
for opt in options {
228+
args.push(opt);
229+
}
230+
} else {
231+
args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
232+
}
233+
201234
let all_output = std::process::Command::new(&cargo_clippy_path)
202235
.env("CARGO_TARGET_DIR", shared_target_dir)
203236
// lint warnings will look like this:
204237
// src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
205-
.args(&[
206-
"--",
207-
"--message-format=json",
208-
"--",
209-
"--cap-lints=warn",
210-
"-Wclippy::pedantic",
211-
"-Wclippy::cargo",
212-
])
238+
.args(&args)
213239
.current_dir(&self.path)
214240
.output()
215241
.unwrap_or_else(|error| {
@@ -289,6 +315,7 @@ fn read_crates(toml_path: Option<&str>) -> (String, Vec<CrateSource>) {
289315
crate_sources.push(CrateSource::Path {
290316
name: tk.name.clone(),
291317
path: PathBuf::from(path),
318+
options: tk.options.clone(),
292319
});
293320
}
294321

@@ -298,6 +325,7 @@ fn read_crates(toml_path: Option<&str>) -> (String, Vec<CrateSource>) {
298325
crate_sources.push(CrateSource::CratesIo {
299326
name: tk.name.clone(),
300327
version: ver.to_string(),
328+
options: tk.options.clone(),
301329
});
302330
})
303331
}
@@ -307,6 +335,7 @@ fn read_crates(toml_path: Option<&str>) -> (String, Vec<CrateSource>) {
307335
name: tk.name.clone(),
308336
url: tk.git_url.clone().unwrap(),
309337
commit: tk.git_hash.clone().unwrap(),
338+
options: tk.options.clone(),
310339
});
311340
}
312341
// if we have a version as well as a git data OR only one git data, something is funky

0 commit comments

Comments
 (0)