Skip to content

Commit 12f0c8a

Browse files
committed
fix: Make auto-fix note work with clippy
1 parent 6553589 commit 12f0c8a

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

src/cargo/core/compiler/job_queue.rs

Lines changed: 28 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;
@@ -769,7 +770,11 @@ impl<'cfg> DrainState<'cfg> {
769770
self.tokens.extend(rustc_tokens);
770771
}
771772
self.to_send_clients.remove(&id);
772-
self.report_warning_count(cx.bcx.config, id);
773+
self.report_warning_count(
774+
cx.bcx.config,
775+
id,
776+
&cx.bcx.rustc().workspace_wrapper,
777+
);
773778
self.active.remove(&id).unwrap()
774779
}
775780
// ... otherwise if it hasn't finished we leave it
@@ -1206,7 +1211,12 @@ impl<'cfg> DrainState<'cfg> {
12061211
}
12071212

12081213
/// Displays a final report of the warnings emitted by a particular job.
1209-
fn report_warning_count(&mut self, config: &Config, id: JobId) {
1214+
fn report_warning_count(
1215+
&mut self,
1216+
config: &Config,
1217+
id: JobId,
1218+
rustc_workspace_wrapper: &Option<PathBuf>,
1219+
) {
12101220
let count = match self.warning_count.remove(&id) {
12111221
// An error could add an entry for a `Unit`
12121222
// with 0 warnings but having fixable
@@ -1239,7 +1249,19 @@ impl<'cfg> DrainState<'cfg> {
12391249
if let FixableWarnings::Positive(fixable) = count.fixable {
12401250
// `cargo fix` doesnt have an option for custom builds
12411251
if !unit.target.is_custom_build() {
1242-
let mut command = {
1252+
// To make sure the correct command is shown for `clippy` we
1253+
// check if `RUSTC_WORKSPACE_WRAPPER` is set and pointing towards
1254+
// `clippy-driver`.
1255+
let command = match rustc_workspace_wrapper {
1256+
#[cfg(windows)]
1257+
Some(wrapper) if wrapper.ends_with("clippy-driver.exe") => {
1258+
"cargo clippy --fix"
1259+
}
1260+
#[cfg(not(windows))]
1261+
Some(wrapper) if wrapper.ends_with("clippy-driver") => "cargo clippy --fix",
1262+
_ => "cargo fix",
1263+
};
1264+
let mut args = {
12431265
let named = unit.target.description_named();
12441266
// if its a lib we need to add the package to fix
12451267
if unit.target.is_lib() {
@@ -1251,16 +1273,16 @@ impl<'cfg> DrainState<'cfg> {
12511273
if unit.mode.is_rustc_test()
12521274
&& !(unit.target.is_test() || unit.target.is_bench())
12531275
{
1254-
command.push_str(" --tests");
1276+
args.push_str(" --tests");
12551277
}
12561278
let mut suggestions = format!("{} suggestion", fixable);
12571279
if fixable > 1 {
12581280
suggestions.push_str("s")
12591281
}
12601282
drop(write!(
12611283
message,
1262-
" (run `cargo fix --{}` to apply {})",
1263-
command, suggestions
1284+
" (run `{} --{}` to apply {})",
1285+
command, args, suggestions
12641286
))
12651287
}
12661288
}

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)