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, 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(());

View file

@ -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(())
} }