Skip to content

Commit 549d750

Browse files
committed
Use rule name rather than message in --statistics
1 parent 9f3e609 commit 549d750

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

crates/ruff/src/printer.rs

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

3737
#[derive(Serialize)]
38-
struct ExpandedStatistics<'a> {
38+
struct ExpandedStatistics {
3939
code: SerializeRuleAsCode,
40-
message: &'a str,
40+
name: SerializeRuleAsTitle,
4141
count: usize,
4242
fixable: bool,
4343
}
@@ -65,6 +65,29 @@ impl From<Rule> for SerializeRuleAsCode {
6565
}
6666
}
6767

68+
struct SerializeRuleAsTitle(Rule);
69+
70+
impl Serialize for SerializeRuleAsTitle {
71+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
72+
where
73+
S: serde::Serializer,
74+
{
75+
serializer.serialize_str(self.0.as_ref())
76+
}
77+
}
78+
79+
impl Display for SerializeRuleAsTitle {
80+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81+
write!(f, "{}", self.0.as_ref())
82+
}
83+
}
84+
85+
impl From<Rule> for SerializeRuleAsTitle {
86+
fn from(rule: Rule) -> Self {
87+
Self(rule)
88+
}
89+
}
90+
6891
pub(crate) struct Printer {
6992
format: SerializationFormat,
7093
log_level: LogLevel,
@@ -313,29 +336,23 @@ impl Printer {
313336
let statistics: Vec<ExpandedStatistics> = diagnostics
314337
.messages
315338
.iter()
316-
.map(|message| {
317-
(
318-
message.kind.rule(),
319-
&message.kind.body,
320-
message.fix.is_some(),
321-
)
322-
})
339+
.map(|message| (message.kind.rule(), message.fix.is_some()))
323340
.sorted()
324-
.fold(vec![], |mut acc, (rule, body, fixable)| {
325-
if let Some((prev_rule, _, _, count)) = acc.last_mut() {
341+
.fold(vec![], |mut acc, (rule, fixable)| {
342+
if let Some((prev_rule, _, count)) = acc.last_mut() {
326343
if *prev_rule == rule {
327344
*count += 1;
328345
return acc;
329346
}
330347
}
331-
acc.push((rule, body, fixable, 1));
348+
acc.push((rule, fixable, 1));
332349
acc
333350
})
334351
.iter()
335-
.map(|(rule, message, fixable, count)| ExpandedStatistics {
352+
.map(|(rule, fixable, count)| ExpandedStatistics {
336353
code: (*rule).into(),
354+
name: (*rule).into(),
337355
count: *count,
338-
message,
339356
fixable: *fixable,
340357
})
341358
.sorted_by_key(|statistic| Reverse(statistic.count))
@@ -384,7 +401,7 @@ impl Printer {
384401
} else {
385402
""
386403
},
387-
statistic.message,
404+
statistic.name,
388405
)?;
389406
}
390407
return Ok(());

0 commit comments

Comments
 (0)