Skip to content

[ruff] Fix --statistics reporting for unsafe fixes #16756

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions crates/ruff/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,11 @@ impl Printer {
code: message.rule().map(std::convert::Into::into),
name: message.kind().into(),
count,
fixable: message.fixable(),
fixable: if let Some(fix) = message.fix() {
fix.applies(self.unsafe_fixes.required_applicability())
} else {
false
},
})
.sorted_by_key(|statistic| Reverse(statistic.count))
.collect();
Expand Down Expand Up @@ -412,9 +416,7 @@ impl Printer {
)?;
}

if any_fixable {
writeln!(writer, "[*] fixable with `ruff check --fix`",)?;
}
self.write_summary_text(writer, diagnostics)?;
return Ok(());
}
OutputFormat::Json => {
Expand Down
77 changes: 69 additions & 8 deletions crates/ruff/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,15 +950,40 @@ fn rule_invalid_rule_name() {
#[test]
fn show_statistics() {
let mut cmd = RuffCheck::default()
.args(["--select", "F401", "--statistics"])
.args(["--select", "C416", "--statistics"])
.build();
assert_cmd_snapshot!(cmd
.pass_stdin("import sys\nimport os\n\nprint(os.getuid())\n"), @r"
.pass_stdin(r#"
def mvce(keys, values):
return {key: value for key, value in zip(keys, values)}
"#), @r"
success: false
exit_code: 1
----- stdout -----
1 F401 [*] unused-import
[*] fixable with `ruff check --fix`
1 C416 unnecessary-comprehension
Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).

----- stderr -----
");
}

#[test]
fn show_statistics_unsafe_fixes() {
let mut cmd = RuffCheck::default()
.args(["--select", "C416", "--statistics", "--unsafe-fixes"])
.build();
assert_cmd_snapshot!(cmd
.pass_stdin(r#"
def mvce(keys, values):
return {key: value for key, value in zip(keys, values)}
"#), @r"
success: false
exit_code: 1
----- stdout -----
1 C416 [*] unnecessary-comprehension
Found 1 error.
[*] 1 fixable with the --fix option.

----- stderr -----
");
Expand All @@ -969,21 +994,57 @@ fn show_statistics_json() {
let mut cmd = RuffCheck::default()
.args([
"--select",
"F401",
"C416",
"--statistics",
"--output-format",
"json",
])
.build();
assert_cmd_snapshot!(cmd
.pass_stdin("import sys\nimport os\n\nprint(os.getuid())\n"), @r#"
.pass_stdin(r#"
def mvce(keys, values):
return {key: value for key, value in zip(keys, values)}
"#), @r#"
success: false
exit_code: 1
----- stdout -----
[
{
"code": "C416",
"name": "unnecessary-comprehension",
"count": 1,
"fixable": false
}
]

----- stderr -----
"#);
}

#[test]
fn show_statistics_json_unsafe_fixes() {
let mut cmd = RuffCheck::default()
.args([
"--select",
"C416",
"--statistics",
"--unsafe-fixes",
"--output-format",
"json",
])
.build();
assert_cmd_snapshot!(cmd
.pass_stdin(r#"
def mvce(keys, values):
return {key: value for key, value in zip(keys, values)}
"#), @r#"
success: false
exit_code: 1
----- stdout -----
[
{
"code": "F401",
"name": "unused-import",
"code": "C416",
"name": "unnecessary-comprehension",
"count": 1,
"fixable": true
}
Expand Down
Loading