Skip to content

Commit 433879d

Browse files
authored
[ruff] Fix --statistics reporting for unsafe fixes (#16756)
Fixes #16751 ## Summary Previously, unsafe fixes were counted as "fixable" in `Printer::write_statistics`, in contrast to the behaviour in `Printer::write_once`. This changes the behaviour to align with `write_once`, including them only if `--unsafe-fixes` is set. We now also reuse `Printer::write_summary` to avoid duplicating the logic for whether or not to report if there are hidden fixes. ## Test Plan Existing tests modified to use an unsafe-fixable rule, and new ones added to cover the case with `--unsafe-fixes`
1 parent b7d232c commit 433879d

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

crates/ruff/src/printer.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,11 @@ impl Printer {
353353
code: message.rule().map(std::convert::Into::into),
354354
name: message.kind().into(),
355355
count,
356-
fixable: message.fixable(),
356+
fixable: if let Some(fix) = message.fix() {
357+
fix.applies(self.unsafe_fixes.required_applicability())
358+
} else {
359+
false
360+
},
357361
})
358362
.sorted_by_key(|statistic| Reverse(statistic.count))
359363
.collect();
@@ -412,9 +416,7 @@ impl Printer {
412416
)?;
413417
}
414418

415-
if any_fixable {
416-
writeln!(writer, "[*] fixable with `ruff check --fix`",)?;
417-
}
419+
self.write_summary_text(writer, diagnostics)?;
418420
return Ok(());
419421
}
420422
OutputFormat::Json => {

crates/ruff/tests/integration_test.rs

+69-8
Original file line numberDiff line numberDiff line change
@@ -950,15 +950,40 @@ fn rule_invalid_rule_name() {
950950
#[test]
951951
fn show_statistics() {
952952
let mut cmd = RuffCheck::default()
953-
.args(["--select", "F401", "--statistics"])
953+
.args(["--select", "C416", "--statistics"])
954954
.build();
955955
assert_cmd_snapshot!(cmd
956-
.pass_stdin("import sys\nimport os\n\nprint(os.getuid())\n"), @r"
956+
.pass_stdin(r#"
957+
def mvce(keys, values):
958+
return {key: value for key, value in zip(keys, values)}
959+
"#), @r"
957960
success: false
958961
exit_code: 1
959962
----- stdout -----
960-
1 F401 [*] unused-import
961-
[*] fixable with `ruff check --fix`
963+
1 C416 unnecessary-comprehension
964+
Found 1 error.
965+
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).
966+
967+
----- stderr -----
968+
");
969+
}
970+
971+
#[test]
972+
fn show_statistics_unsafe_fixes() {
973+
let mut cmd = RuffCheck::default()
974+
.args(["--select", "C416", "--statistics", "--unsafe-fixes"])
975+
.build();
976+
assert_cmd_snapshot!(cmd
977+
.pass_stdin(r#"
978+
def mvce(keys, values):
979+
return {key: value for key, value in zip(keys, values)}
980+
"#), @r"
981+
success: false
982+
exit_code: 1
983+
----- stdout -----
984+
1 C416 [*] unnecessary-comprehension
985+
Found 1 error.
986+
[*] 1 fixable with the --fix option.
962987
963988
----- stderr -----
964989
");
@@ -969,21 +994,57 @@ fn show_statistics_json() {
969994
let mut cmd = RuffCheck::default()
970995
.args([
971996
"--select",
972-
"F401",
997+
"C416",
973998
"--statistics",
974999
"--output-format",
9751000
"json",
9761001
])
9771002
.build();
9781003
assert_cmd_snapshot!(cmd
979-
.pass_stdin("import sys\nimport os\n\nprint(os.getuid())\n"), @r#"
1004+
.pass_stdin(r#"
1005+
def mvce(keys, values):
1006+
return {key: value for key, value in zip(keys, values)}
1007+
"#), @r#"
1008+
success: false
1009+
exit_code: 1
1010+
----- stdout -----
1011+
[
1012+
{
1013+
"code": "C416",
1014+
"name": "unnecessary-comprehension",
1015+
"count": 1,
1016+
"fixable": false
1017+
}
1018+
]
1019+
1020+
----- stderr -----
1021+
"#);
1022+
}
1023+
1024+
#[test]
1025+
fn show_statistics_json_unsafe_fixes() {
1026+
let mut cmd = RuffCheck::default()
1027+
.args([
1028+
"--select",
1029+
"C416",
1030+
"--statistics",
1031+
"--unsafe-fixes",
1032+
"--output-format",
1033+
"json",
1034+
])
1035+
.build();
1036+
assert_cmd_snapshot!(cmd
1037+
.pass_stdin(r#"
1038+
def mvce(keys, values):
1039+
return {key: value for key, value in zip(keys, values)}
1040+
"#), @r#"
9801041
success: false
9811042
exit_code: 1
9821043
----- stdout -----
9831044
[
9841045
{
985-
"code": "F401",
986-
"name": "unused-import",
1046+
"code": "C416",
1047+
"name": "unnecessary-comprehension",
9871048
"count": 1,
9881049
"fixable": true
9891050
}

0 commit comments

Comments
 (0)