feat(cli): let --statistics show fixable codes (#2659)

This commit is contained in:
Florian Best 2023-02-10 01:36:31 +01:00 committed by GitHub
parent fc628de667
commit a129181407
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View file

@ -48,6 +48,7 @@ struct ExpandedStatistics<'a> {
count: usize,
code: &'a str,
message: String,
fixable: bool,
}
struct SerializeRuleAsCode<'a>(&'a Rule);
@ -371,6 +372,12 @@ impl<'a> Printer<'a> {
.find(|message| message.kind.rule() == *rule)
.map(|message| message.kind.body())
.unwrap(),
fixable: diagnostics
.messages
.iter()
.find(|message| message.kind.rule() == *rule)
.iter()
.any(|message| message.kind.fixable()),
})
.collect::<Vec<_>>();
@ -391,13 +398,25 @@ impl<'a> Printer<'a> {
.map(|statistic| statistic.code.len())
.max()
.unwrap();
let any_fixable = statistics.iter().any(|statistic| statistic.fixable);
// By default, we mimic Flake8's `--statistics` format.
for msg in statistics {
for statistic in statistics {
writeln!(
stdout,
"{:>count_width$}\t{:<code_width$}\t{}",
msg.count, msg.code, msg.message
"{:>count_width$}\t{:<code_width$}\t{}{}",
statistic.count,
statistic.code,
if any_fixable {
if statistic.fixable {
"[*] "
} else {
"[ ] "
}
} else {
""
},
statistic.message,
)?;
}
return Ok(());

View file

@ -200,7 +200,7 @@ fn show_statistics() -> Result<()> {
.lines()
.last()
.unwrap(),
"1\tF401\t`sys` imported but unused"
"1\tF401\t[*] `sys` imported but unused"
);
Ok(())
}