Improve --explain output

Previous output for `ruff --explain E711`:

    E711 (pycodestyle): Comparison to `None` should be `cond is None`

New output:

    none-comparison

    Code: E711 (pycodestyle)

    Autofix is always available.

    Message formats:

    * Comparison to `None` should be `cond is None`
    * Comparison to `None` should be `cond is not None`
This commit is contained in:
Martin Fischer 2023-01-20 03:21:44 +01:00 committed by Charlie Marsh
parent ec0c7647ab
commit 6acf2accc6
1 changed files with 17 additions and 7 deletions

View File

@ -19,7 +19,7 @@ use ruff::registry::Rule;
use ruff::resolver::{FileDiscovery, PyprojectDiscovery}; use ruff::resolver::{FileDiscovery, PyprojectDiscovery};
use ruff::settings::flags; use ruff::settings::flags;
use ruff::settings::types::SerializationFormat; use ruff::settings::types::SerializationFormat;
use ruff::{fix, fs, packaging, resolver, warn_user_once, IOError}; use ruff::{fix, fs, packaging, resolver, warn_user_once, AutofixAvailability, IOError};
use serde::Serialize; use serde::Serialize;
use walkdir::WalkDir; use walkdir::WalkDir;
@ -293,13 +293,23 @@ struct Explanation<'a> {
pub fn explain(rule: &Rule, format: SerializationFormat) -> Result<()> { pub fn explain(rule: &Rule, format: SerializationFormat) -> Result<()> {
match format { match format {
SerializationFormat::Text | SerializationFormat::Grouped => { SerializationFormat::Text | SerializationFormat::Grouped => {
println!("{}\n", rule.as_ref());
println!("Code: {} ({})\n", rule.code(), rule.origin().name());
if let Some(autofix) = rule.autofixable() {
println!( println!(
"{} ({}): {}", "{}",
rule.code(), match autofix.available {
rule.origin().name(), AutofixAvailability::Sometimes => "Autofix is sometimes available.\n",
rule.message_formats()[0] AutofixAvailability::Always => "Autofix is always available.\n",
}
); );
} }
println!("Message formats:\n");
for format in rule.message_formats() {
println!("* {format}");
}
}
SerializationFormat::Json => { SerializationFormat::Json => {
println!( println!(
"{}", "{}",