Skip to content

Commit b0742b2

Browse files
committed
Auto merge of #11882 - hi-rustin:rustin-patch-clippy-fix, r=weihanglo
Correct the bug report for `cargo clippy --fix`
2 parents 84b7041 + 08169fd commit b0742b2

File tree

5 files changed

+131
-45
lines changed

5 files changed

+131
-45
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,29 @@ pub fn cargo_exe() -> PathBuf {
517517
snapbox::cmd::cargo_bin("cargo")
518518
}
519519

520+
/// A wrapper around `rustc` instead of calling `clippy`.
521+
pub fn wrapped_clippy_driver() -> PathBuf {
522+
let clippy_driver = project()
523+
.at(paths::global_root().join("clippy-driver"))
524+
.file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1"))
525+
.file(
526+
"src/main.rs",
527+
r#"
528+
fn main() {
529+
let mut args = std::env::args_os();
530+
let _me = args.next().unwrap();
531+
let rustc = args.next().unwrap();
532+
let status = std::process::Command::new(rustc).args(args).status().unwrap();
533+
std::process::exit(status.code().unwrap_or(1));
534+
}
535+
"#,
536+
)
537+
.build();
538+
clippy_driver.cargo("build").run();
539+
540+
clippy_driver.bin("clippy-driver")
541+
}
542+
520543
/// This is the raw output from the process.
521544
///
522545
/// This is similar to `std::process::Output`, however the `status` is

src/cargo/core/compiler/job_queue/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ impl<'cfg> JobQueue<'cfg> {
487487
timings: self.timings,
488488
tokens: Vec::new(),
489489
pending_queue: Vec::new(),
490-
print: DiagnosticPrinter::new(cx.bcx.config),
490+
print: DiagnosticPrinter::new(cx.bcx.config, &cx.bcx.rustc().workspace_wrapper),
491491
finished: 0,
492492
per_package_future_incompat_reports: Vec::new(),
493493
};

src/cargo/util/diagnostic_server.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::collections::HashSet;
55
use std::io::{BufReader, Read, Write};
66
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
7+
use std::path::PathBuf;
78
use std::sync::atomic::{AtomicBool, Ordering};
89
use std::sync::Arc;
910
use std::thread::{self, JoinHandle};
@@ -18,16 +19,6 @@ use crate::util::errors::CargoResult;
1819
use crate::util::Config;
1920

2021
const DIAGNOSTICS_SERVER_VAR: &str = "__CARGO_FIX_DIAGNOSTICS_SERVER";
21-
const PLEASE_REPORT_THIS_BUG: &str =
22-
"This likely indicates a bug in either rustc or cargo itself,\n\
23-
and we would appreciate a bug report! You're likely to see \n\
24-
a number of compiler warnings after this message which cargo\n\
25-
attempted to fix but failed. If you could open an issue at\n\
26-
https://github.com/rust-lang/rust/issues\n\
27-
quoting the full output of this command we'd be very appreciative!\n\
28-
Note that you may be able to make some more progress in the near-term\n\
29-
fixing code with the `--broken-code` flag\n\n\
30-
";
3122

3223
#[derive(Deserialize, Serialize, Hash, Eq, PartialEq, Clone)]
3324
pub enum Message {
@@ -83,15 +74,27 @@ impl Message {
8374
}
8475
}
8576

77+
/// A printer that will print diagnostics messages to the shell.
8678
pub struct DiagnosticPrinter<'a> {
79+
/// The config to get the shell to print to.
8780
config: &'a Config,
81+
/// An optional wrapper to be used in addition to `rustc.wrapper` for workspace crates.
82+
/// This is used to get the correct bug report URL. For instance,
83+
/// if `clippy-driver` is set as the value for the wrapper,
84+
/// then the correct bug report URL for `clippy` can be obtained.
85+
workspace_wrapper: &'a Option<PathBuf>,
86+
// A set of messages that have already been printed.
8887
dedupe: HashSet<Message>,
8988
}
9089

