Skip to content

Commit 7096340

Browse files
Use rule name rather than message in --statistics (#11697)
Co-authored-by: Micha Reiser <micha@reiser.io> Closes #11097.
1 parent ab9b050 commit 7096340

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

crates/ruff/src/printer.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ bitflags! {
3636
}
3737

3838
#[derive(Serialize)]
39-
struct ExpandedStatistics<'a> {
39+
struct ExpandedStatistics {
4040
code: SerializeRuleAsCode,
41-
message: &'a str,
41+
name: SerializeRuleAsTitle,
4242
count: usize,
4343
fixable: bool,
4444
}
@@ -66,6 +66,29 @@ impl From<Rule> for SerializeRuleAsCode {
6666
}
6767
}
6868

69+
struct SerializeRuleAsTitle(Rule);
70+
71+
impl Serialize for SerializeRuleAsTitle {
72+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
73+
where
74+
S: serde::Serializer,
75+
{
76+
serializer.serialize_str(self.0.as_ref())
77+
}
78+
}
79+
80+
impl Display for SerializeRuleAsTitle {
81+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82+
write!(f, "{}", self.0.as_ref())
83+
}
84+
}
85+
86+
impl From<Rule> for SerializeRuleAsTitle {
87+
fn from(rule: Rule) -> Self {
88+
Self(rule)
89+
}
90+
}
91+
6992
pub(crate) struct Printer {
7093
format: OutputFormat,
7194
log_level: LogLevel,
@@ -317,29 +340,23 @@ impl Printer {
317340
let statistics: Vec<ExpandedStatistics> = diagnostics
318341
.messages
319342
.iter()
320-
.map(|message| {
321-
(
322-
message.kind.rule(),
323-
&message.kind.body,
324-
message.fix.is_some(),
325-
)
326-
})
343+
.map(|message| (message.kind.rule(), message.fix.is_some()))
327344
.sorted()
328-
.fold(vec![], |mut acc, (rule, body, fixable)| {
329-
if let Some((prev_rule, _, _, count)) = acc.last_mut() {
345+
.fold(vec![], |mut acc, (rule, fixable)| {
346+
if let Some((prev_rule, _, count)) = acc.last_mut() {
330347
if *prev_rule == rule {
331348
*count += 1;
332349
return acc;
333350
}
334351
}
335-
acc.push((rule, body, fixable, 1));
352+
acc.push((rule, fixable, 1));
336353
acc
337354
})
338355
.iter()
339-
.map(|(rule, message, fixable, count)| ExpandedStatistics {
356+
.map(|(rule, fixable, count)| ExpandedStatistics {
340357
code: (*rule).into(),
358+
name: (*rule).into(),
341359
count: *count,
342-
message,
343360
fixable: *fixable,
344361
})
345362
.sorted_by_key(|statistic| Reverse(statistic.count))
@@ -386,7 +403,7 @@ impl Printer {
386403
} else {
387404
""
388405
},
389-
statistic.message,
406+
statistic.name,
390407
)?;
391408
}
392409
return Ok(());

crates/ruff/tests/integration_test.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,36 @@ fn show_statistics() {
854854
success: false
855855
exit_code: 1
856856
----- stdout -----
857-
1 F401 [*] `sys` imported but unused
857+
1 F401 [*] unused-import
858+
859+
----- stderr -----
860+
"###);
861+
}
862+
863+
#[test]
864+
fn show_statistics_json() {
865+
let mut cmd = RuffCheck::default()
866+
.args([
867+
"--select",
868+
"F401",
869+
"--statistics",
870+
"--output-format",
871+
"json",
872+
])
873+
.build();
874+
assert_cmd_snapshot!(cmd
875+
.pass_stdin("import sys\nimport os\n\nprint(os.getuid())\n"), @r###"
876+
success: false
877+
exit_code: 1
878+
----- stdout -----
879+
[
880+
{
881+
"code": "F401",
882+
"name": "unused-import",
883+
"count": 1,
884+
"fixable": true
885+
}
886+
]
858887
859888
----- stderr -----
860889
"###);

0 commit comments

Comments
 (0)