Print out autofix-broken or non-converging code when debugging (#4201)

This commit is contained in:
Aarni Koskela 2023-05-03 14:50:03 +03:00 committed by GitHub
parent ccfc78e2d5
commit d0e3ca29d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -453,24 +453,7 @@ pub fn lint_fix<'a>(
// longer parseable on a subsequent pass, then we've introduced a // longer parseable on a subsequent pass, then we've introduced a
// syntax error. Return the original code. // syntax error. Return the original code.
if parseable && result.error.is_some() { if parseable && result.error.is_some() {
#[allow(clippy::print_stderr)] report_autofix_syntax_error(path, &transformed, &result.error.unwrap());
{
eprintln!(
r#"
{}: Autofix introduced a syntax error. Reverting all changes.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BAutofix%20error%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}
return Err(anyhow!("Autofix introduced a syntax error")); return Err(anyhow!("Autofix introduced a syntax error"));
} }
} }
@ -493,25 +476,7 @@ This indicates a bug in `{}`. If you could open an issue at:
continue; continue;
} }
#[allow(clippy::print_stderr)] report_failed_to_converge_error(path, &transformed);
{
eprintln!(
r#"
{}: Failed to converge after {} iterations.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BInfinite%20loop%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
MAX_ITERATIONS,
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}
} }
return Ok(FixerResult { return Ok(FixerResult {
@ -526,3 +491,62 @@ This indicates a bug in `{}`. If you could open an issue at:
}); });
} }
} }
#[allow(clippy::print_stderr)]
fn report_failed_to_converge_error(path: &Path, transformed: &str) {
if cfg!(debug_assertions) {
eprintln!(
"{}: Failed to converge after {} iterations in `{}`:---\n{}\n---",
"debug error".red().bold(),
MAX_ITERATIONS,
fs::relativize_path(path),
transformed,
);
} else {
eprintln!(
r#"
{}: Failed to converge after {} iterations.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BInfinite%20loop%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
MAX_ITERATIONS,
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}
}
#[allow(clippy::print_stderr)]
fn report_autofix_syntax_error(path: &Path, transformed: &str, error: &ParseError) {
if cfg!(debug_assertions) {
eprintln!(
"{}: Autofix introduced a syntax error in `{}`: {}\n---\n{}\n---",
"debug error".red().bold(),
fs::relativize_path(path),
error,
transformed,
);
} else {
eprintln!(
r#"
{}: Autofix introduced a syntax error. Reverting all changes.
This indicates a bug in `{}`. If you could open an issue at:
{}/issues/new?title=%5BAutofix%20error%5D
...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
"#,
"error".red().bold(),
CARGO_PKG_NAME,
CARGO_PKG_REPOSITORY,
fs::relativize_path(path),
);
}
}