9190
impl<'a> DiagnosticPrinter<'a> {
92-
pub fn new(config: &'a Config) -> DiagnosticPrinter<'a> {
91+
pub fn new(
92+
config: &'a Config,
93+
workspace_wrapper: &'a Option<PathBuf>,
94+
) -> DiagnosticPrinter<'a> {
9395
DiagnosticPrinter {
9496
config,
97+
workspace_wrapper,
9598
dedupe: HashSet::new(),
9699
}
97100
}
@@ -128,7 +131,12 @@ impl<'a> DiagnosticPrinter<'a> {
128131
"The full error message was:\n\n> {}\n\n",
129132
message,
130133
)?;
131-
write!(self.config.shell().err(), "{}", PLEASE_REPORT_THIS_BUG)?;
134+
let issue_link = get_bug_report_url(self.workspace_wrapper);
135+
write!(
136+
self.config.shell().err(),
137+
"{}",
138+
gen_please_report_this_bug_text(issue_link)
139+
)?;
132140
Ok(())
133141
}
134142
Message::FixFailed {
@@ -159,7 +167,12 @@ impl<'a> DiagnosticPrinter<'a> {
159167
}
160168
writeln!(self.config.shell().err())?;
161169
}
162-
write!(self.config.shell().err(), "{}", PLEASE_REPORT_THIS_BUG)?;
170+
let issue_link = get_bug_report_url(self.workspace_wrapper);
171+
write!(
172+
self.config.shell().err(),
173+
"{}",
174+
gen_please_report_this_bug_text(issue_link)
175+
)?;
163176
if !errors.is_empty() {
164177
writeln!(
165178
self.config.shell().err(),
@@ -218,6 +231,31 @@ https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-proje
218231
}
219232
}
220233

234+
fn gen_please_report_this_bug_text(url: &str) -> String {
235+
format!(
236+
"This likely indicates a bug in either rustc or cargo itself,\n\
237+
and we would appreciate a bug report! You're likely to see \n\
238+
a number of compiler warnings after this message which cargo\n\
239+
attempted to fix but failed. If you could open an issue at\n\
240+
{}\n\
241+
quoting the full output of this command we'd be very appreciative!\n\
242+
Note that you may be able to make some more progress in the near-term\n\
243+
fixing code with the `--broken-code` flag\n\n\
244+
",
245+
url
246+
)
247+
}
248+
249+
fn get_bug_report_url(rustc_workspace_wrapper: &Option<PathBuf>) -> &str {
250+
let clippy = std::ffi::OsStr::new("clippy-driver");
251+
let issue_link = match rustc_workspace_wrapper.as_ref().and_then(|x| x.file_stem()) {
252+
Some(wrapper) if wrapper == clippy => "https://github.com/rust-lang/rust-clippy/issues",
253+
_ => "https://github.com/rust-lang/rust/issues",
254+
};
255+
256+
issue_link
257+
}
258+
221259
#[derive(Debug)]
222260
pub struct RustfixDiagnosticServer {
223261
listener: TcpListener,

tests/testsuite/check.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::messages::raw_rustc_output;
66
use cargo_test_support::install::exe;
77
use cargo_test_support::paths::CargoPathExt;
88
use cargo_test_support::registry::Package;
9-
use cargo_test_support::tools;
109
use cargo_test_support::{basic_bin_manifest, basic_manifest, git, project};
10+
use cargo_test_support::{tools, wrapped_clippy_driver};
1111

1212
#[cargo_test]
1313
fn check_success() {
@@ -1416,25 +1416,6 @@ fn check_fixable_mixed() {
14161416

14171417
#[cargo_test]
14181418
fn check_fixable_warning_for_clippy() {
1419-
// A wrapper around `rustc` instead of calling `clippy`
1420-
let clippy_driver = project()
1421-
.at(cargo_test_support::paths::global_root().join("clippy-driver"))
1422-
.file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1"))
1423-
.file(
1424-
"src/main.rs",
1425-
r#"
1426-
fn main() {
1427-
let mut args = std::env::args_os();
1428-
let _me = args.next().unwrap();
1429-
let rustc = args.next().unwrap();
1430-
let status = std::process::Command::new(rustc).args(args).status().unwrap();
1431-
std::process::exit(status.code().unwrap_or(1));
1432-
}
1433-
"#,
1434-
)
1435-
.build();
1436-
clippy_driver.cargo("build").run();
1437-
14381419
let foo = project()
14391420
.file(
14401421
"Cargo.toml",
@@ -1452,10 +1433,7 @@ fn check_fixable_warning_for_clippy() {
14521433

14531434
foo.cargo("check")
14541435
// We can't use `clippy` so we use a `rustc` workspace wrapper instead
1455-
.env(
1456-
"RUSTC_WORKSPACE_WRAPPER",
1457-
clippy_driver.bin("clippy-driver"),
1458-
)
1436+
.env("RUSTC_WORKSPACE_WRAPPER", wrapped_clippy_driver())
14591437
.with_stderr_contains("[..] (run `cargo clippy --fix --lib -p foo` to apply 1 suggestion)")
14601438
.run();
14611439
}

tests/testsuite/fix.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use cargo_test_support::compare::assert_match_exact;
55
use cargo_test_support::git::{self, init};
66
use cargo_test_support::paths::{self, CargoPathExt};
77
use cargo_test_support::registry::{Dependency, Package};
8-
use cargo_test_support::tools;
9-
use cargo_test_support::{basic_manifest, is_nightly, project};
8+
use cargo_test_support::{basic_manifest, is_nightly, project, Project};
9+
use cargo_test_support::{tools, wrapped_clippy_driver};
1010

1111
#[cargo_test]
1212
fn do_not_fix_broken_builds() {
@@ -53,8 +53,7 @@ fn fix_broken_if_requested() {
5353
.run();
5454
}
5555

56-
#[cargo_test]
57-
fn broken_fixes_backed_out() {
56+
fn rustc_shim_for_cargo_fix() -> Project {
5857
// This works as follows:
5958
// - Create a `rustc` shim (the "foo" project) which will pretend that the
6059
// verification step fails.
@@ -109,7 +108,6 @@ fn broken_fixes_backed_out() {
109108
fs::File::create(&first).unwrap();
110109
}
111110
}
112-
113111
let status = Command::new("rustc")
114112
.args(env::args().skip(1))
115113
.status()
@@ -142,11 +140,60 @@ fn broken_fixes_backed_out() {
142140
// Build our rustc shim
143141
p.cargo("build").cwd("foo").run();
144142

145-
// Attempt to fix code, but our shim will always fail the second compile
143+
p
144+
}
145+
146+
#[cargo_test]
147+
fn broken_fixes_backed_out() {
148+
let p = rustc_shim_for_cargo_fix();
149+
// Attempt to fix code, but our shim will always fail the second compile.
150+
p.cargo("fix --allow-no-vcs --lib")
151+
.cwd("bar")
152+
.env("__CARGO_FIX_YOLO", "1")
153+
.env("RUSTC", p.root().join("foo/target/debug/foo"))
154+
.with_stderr_contains(
155+
"warning: failed to automatically apply fixes suggested by rustc \
156+
to crate `bar`\n\
157+
\n\
158+
after fixes were automatically applied the compiler reported \
159+
errors within these files:\n\
160+
\n \
161+
* src/lib.rs\n\
162+
\n\
163+
This likely indicates a bug in either rustc or cargo itself,\n\
164+
and we would appreciate a bug report! You're likely to see \n\
165+
a number of compiler warnings after this message which cargo\n\
166+
attempted to fix but failed. If you could open an issue at\n\
167+
https://github.com/rust-lang/rust/issues\n\
168+
quoting the full output of this command we'd be very appreciative!\n\
169+
Note that you may be able to make some more progress in the near-term\n\
170+
fixing code with the `--broken-code` flag\n\
171+
\n\
172+
The following errors were reported:\n\
173+
error: expected one of `!` or `::`, found `rust`\n\
174+
",
175+
)
176+
.with_stderr_contains("Original diagnostics will follow.")
177+
.with_stderr_contains("[WARNING] variable does not need to be mutable")
178+
.with_stderr_does_not_contain("[..][FIXED][..]")
179+
.run();
180+
181+
// Make sure the fix which should have been applied was backed out
182+
assert!(p.read_file("bar/src/lib.rs").contains("let mut x = 3;"));
183+
}
184+
185+
#[cargo_test]
186+
fn broken_clippy_fixes_backed_out() {
187+
let p = rustc_shim_for_cargo_fix();
188+
// Attempt to fix code, but our shim will always fail the second compile.
189+
// Also, we use `clippy` as a workspace wrapper to make sure that we properly
190+
// generate the report bug text.
146191
p.cargo("fix --allow-no-vcs --lib")
147192
.cwd("bar")
148193
.env("__CARGO_FIX_YOLO", "1")
149194
.env("RUSTC", p.root().join("foo/target/debug/foo"))
195+
// We can't use `clippy` so we use a `rustc` workspace wrapper instead
196+
.env("RUSTC_WORKSPACE_WRAPPER", wrapped_clippy_driver())
150197
.with_stderr_contains(
151198
"warning: failed to automatically apply fixes suggested by rustc \
152199
to crate `bar`\n\
@@ -160,7 +207,7 @@ fn broken_fixes_backed_out() {
160207
and we would appreciate a bug report! You're likely to see \n\
161208
a number of compiler warnings after this message which cargo\n\
162209
attempted to fix but failed. If you could open an issue at\n\
163-
[..]\n\
210+
https://github.com/rust-lang/rust-clippy/issues\n\
164211
quoting the full output of this command we'd be very appreciative!\n\
165212
Note that you may be able to make some more progress in the near-term\n\
166213
fixing code with the `--broken-code` flag\n\

0 commit comments

Comments
 (0)