From 56398e000217e5719cab9905a4635d41110f4031 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 7 Feb 2023 19:02:41 -0500 Subject: [PATCH] Tweak format for rule explanations (#2645) --- crates/ruff_cli/src/commands.rs | 46 ++++++++++++++------------- crates/ruff_dev/src/generate_docs.rs | 25 ++++++++++++--- docs/rules/assert-raises-exception.md | 10 +++--- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/crates/ruff_cli/src/commands.rs b/crates/ruff_cli/src/commands.rs index 77cfac3a28..b1b4dde079 100644 --- a/crates/ruff_cli/src/commands.rs +++ b/crates/ruff_cli/src/commands.rs @@ -278,34 +278,36 @@ pub fn rule(rule: &Rule, format: HelpFormat) -> Result<()> { let mut stdout = BufWriter::new(io::stdout().lock()); match format { HelpFormat::Text => { - writeln!( - stdout, - "[{}] {} ({})", - linter.name(), - rule.as_ref(), - rule.code(), - )?; - writeln!(stdout)?; + let mut output = String::new(); + output.push_str(&format!("# {} ({})", rule.as_ref(), rule.code())); + output.push('\n'); + output.push('\n'); + + let (linter, _) = Linter::parse_code(rule.code()).unwrap(); + output.push_str(&format!("Derived from the **{}** linter.", linter.name())); + output.push('\n'); + output.push('\n'); + + if let Some(autofix) = rule.autofixable() { + output.push_str(match autofix.available { + AutofixAvailability::Sometimes => "Autofix is sometimes available.", + AutofixAvailability::Always => "Autofix is always available.", + }); + output.push('\n'); + output.push('\n'); + } + if let Some(explanation) = rule.explanation() { - writeln!(stdout, "{}", explanation.trim())?; + output.push_str(explanation.trim()); } else { - writeln!(stdout, "Message formats:")?; + output.push_str("Message formats:"); for format in rule.message_formats() { - writeln!(stdout, "* {format}")?; + output.push('\n'); + output.push_str(&format!("* {}", format)); } } - if let Some(autofix) = rule.autofixable() { - writeln!(stdout)?; - writeln!( - stdout, - "{}", - match autofix.available { - AutofixAvailability::Sometimes => "Autofix is sometimes available.", - AutofixAvailability::Always => "Autofix is always available.", - } - )?; - } + writeln!(stdout, "{}", output)?; } HelpFormat::Json => { writeln!( diff --git a/crates/ruff_dev/src/generate_docs.rs b/crates/ruff_dev/src/generate_docs.rs index 5ec52a92b1..c1c35b815a 100644 --- a/crates/ruff_dev/src/generate_docs.rs +++ b/crates/ruff_dev/src/generate_docs.rs @@ -4,9 +4,10 @@ use std::fs; use anyhow::Result; +use ruff::AutofixAvailability; use strum::IntoEnumIterator; -use ruff::registry::Rule; +use ruff::registry::{Linter, Rule, RuleNamespace}; #[derive(clap::Args)] pub struct Args { @@ -18,13 +19,29 @@ pub struct Args { pub fn main(args: &Args) -> Result<()> { for rule in Rule::iter() { if let Some(explanation) = rule.explanation() { - let explanation = format!("# {} ({})\n\n{}", rule.as_ref(), rule.code(), explanation); + let mut output = String::new(); + output.push_str(&format!("# {} ({})", rule.as_ref(), rule.code())); + output.push('\n'); + + let (linter, _) = Linter::parse_code(rule.code()).unwrap(); + output.push_str(&format!("Derived from the **{}** linter.", linter.name())); + output.push('\n'); + + if let Some(autofix) = rule.autofixable() { + output.push_str(match autofix.available { + AutofixAvailability::Sometimes => "Autofix is sometimes available.", + AutofixAvailability::Always => "Autofix is always available.", + }); + output.push('\n'); + } + + output.push_str(explanation.trim()); if args.dry_run { - println!("{}", explanation); + println!("{}", output); } else { fs::create_dir_all("docs/rules")?; - fs::write(format!("docs/rules/{}.md", rule.as_ref()), explanation)?; + fs::write(format!("docs/rules/{}.md", rule.as_ref()), output)?; } } } diff --git a/docs/rules/assert-raises-exception.md b/docs/rules/assert-raises-exception.md index fb8d0074bd..ecddf347a2 100644 --- a/docs/rules/assert-raises-exception.md +++ b/docs/rules/assert-raises-exception.md @@ -1,13 +1,13 @@ # assert-raises-exception (B017) - +Derived from the **flake8-bugbear** linter. ### What it does -Checks for the use of `assertRaises(Exception)`. +Checks for `self.assertRaises(Exception)`. ### Why is this bad? `assertRaises(Exception)` can lead to your test passing even if the -code being tested is never executed (e.g., due to a typo). +code being tested is never executed due to a typo. -Assert for a more specific exception (builtin or custom), use +Either assert for a more specific exception (builtin or custom), use `assertRaisesRegex` or the context manager form of `assertRaises`. ### Example @@ -18,4 +18,4 @@ self.assertRaises(Exception, foo) Use instead: ```python self.assertRaises(SomeSpecificException, foo) -``` +``` \ No newline at end of file