Skip to content

Commit 1be4718

Browse files
committed
Fix lintcheck --fix
There were several issues: - `--fix` was ignored as part of rust-lang#9461 - `--filter` in conjunction to `--fix` was broken due to rust-lang#9703 After `lintcheck --fix` is used, crates will be re-extracted since their content has been modified
1 parent f5d225d commit 1be4718

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

lintcheck/src/main.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,25 @@ impl CrateSource {
188188
create_dirs(&krate_download_dir, &extract_dir);
189189

190190
let krate_file_path = krate_download_dir.join(format!("{name}-{version}.crate.tar.gz"));
191-
// don't download/extract if we already have done so
191+
// don't download if we already have done so
192192
if !krate_file_path.is_file() {
193193
// create a file path to download and write the crate data into
194194
let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
195195
let mut krate_req = get(&url).unwrap().into_reader();
196196
// copy the crate into the file
197197
std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
198+
}
198199

200+
// if the source code was altered (previous use of `--fix`), we need to restore the crate
201+
// to its original state by re-extracting it
202+
if !extracted_krate_dir.exists()
203+
|| extracted_krate_dir.metadata().map_or(false, |metadata| {
204+
metadata.created().map_or(false, |created| {
205+
metadata.modified().map_or(false, |modified| created != modified)
206+
})
207+
})
208+
{
209+
debug!("extracting {} {}", name, version);
199210
// unzip the tarball
200211
let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
201212
// extract the tar archive
@@ -337,7 +348,9 @@ impl Crate {
337348
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
338349

339350
let mut cargo_clippy_args = if config.fix {
340-
vec!["--fix", "--"]
351+
// need to pass `clippy` arg even if already feeding `cargo-clippy`
352+
// see https://github.com/rust-lang/rust-clippy/pull/9461
353+
vec!["clippy", "--fix", "--allow-no-vcs", "--"]
341354
} else {
342355
vec!["--", "--message-format=json", "--"]
343356
};
@@ -347,16 +360,19 @@ impl Crate {
347360
for opt in options {
348361
clippy_args.push(opt);
349362
}
350-
} else {
351-
clippy_args.extend(["-Wclippy::pedantic", "-Wclippy::cargo"]);
352363
}
353364

354-
if lint_filter.is_empty() {
355-
clippy_args.push("--cap-lints=warn");
365+
// cap-lints flag is ignored when using `clippy --fix` for now
366+
// So it needs to be passed directly to rustc
367+
// see https://github.com/rust-lang/rust-clippy/issues/9703
368+
let rustc_flags = if lint_filter.is_empty() {
369+
clippy_args.extend(["-Wclippy::pedantic", "-Wclippy::cargo"]);
370+
"--cap-lints=warn".to_string()
356371
} else {
357-
clippy_args.push("--cap-lints=allow");
358-
clippy_args.extend(lint_filter.iter().map(std::string::String::as_str));
359-
}
372+
let mut lint_filter_buf = lint_filter.clone();
373+
lint_filter_buf.push("--cap-lints=allow".to_string());
374+
lint_filter_buf.join(" ")
375+
};
360376

361377
if let Some(server) = server {
362378
let target = shared_target_dir.join("recursive");
@@ -393,6 +409,7 @@ impl Crate {
393409
let all_output = Command::new(&cargo_clippy_path)
394410
// use the looping index to create individual target dirs
395411
.env("CARGO_TARGET_DIR", shared_target_dir.join(format!("_{thread_index:?}")))
412+
.env("RUSTFLAGS", rustc_flags)
396413
.args(&cargo_clippy_args)
397414
.current_dir(&self.path)
398415
.output()

0 commit comments

Comments
 (0)