Skip to content

Commit

Permalink
Correct the bug report for cargo clippy --fix
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Apr 14, 2023
1 parent 41f7888 commit 3a880a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/job_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl<'cfg> JobQueue<'cfg> {
timings: self.timings,
tokens: Vec::new(),
pending_queue: Vec::new(),
print: DiagnosticPrinter::new(cx.bcx.config),
print: DiagnosticPrinter::new(cx.bcx.config, &cx.bcx.rustc().workspace_wrapper),
finished: 0,
per_package_future_incompat_reports: Vec::new(),
};
Expand Down
64 changes: 51 additions & 13 deletions src/cargo/util/diagnostic_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::collections::HashSet;
use std::io::{BufReader, Read, Write};
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
Expand All @@ -18,16 +19,6 @@ use crate::util::errors::CargoResult;
use crate::util::Config;

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

#[derive(Deserialize, Serialize, Hash, Eq, PartialEq, Clone)]
pub enum Message {
Expand Down Expand Up @@ -83,15 +74,27 @@ impl Message {
}
}

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

impl<'a> DiagnosticPrinter<'a> {
pub fn new(config: &'a Config) -> DiagnosticPrinter<'a> {
pub fn new(
config: &'a Config,
workspace_wrapper: &'a Option<PathBuf>,
) -> DiagnosticPrinter<'a> {
DiagnosticPrinter {
config,
workspace_wrapper,
dedupe: HashSet::new(),
}
}
Expand Down Expand Up @@ -128,7 +131,12 @@ impl<'a> DiagnosticPrinter<'a> {
"The full error message was:\n\n> {}\n\n",
message,
)?;
write!(self.config.shell().err(), "{}", PLEASE_REPORT_THIS_BUG)?;
let issue_link = get_bug_report_url(self.workspace_wrapper);
write!(
self.config.shell().err(),
"{}",
gen_please_report_this_bug_text(issue_link)
)?;
Ok(())
}
Message::FixFailed {
Expand Down Expand Up @@ -159,7 +167,12 @@ impl<'a> DiagnosticPrinter<'a> {
}
writeln!(self.config.shell().err())?;
}
write!(self.config.shell().err(), "{}", PLEASE_REPORT_THIS_BUG)?;
let issue_link = get_bug_report_url(self.workspace_wrapper);
write!(
self.config.shell().err(),
"{}",
gen_please_report_this_bug_text(issue_link)
)?;
if !errors.is_empty() {
writeln!(
self.config.shell().err(),
Expand Down Expand Up @@ -218,6 +231,31 @@ https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-proje
}
}

fn gen_please_report_this_bug_text(url: &str) -> String {
format!(
"This likely indicates a bug in either rustc or cargo itself,\n\
and we would appreciate a bug report! You're likely to see \n\
a number of compiler warnings after this message which cargo\n\
attempted to fix but failed. If you could open an issue at\n\
{}\n\
quoting the full output of this command we'd be very appreciative!\n\
Note that you may be able to make some more progress in the near-term\n\
fixing code with the `--broken-code` flag\n\n\
",
url
)
}

fn get_bug_report_url(rustc_workspace_wrapper: &Option<PathBuf>) -> &str {
let clippy = std::ffi::OsStr::new("clippy-driver");
let issue_link = match rustc_workspace_wrapper.as_ref().and_then(|x| x.file_stem()) {
Some(wrapper) if wrapper == clippy => "https://github.com/rust-lang/rust-clippy/issues",
_ => "https://github.com/rust-lang/rust/issues",
};

issue_link
}

#[derive(Debug)]
pub struct RustfixDiagnosticServer {
listener: TcpListener,
Expand Down

0 comments on commit 3a880a2

Please sign in to comment.