Skip to content

Commit 3a880a2

Browse files
committed
Correct the bug report for cargo clippy --fix
Signed-off-by: hi-rustin <[email protected]>
1 parent 41f7888 commit 3a880a2

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

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,

0 commit comments

Comments
 (0)