-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update fix summary message in check --diff
to include unsafe fix hints
#7790
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8b43b31
Update fix summary message in `check --diff` to include suggested fix…
zanieb f3f7030
Remove "additional" when no errors are fixed
zanieb b60cbab
Update snapshots for https://github.com/astral-sh/ruff/pull/7838
zanieb 67e1782
Add "would be" handling
zanieb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,12 +102,15 @@ impl Printer { | |
|
||
fn write_summary_text(&self, writer: &mut dyn Write, diagnostics: &Diagnostics) -> Result<()> { | ||
if self.log_level >= LogLevel::Default { | ||
let fixables = FixableStatistics::try_from(diagnostics, self.unsafe_fixes); | ||
|
||
let fixed = diagnostics | ||
.fixed | ||
.values() | ||
.flat_map(std::collections::HashMap::values) | ||
.sum::<usize>(); | ||
|
||
if self.flags.intersects(Flags::SHOW_VIOLATIONS) { | ||
let fixed = diagnostics | ||
.fixed | ||
.values() | ||
.flat_map(std::collections::HashMap::values) | ||
.sum::<usize>(); | ||
let remaining = diagnostics.messages.len(); | ||
let total = fixed + remaining; | ||
if fixed > 0 { | ||
|
@@ -121,21 +124,73 @@ impl Printer { | |
writeln!(writer, "Found {remaining} error{s}.")?; | ||
} | ||
|
||
if let Some(fixables) = FixableSummary::try_from(diagnostics, self.unsafe_fixes) { | ||
writeln!(writer, "{fixables}")?; | ||
if let Some(fixables) = fixables { | ||
let fix_prefix = format!("[{}]", "*".cyan()); | ||
|
||
if self.unsafe_fixes.is_enabled() { | ||
writeln!( | ||
writer, | ||
"{fix_prefix} {} fixable with the --fix option.", | ||
fixables.applicable | ||
)?; | ||
} else { | ||
if fixables.applicable > 0 && fixables.unapplicable > 0 { | ||
let es = if fixables.unapplicable == 1 { "" } else { "es" }; | ||
writeln!(writer, | ||
"{fix_prefix} {} fixable with the `--fix` option ({} hidden fix{es} can be enabled with the `--unsafe-fixes` option).", | ||
fixables.applicable, fixables.unapplicable | ||
)?; | ||
} else if fixables.applicable > 0 { | ||
// Only applicable fixes | ||
writeln!( | ||
writer, | ||
"{fix_prefix} {} fixable with the `--fix` option.", | ||
fixables.applicable, | ||
)?; | ||
} else { | ||
// Only unapplicable fixes | ||
let es = if fixables.unapplicable == 1 { "" } else { "es" }; | ||
writeln!(writer, | ||
"{} hidden fix{es} can be enabled with the `--unsafe-fixes` option.", | ||
fixables.unapplicable | ||
)?; | ||
} | ||
} | ||
} | ||
} else { | ||
let fixed = diagnostics | ||
.fixed | ||
.values() | ||
.flat_map(std::collections::HashMap::values) | ||
.sum::<usize>(); | ||
if fixed > 0 { | ||
let s = if fixed == 1 { "" } else { "s" }; | ||
if self.fix_mode.is_apply() { | ||
writeln!(writer, "Fixed {fixed} error{s}.")?; | ||
// Check if there are unapplied fixes | ||
let unapplied = { | ||
if let Some(fixables) = fixables { | ||
fixables.unapplicable | ||
} else { | ||
writeln!(writer, "Would fix {fixed} error{s}.")?; | ||
0 | ||
} | ||
}; | ||
|
||
if unapplied > 0 { | ||
let es = if unapplied == 1 { "" } else { "es" }; | ||
if fixed > 0 { | ||
let s = if fixed == 1 { "" } else { "s" }; | ||
if self.fix_mode.is_apply() { | ||
writeln!(writer, "Fixed {fixed} error{s} ({unapplied} additional fix{es} available with `--unsafe-fixes`).")?; | ||
} else { | ||
writeln!(writer, "Would fix {fixed} error{s} ({unapplied} additional fix{es} available with `--unsafe-fixes`).")?; | ||
} | ||
} else { | ||
if self.fix_mode.is_apply() { | ||
writeln!(writer, "No errors fixed ({unapplied} fix{es} available with `--unsafe-fixes`).")?; | ||
} else { | ||
writeln!(writer, "No errors would be fixed ({unapplied} fix{es} available with `--unsafe-fixes`).")?; | ||
} | ||
} | ||
} else { | ||
if fixed > 0 { | ||
let s = if fixed == 1 { "" } else { "s" }; | ||
if self.fix_mode.is_apply() { | ||
writeln!(writer, "Fixed {fixed} error{s}.")?; | ||
} else { | ||
writeln!(writer, "Would fix {fixed} error{s}.")?; | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -170,7 +225,7 @@ impl Printer { | |
} | ||
|
||
let context = EmitterContext::new(&diagnostics.notebook_indexes); | ||
let fixables = FixableSummary::try_from(diagnostics, self.unsafe_fixes); | ||
let fixables = FixableStatistics::try_from(diagnostics, self.unsafe_fixes); | ||
|
||
match self.format { | ||
SerializationFormat::Json => { | ||
|
@@ -354,7 +409,7 @@ impl Printer { | |
); | ||
} | ||
|
||
let fixables = FixableSummary::try_from(diagnostics, self.unsafe_fixes); | ||
let fixables = FixableStatistics::try_from(diagnostics, self.unsafe_fixes); | ||
|
||
if !diagnostics.messages.is_empty() { | ||
if self.log_level >= LogLevel::Default { | ||
|
@@ -388,13 +443,13 @@ fn num_digits(n: usize) -> usize { | |
} | ||
|
||
/// Return `true` if the [`Printer`] should indicate that a rule is fixable. | ||
fn show_fix_status(fix_mode: flags::FixMode, fixables: Option<&FixableSummary>) -> bool { | ||
fn show_fix_status(fix_mode: flags::FixMode, fixables: Option<&FixableStatistics>) -> bool { | ||
// If we're in application mode, avoid indicating that a rule is fixable. | ||
// If the specific violation were truly fixable, it would've been fixed in | ||
// this pass! (We're occasionally unable to determine whether a specific | ||
// violation is fixable without trying to fix it, so if fix is not | ||
// enabled, we may inadvertently indicate that a rule is fixable.) | ||
(!fix_mode.is_apply()) && fixables.is_some_and(FixableSummary::any_applicable_fixes) | ||
(!fix_mode.is_apply()) && fixables.is_some_and(FixableStatistics::any_applicable_fixes) | ||
} | ||
|
||
fn print_fix_summary(writer: &mut dyn Write, fixed: &FxHashMap<String, FixTable>) -> Result<()> { | ||
|
@@ -438,15 +493,14 @@ fn print_fix_summary(writer: &mut dyn Write, fixed: &FxHashMap<String, FixTable> | |
Ok(()) | ||
} | ||
|
||
/// Summarizes [applicable][ruff_diagnostics::Applicability] fixes. | ||
/// Statistics for [applicable][ruff_diagnostics::Applicability] fixes. | ||
#[derive(Debug)] | ||
struct FixableSummary { | ||
struct FixableStatistics { | ||
applicable: u32, | ||
unapplicable: u32, | ||
unsafe_fixes: UnsafeFixes, | ||
} | ||
|
||
impl FixableSummary { | ||
impl FixableStatistics { | ||
fn try_from(diagnostics: &Diagnostics, unsafe_fixes: UnsafeFixes) -> Option<Self> { | ||
let mut applicable = 0; | ||
let mut unapplicable = 0; | ||
|
@@ -467,7 +521,6 @@ impl FixableSummary { | |
Some(Self { | ||
applicable, | ||
unapplicable, | ||
unsafe_fixes, | ||
}) | ||
} | ||
} | ||
|
@@ -476,41 +529,3 @@ impl FixableSummary { | |
self.applicable > 0 | ||
} | ||
} | ||
|
||
impl Display for FixableSummary { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this belongs here, this is one specific message derived from this struct. |
||
let fix_prefix = format!("[{}]", "*".cyan()); | ||
|
||
if self.unsafe_fixes.is_enabled() { | ||
write!( | ||
f, | ||
"{fix_prefix} {} fixable with the --fix option.", | ||
self.applicable | ||
) | ||
} else { | ||
if self.applicable > 0 && self.unapplicable > 0 { | ||
let es = if self.unapplicable == 1 { "" } else { "es" }; | ||
write!( | ||
f, | ||
"{fix_prefix} {} fixable with the `--fix` option ({} hidden fix{es} can be enabled with the `--unsafe-fixes` option).", | ||
self.applicable, self.unapplicable | ||
) | ||
} else if self.applicable > 0 { | ||
// Only applicable fixes | ||
write!( | ||
f, | ||
"{fix_prefix} {} fixable with the `--fix` option.", | ||
self.applicable, | ||
) | ||
} else { | ||
// Only unapplicable fixes | ||
let es = if self.unapplicable == 1 { "" } else { "es" }; | ||
write!( | ||
f, | ||
"{} hidden fix{es} can be enabled with the `--unsafe-fixes` option.", | ||
self.unapplicable | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all unchanged and pulled out of
FixableStatistics.fmt
because I don't think it makes sense to have it on the struct.