mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:37 +00:00
feat(cli): let --statistics show fixable codes (#2659)
This commit is contained in:
parent
fc628de667
commit
a129181407
2 changed files with 23 additions and 4 deletions
|
@ -48,6 +48,7 @@ struct ExpandedStatistics<'a> {
|
||||||
count: usize,
|
count: usize,
|
||||||
code: &'a str,
|
code: &'a str,
|
||||||
message: String,
|
message: String,
|
||||||
|
fixable: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SerializeRuleAsCode<'a>(&'a Rule);
|
struct SerializeRuleAsCode<'a>(&'a Rule);
|
||||||
|
@ -371,6 +372,12 @@ impl<'a> Printer<'a> {
|
||||||
.find(|message| message.kind.rule() == *rule)
|
.find(|message| message.kind.rule() == *rule)
|
||||||
.map(|message| message.kind.body())
|
.map(|message| message.kind.body())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
fixable: diagnostics
|
||||||
|
.messages
|
||||||
|
.iter()
|
||||||
|
.find(|message| message.kind.rule() == *rule)
|
||||||
|
.iter()
|
||||||
|
.any(|message| message.kind.fixable()),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
@ -391,13 +398,25 @@ impl<'a> Printer<'a> {
|
||||||
.map(|statistic| statistic.code.len())
|
.map(|statistic| statistic.code.len())
|
||||||
.max()
|
.max()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let any_fixable = statistics.iter().any(|statistic| statistic.fixable);
|
||||||
|
|
||||||
// By default, we mimic Flake8's `--statistics` format.
|
// By default, we mimic Flake8's `--statistics` format.
|
||||||
for msg in statistics {
|
for statistic in statistics {
|
||||||
writeln!(
|
writeln!(
|
||||||
stdout,
|
stdout,
|
||||||
"{:>count_width$}\t{:<code_width$}\t{}",
|
"{:>count_width$}\t{:<code_width$}\t{}{}",
|
||||||
msg.count, msg.code, msg.message
|
statistic.count,
|
||||||
|
statistic.code,
|
||||||
|
if any_fixable {
|
||||||
|
if statistic.fixable {
|
||||||
|
"[*] "
|
||||||
|
} else {
|
||||||
|
"[ ] "
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
statistic.message,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -200,7 +200,7 @@ fn show_statistics() -> Result<()> {
|
||||||
.lines()
|
.lines()
|
||||||
.last()
|
.last()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
"1\tF401\t`sys` imported but unused"
|
"1\tF401\t[*] `sys` imported but unused"
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue