Skip to content

Commit 8607003

Browse files
committed
Auto merge of #11399 - Muscraft:autofix-for-clippy, r=weihanglo
fix: Make auto-fix note work with `clippy` [Someone pointed out](#10976 (comment)) that the suggestion to run `cargo --fix` did not work properly for `cargo clippy`. This makes sure the correct command to run is output when running under `cargo clippy`. This is done by checking if the `RUSTC_WORKSPACE_WRAPPER` environment variable is set, since `clippy` [sets this every run](https://github.com/rust-lang/rust-clippy/blob/51ec465cc3dfb62a2dad7e808b399fa76a1c9170/src/main.rs#L140). Since other things can use `RUSTC_WORKSPACE_WRAPPER`, we look for a wrapper named [`clippy-driver`](https://github.com/rust-lang/rust-clippy/blob/51ec465cc3dfb62a2dad7e808b399fa76a1c9170/src/main.rs#L120). Since `clippy` might not be available everywhere `cargo` is tested, this is tested using a `rustc` wrapper.
2 parents a56fedc + 33c3208 commit 8607003

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/cargo/core/compiler/job_queue.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use std::collections::{BTreeMap, HashMap, HashSet};
5454
use std::fmt::Write as _;
5555
use std::io;
5656
use std::marker;
57+
use std::path::PathBuf;
5758
use std::sync::Arc;
5859
use std::thread::{self, Scope};
5960
use std::time::Duration;
@@ -799,7 +800,11 @@ impl<'cfg> DrainState<'cfg> {
799800
self.tokens.extend(rustc_tokens);
800801
}
801802
self.to_send_clients.remove(&id);
802-
self.report_warning_count(cx.bcx.config, id);
803+
self.report_warning_count(
804+
cx.bcx.config,
805+
id,
806+
&cx.bcx.rustc().workspace_wrapper,
807+
);
803808
self.active.remove(&id).unwrap()
804809
}
805810
// ... otherwise if it hasn't finished we leave it
@@ -1243,7 +1248,12 @@ impl<'cfg> DrainState<'cfg> {
12431248
}
12441249

12451250
/// Displays a final report of the warnings emitted by a particular job.
1246-
fn report_warning_count(&mut self, config: &Config, id: JobId) {
1251+
fn report_warning_count(
1252+
&mut self,
1253+
config: &Config,
1254+
id: JobId,
1255+
rustc_workspace_wrapper: &Option<PathBuf>,
1256+
) {
12471257
let count = match self.warning_count.remove(&id) {
12481258
// An error could add an entry for a `Unit`
12491259
// with 0 warnings but having fixable
@@ -1276,7 +1286,16 @@ impl<'cfg> DrainState<'cfg> {
12761286
if let FixableWarnings::Positive(fixable) = count.fixable {
12771287
// `cargo fix` doesnt have an option for custom builds
12781288
if !unit.target.is_custom_build() {
1279-
let mut command = {
1289+
// To make sure the correct command is shown for `clippy` we
1290+
// check if `RUSTC_WORKSPACE_WRAPPER` is set and pointing towards
1291+
// `clippy-driver`.
1292+
let clippy = std::ffi::OsStr::new("clippy-driver");
1293+
let command = match rustc_workspace_wrapper.as_ref().and_then(|x| x.file_stem())
1294+
{
1295+
Some(wrapper) if wrapper == clippy => "cargo clippy --fix",
1296+
_ => "cargo fix",
1297+
};
1298+
let mut args = {
12801299
let named = unit.target.description_named();
12811300
// if its a lib we need to add the package to fix
12821301
if unit.target.is_lib() {
@@ -1288,16 +1307,15 @@ impl<'cfg> DrainState<'cfg> {
12881307
if unit.mode.is_rustc_test()
12891308
&& !(unit.target.is_test() || unit.target.is_bench())
12901309
{
1291-
command.push_str(" --tests");
1310+
args.push_str(" --tests");
12921311
}
12931312
let mut suggestions = format!("{} suggestion", fixable);
12941313
if fixable > 1 {
12951314
suggestions.push_str("s")
12961315
}
12971316
drop(write!(
12981317
message,
1299-
" (run `cargo fix --{}` to apply {})",
1300-
command, suggestions
1318+
" (run `{command} --{args}` to apply {suggestions})"
13011319
))
13021320
}
13031321
}

tests/testsuite/check.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,3 +1447,50 @@ fn check_fixable_mixed() {
14471447
.with_stderr_contains("[..] (run `cargo fix --bench \"bench\"` to apply 1 suggestion)")
14481448
.run();
14491449
}
1450+
1451+
#[cargo_test]
1452+
fn check_fixable_warning_for_clippy() {
1453+
// A wrapper around `rustc` instead of calling `clippy`
1454+
let clippy_driver = project()
1455+
.at(cargo_test_support::paths::global_root().join("clippy-driver"))
1456+
.file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1"))
1457+
.file(
1458+
"src/main.rs",
1459+
r#"
1460+
fn main() {
1461+
let mut args = std::env::args_os();
1462+
let _me = args.next().unwrap();
1463+
let rustc = args.next().unwrap();
1464+
let status = std::process::Command::new(rustc).args(args).status().unwrap();
1465+
std::process::exit(status.code().unwrap_or(1));
1466+
}
1467+
"#,
1468+
)
1469+
.build();
1470+
clippy_driver.cargo("build").run();
1471+
1472+
let foo = project()
1473+
.file(
1474+
"Cargo.toml",
1475+
r#"
1476+
[package]
1477+
name = "foo"
1478+
version = "0.0.1"
1479+
"#,
1480+
)
1481+
// We don't want to show a warning that is `clippy`
1482+
// specific since we are using a `rustc` wrapper
1483+
// inplace of `clippy`
1484+
.file("src/lib.rs", "use std::io;")
1485+
.build();
1486+
1487+
foo.cargo("check")
1488+
// We can't use `clippy` so we use a `rustc` workspace wrapper instead
1489+
.env(
1490+
"RUSTC_WORKSPACE_WRAPPER",
1491+
clippy_driver.bin("clippy-driver"),
1492+
)
1493+
.masquerade_as_nightly_cargo(&["auto-fix note"])
1494+
.with_stderr_contains("[..] (run `cargo clippy --fix --lib -p foo` to apply 1 suggestion)")
1495+
.run();
1496+
}

0 commit comments

Comments
 (0)