Skip to content

Commit

Permalink
Use rule name rather than message in --statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 2, 2024
1 parent 9f3e609 commit b5de54b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
47 changes: 32 additions & 15 deletions crates/ruff/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ bitflags! {
}

#[derive(Serialize)]
struct ExpandedStatistics<'a> {
struct ExpandedStatistics {
code: SerializeRuleAsCode,
message: &'a str,
name: SerializeRuleAsTitle,
count: usize,
fixable: bool,
}
Expand Down Expand Up @@ -65,6 +65,29 @@ impl From<Rule> for SerializeRuleAsCode {
}
}

struct SerializeRuleAsTitle(Rule);

impl Serialize for SerializeRuleAsTitle {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.0.as_ref())
}
}

impl Display for SerializeRuleAsTitle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.as_ref())
}
}

impl From<Rule> for SerializeRuleAsTitle {
fn from(rule: Rule) -> Self {
Self(rule)
}
}

pub(crate) struct Printer {
format: SerializationFormat,
log_level: LogLevel,
Expand Down Expand Up @@ -313,29 +336,23 @@ impl Printer {
let statistics: Vec<ExpandedStatistics> = diagnostics
.messages
.iter()
.map(|message| {
(
message.kind.rule(),
&message.kind.body,
message.fix.is_some(),
)
})
.map(|message| (message.kind.rule(), message.fix.is_some()))
.sorted()
.fold(vec![], |mut acc, (rule, body, fixable)| {
if let Some((prev_rule, _, _, count)) = acc.last_mut() {
.fold(vec![], |mut acc, (rule, fixable)| {
if let Some((prev_rule, _, count)) = acc.last_mut() {
if *prev_rule == rule {
*count += 1;
return acc;
}
}
acc.push((rule, body, fixable, 1));
acc.push((rule, fixable, 1));
acc
})
.iter()
.map(|(rule, message, fixable, count)| ExpandedStatistics {
.map(|(rule, fixable, count)| ExpandedStatistics {
code: (*rule).into(),
name: (*rule).into(),
count: *count,
message,
fixable: *fixable,
})
.sorted_by_key(|statistic| Reverse(statistic.count))
Expand Down Expand Up @@ -384,7 +401,7 @@ impl Printer {
} else {
""
},
statistic.message,
statistic.name,
)?;
}
return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ fn show_statistics() {
success: false
exit_code: 1
----- stdout -----
1 F401 [*] `sys` imported but unused
1 F401 [*] unused-import
----- stderr -----
"###);
Expand Down

0 comments on commit b5de54b

Please sign in to comment.