Skip to content

Commit

Permalink
fix: Make auto-fix note work with clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Nov 21, 2022
1 parent 6553589 commit e8e9b8b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,14 @@ impl<'cfg> DrainState<'cfg> {
if let FixableWarnings::Positive(fixable) = count.fixable {
// `cargo fix` doesnt have an option for custom builds
if !unit.target.is_custom_build() {
let mut command = {
// To make sure the correct command is shown for `clippy` we
// check if `CLIPPY_ARGS` is set as `clippy` sets this when ran
let command = if config.env().get("CLIPPY_ARGS").is_some() {
"cargo clippy --fix"
} else {
"cargo fix"
};
let mut args = {
let named = unit.target.description_named();
// if its a lib we need to add the package to fix
if unit.target.is_lib() {
Expand All @@ -1251,16 +1258,16 @@ impl<'cfg> DrainState<'cfg> {
if unit.mode.is_rustc_test()
&& !(unit.target.is_test() || unit.target.is_bench())
{
command.push_str(" --tests");
args.push_str(" --tests");
}
let mut suggestions = format!("{} suggestion", fixable);
if fixable > 1 {
suggestions.push_str("s")
}
drop(write!(
message,
" (run `cargo fix --{}` to apply {})",
command, suggestions
" (run `{} --{}` to apply {})",
command, args, suggestions
))
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,33 @@ error: no such command: `bluid`
)
.run();
}

#[cargo_test]
fn check_fixable_warning_for_clippy() {
let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
"#,
)
// We don't want to show a warning that is `clippy`
// specific since it could change in the future
.file("src/lib.rs", "use std::io;")
.build();

// To ensure it uses the version of `cargo` we currently
// test against, we prepend build artifacts directory
// to `$PATH`
let cargo = cargo_exe().canonicalize().unwrap();
let mut path = path();
path.insert(0, cargo.parent().unwrap().into());
let path = env::join_paths(path.iter()).unwrap();
foo.cargo("clippy")
.env("PATH", &path)
.masquerade_as_nightly_cargo(&["auto-fix note"])
.with_stderr_contains("[..] (run `cargo clippy --fix --lib -p foo` to apply 1 suggestion)")
.run();
}

0 comments on commit e8e9b8b

Please sign in to comment.