Skip to content

Commit f381eb1

Browse files
authored
Prioritize human-readable names in CLI output (#25869)
Summary -- This PR swaps the rule name and code in our `--show-fixes` output, which previously looked like this: <img width="324" height="107" alt="image" src="https://github.com/user-attachments/assets/32dff652-421c-4e7a-adbd-ac87cc740965" /> and now looks like this in preview: <img width="314" height="103" alt="image" src="https://github.com/user-attachments/assets/d7d942d6-d5a5-4622-b9a2-a271eb5b0663" /> The actual code change is just for this one flag, but I also examined the other CLI flags (besides diagnostic output formats, which I'll update separately) and think that nothing else needs to be updated. The other two main candidates were `--statistics` and `--show-settings`, but these already show both codes and names prominently. `--show-settings` even prioritizes them over codes: ```console ❯ ruff check --no-cache --ignore-noqa --statistics 1 F401 [*] unused-import Found 1 error. [*] 1 fixable with the `--fix` option. ❯ ruff check --no-cache --show-settings ... linter.rules.enabled = [ multiple-imports-on-one-line (E401), module-import-not-at-top-of-file (E402), ... ] ... ``` Test Plan -- New CLI test and the screenshots above
1 parent b9b4546 commit f381eb1

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

‎crates/ruff/src/printer.rs‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ruff_linter::fs::relativize_path;
1414
use ruff_linter::logging::LogLevel;
1515
use ruff_linter::message::{EmitterContext, render_diagnostics};
1616
use ruff_linter::notify_user;
17-
use ruff_linter::preview::is_warning_severity_enabled;
17+
use ruff_linter::preview::{is_human_readable_names_enabled, is_warning_severity_enabled};
1818
use ruff_linter::settings::flags::{self};
1919
use ruff_linter::settings::types::{OutputFormat, PreviewMode, UnsafeFixes};
2020

@@ -223,7 +223,7 @@ impl Printer {
223223
if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) {
224224
if !diagnostics.fixed.is_empty() {
225225
writeln!(writer)?;
226-
print_fix_summary(writer, &diagnostics.fixed)?;
226+
print_fix_summary(writer, &diagnostics.fixed, preview)?;
227227
writeln!(writer)?;
228228
}
229229
}
@@ -252,7 +252,7 @@ impl Printer {
252252
if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) {
253253
if !diagnostics.fixed.is_empty() {
254254
writeln!(writer)?;
255-
print_fix_summary(writer, &diagnostics.fixed)?;
255+
print_fix_summary(writer, &diagnostics.fixed, preview)?;
256256
writeln!(writer)?;
257257
}
258258
}
@@ -447,7 +447,7 @@ fn show_fix_status(fix_mode: flags::FixMode, fixables: Option<&FixableStatistics
447447
(!fix_mode.is_apply()) && fixables.is_some_and(FixableStatistics::any_applicable_fixes)
448448
}
449449

450-
fn print_fix_summary(writer: &mut dyn Write, fixed: &FixMap) -> Result<()> {
450+
fn print_fix_summary(writer: &mut dyn Write, fixed: &FixMap, preview: PreviewMode) -> Result<()> {
451451
let total = fixed
452452
.values()
453453
.map(|table| table.counts().sum::<usize>())
@@ -477,11 +477,19 @@ fn print_fix_summary(writer: &mut dyn Write, fixed: &FixMap) -> Result<()> {
477477
":".cyan()
478478
)?;
479479
for (code, name, count) in table.iter().sorted_by_key(|(.., count)| Reverse(*count)) {
480-
writeln!(
481-
writer,
482-
" {count:>num_digits$} × {code} ({name})",
483-
code = code.to_string().red().bold(),
484-
)?;
480+
if is_human_readable_names_enabled(preview) {
481+
writeln!(
482+
writer,
483+
" {count:>num_digits$} × {name} ({code})",
484+
name = name.to_string().red().bold(),
485+
)?;
486+
} else {
487+
writeln!(
488+
writer,
489+
" {count:>num_digits$} × {code} ({name})",
490+
code = code.to_string().red().bold(),
491+
)?;
492+
}
485493
}
486494
}
487495
Ok(())

‎crates/ruff/tests/cli/lint.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,37 @@ fn output_format_show_fixes(output_format: &str) -> Result<()> {
37033703
Ok(())
37043704
}
37053705

3706+
#[test]
3707+
fn show_fixes_preview() -> Result<()> {
3708+
let fixture = CliTest::with_file("input.py", "import os # F401")?;
3709+
3710+
assert_cmd_snapshot!(
3711+
fixture.check_command().args([
3712+
"--select",
3713+
"F401",
3714+
"--fix",
3715+
"--show-fixes",
3716+
"--preview",
3717+
"input.py",
3718+
]),
3719+
@"
3720+
success: true
3721+
exit_code: 0
3722+
----- stdout -----
3723+
3724+
Fixed 1 error:
3725+
- input.py:
3726+
1 × unused-import (F401)
3727+
3728+
Found 1 error (1 fixed, 0 remaining).
3729+
3730+
----- stderr -----
3731+
"
3732+
);
3733+
3734+
Ok(())
3735+
}
3736+
37063737
#[test]
37073738
fn up045_nested_optional_flatten_all() {
37083739
let contents = "\

‎crates/ruff_linter/src/preview.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,6 @@ pub(crate) const fn is_pep604_future_annotations_fix_enabled(settings: &LinterSe
369369
}
370370

371371
// https://github.com/astral-sh/ruff/pull/25614
372-
pub(crate) const fn is_human_readable_names_enabled(preview: PreviewMode) -> bool {
372+
pub const fn is_human_readable_names_enabled(preview: PreviewMode) -> bool {
373373
preview.is_enabled()
374374
}

0 commit comments

Comments
 (0)