mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Use rule name rather than message in --statistics
(#11697)
Co-authored-by: Micha Reiser <micha@reiser.io> Closes https://github.com/astral-sh/ruff/issues/11097.
This commit is contained in:
parent
b24e4473c5
commit
bfe36b9584
2 changed files with 62 additions and 16 deletions
|
@ -36,9 +36,9 @@ bitflags! {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct ExpandedStatistics<'a> {
|
struct ExpandedStatistics {
|
||||||
code: SerializeRuleAsCode,
|
code: SerializeRuleAsCode,
|
||||||
message: &'a str,
|
name: SerializeRuleAsTitle,
|
||||||
count: usize,
|
count: usize,
|
||||||
fixable: bool,
|
fixable: bool,
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,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 {
|
pub(crate) struct Printer {
|
||||||
format: OutputFormat,
|
format: OutputFormat,
|
||||||
log_level: LogLevel,
|
log_level: LogLevel,
|
||||||
|
@ -317,29 +340,23 @@ impl Printer {
|
||||||
let statistics: Vec<ExpandedStatistics> = diagnostics
|
let statistics: Vec<ExpandedStatistics> = diagnostics
|
||||||
.messages
|
.messages
|
||||||
.iter()
|
.iter()
|
||||||
.map(|message| {
|
.map(|message| (message.kind.rule(), message.fix.is_some()))
|
||||||
(
|
|
||||||
message.kind.rule(),
|
|
||||||
&message.kind.body,
|
|
||||||
message.fix.is_some(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.sorted()
|
.sorted()
|
||||||
.fold(vec![], |mut acc, (rule, body, fixable)| {
|
.fold(vec![], |mut acc, (rule, fixable)| {
|
||||||
if let Some((prev_rule, _, _, count)) = acc.last_mut() {
|
if let Some((prev_rule, _, count)) = acc.last_mut() {
|
||||||
if *prev_rule == rule {
|
if *prev_rule == rule {
|
||||||
*count += 1;
|
*count += 1;
|
||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
acc.push((rule, body, fixable, 1));
|
acc.push((rule, fixable, 1));
|
||||||
acc
|
acc
|
||||||
})
|
})
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(rule, message, fixable, count)| ExpandedStatistics {
|
.map(|(rule, fixable, count)| ExpandedStatistics {
|
||||||
code: (*rule).into(),
|
code: (*rule).into(),
|
||||||
|
name: (*rule).into(),
|
||||||
count: *count,
|
count: *count,
|
||||||
message,
|
|
||||||
fixable: *fixable,
|
fixable: *fixable,
|
||||||
})
|
})
|
||||||
.sorted_by_key(|statistic| Reverse(statistic.count))
|
.sorted_by_key(|statistic| Reverse(statistic.count))
|
||||||
|
@ -386,7 +403,7 @@ impl Printer {
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
},
|
},
|
||||||
statistic.message,
|
statistic.name,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -854,7 +854,36 @@ fn show_statistics() {
|
||||||
success: false
|
success: false
|
||||||
exit_code: 1
|
exit_code: 1
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
1 F401 [*] `sys` imported but unused
|
1 F401 [*] unused-import
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
"###);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn show_statistics_json() {
|
||||||
|
let mut cmd = RuffCheck::default()
|
||||||
|
.args([
|
||||||
|
"--select",
|
||||||
|
"F401",
|
||||||
|
"--statistics",
|
||||||
|
"--output-format",
|
||||||
|
"json",
|
||||||
|
])
|
||||||
|
.build();
|
||||||
|
assert_cmd_snapshot!(cmd
|
||||||
|
.pass_stdin("import sys\nimport os\n\nprint(os.getuid())\n"), @r###"
|
||||||
|
success: false
|
||||||
|
exit_code: 1
|
||||||
|
----- stdout -----
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"code": "F401",
|
||||||
|
"name": "unused-import",
|
||||||
|
"count": 1,
|
||||||
|
"fixable": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
"###);
|
"###);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue