Skip to content

Commit 600816f

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

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

lintcheck/src/main.rs

+28-10
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,33 @@ impl CrateSource {
181181
CrateSource::CratesIo { name, version, options } => {
182182
let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
183183
let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
184+
let extracted_krate_dir = extract_dir.join(format!("{name}-{version}/"));
184185

185186
// url to download the crate from crates.io
186187
let url = format!("https://crates.io/api/v1/crates/{name}/{version}/download");
187188
println!("Downloading and extracting {name} {version} from {url}");
188189
create_dirs(&krate_download_dir, &extract_dir);
189190

190191
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
192+
// don't download if we already have done so
192193
if !krate_file_path.is_file() {
193194
// create a file path to download and write the crate data into
194195
let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
195196
let mut krate_req = get(&url).unwrap().into_reader();
196197
// copy the crate into the file
197198
std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
199+
}
198200

201+
// if the source code was altered (previous use of `--fix`), we need to restore the crate
202+
// to its original state by re-extracting it
203+
if !extracted_krate_dir.exists()
204+
|| extracted_krate_dir.metadata().map_or(false, |metadata| {
205+
metadata.created().map_or(false, |created| {
206+
metadata.modified().map_or(false, |modified| created != modified)
207+
})
208+
})
209+
{
210+
debug!("extracting {} {}", name, version);
199211
// unzip the tarball
200212
let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
201213
// extract the tar archive
@@ -207,7 +219,7 @@ impl CrateSource {
207219
Crate {
208220
version: version.clone(),
209221
name: name.clone(),
210-
path: extract_dir.join(format!("{name}-{version}/")),
222+
path: extracted_krate_dir,
211223
options: options.clone(),
212224
}
213225
},
@@ -337,7 +349,9 @@ impl Crate {
337349
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
338350

339351
let mut cargo_clippy_args = if config.fix {
340-
vec!["--fix", "--"]
352+
// need to pass `clippy` arg even if already feeding `cargo-clippy`
353+
// see https://github.com/rust-lang/rust-clippy/pull/9461
354+
vec!["clippy", "--fix", "--allow-no-vcs", "--"]
341355
} else {
342356
vec!["--", "--message-format=json", "--"]
343357
};
@@ -347,16 +361,19 @@ impl Crate {
347361
for opt in options {
348362
clippy_args.push(opt);
349363
}
350-
} else {
351-
clippy_args.extend(["-Wclippy::pedantic", "-Wclippy::cargo"]);
352364
}
353365

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

361378
if let Some(server) = server {
362379
let target = shared_target_dir.join("recursive");
@@ -393,6 +410,7 @@ impl Crate {
393410
let all_output = Command::new(&cargo_clippy_path)
394411
// use the looping index to create individual target dirs
395412
.env("CARGO_TARGET_DIR", shared_target_dir.join(format!("_{thread_index:?}")))
413+
.env("RUSTFLAGS", rustc_flags)
396414
.args(&cargo_clippy_args)
397415
.current_dir(&self.path)
398416
.output()

0 commit comments

Comments
 (0)