Skip to content

Commit 4956d65

Browse files
charliermarshMichaReiser
authored andcommitted
Use rule name rather than message in --statistics
1 parent e2e98d0 commit 4956d65

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-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: SerializationFormat,
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))
@@ -388,7 +405,7 @@ impl Printer {
388405
} else {
389406
""
390407
},
391-
statistic.message,
408+
statistic.name,
392409
)?;
393410
}
394411
return Ok(());

crates/ruff/tests/integration_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ fn show_statistics() {
854854
success: false
855855
exit_code: 1
856856
----- stdout -----
857-
1 F401 [*] `sys` imported but unused
857+
1 F401 [*] unused-import
858858
859859
----- stderr -----
860860
"###);

0 commit comments

Comments
 (0)