Derive explanation method on Rule struct via rustdoc (#2642)

```console
❯ cargo run rule B017
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/ruff rule B017`
no-assert-raises-exception

Code: B017 (flake8-bugbear)

### What it does
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 due to a typo.

Either assert for a more specific exception (builtin or custom), use
`assertRaisesRegex` or the context manager form of `assertRaises`.
```
This commit is contained in:
Charlie Marsh 2023-02-07 17:23:29 -05:00 committed by GitHub
parent 8fd29b3b60
commit f1cdd108e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
262 changed files with 400 additions and 537 deletions

View file

@ -0,0 +1,75 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{Attribute, Error, Ident, Lit, LitStr, Meta, Result, Token};
fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) -> Option<LitStr> {
if let Meta::NameValue(name_value) = attr.parse_meta().ok()? {
let path_idents = name_value
.path
.segments
.iter()
.map(|segment| &segment.ident);
if itertools::equal(path_idents, path) {
if let Lit::Str(lit) = name_value.lit {
return Some(lit);
}
}
}
None
}
pub struct LintMeta {
explanation: String,
name: Ident,
}
impl Parse for LintMeta {
fn parse(input: ParseStream) -> Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
let mut in_code = false;
let mut explanation = String::new();
for attr in &attrs {
if let Some(lit) = parse_attr(["doc"], attr) {
let value = lit.value();
let line = value.strip_prefix(' ').unwrap_or(&value);
if line.starts_with("```") {
explanation += "```\n";
in_code = !in_code;
} else if !(in_code && line.starts_with("# ")) {
explanation += line;
explanation.push('\n');
}
} else {
return Err(Error::new_spanned(attr, "unexpected attribute"));
}
}
input.parse::<Token![pub]>()?;
input.parse::<Token![struct]>()?;
let name = input.parse()?;
// Ignore the rest of the input.
input.parse::<TokenStream>()?;
Ok(Self { explanation, name })
}
}
pub fn define_violation(input: &TokenStream, meta: LintMeta) -> TokenStream {
let LintMeta { explanation, name } = meta;
let output = quote! {
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#input
impl #name {
pub fn explanation() -> Option<&'static str> {
Some(#explanation)
}
}
};
output
}