mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 14:51:25 +00:00
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:
parent
8fd29b3b60
commit
f1cdd108e6
262 changed files with 400 additions and 537 deletions
|
@ -10,6 +10,7 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
|
|||
let mut diagnostic_kind_variants = quote!();
|
||||
let mut rule_message_formats_match_arms = quote!();
|
||||
let mut rule_autofixable_match_arms = quote!();
|
||||
let mut rule_explanation_match_arms = quote!();
|
||||
let mut rule_code_match_arms = quote!();
|
||||
let mut rule_from_code_match_arms = quote!();
|
||||
let mut diagnostic_kind_code_match_arms = quote!();
|
||||
|
@ -32,6 +33,7 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
|
|||
.extend(quote! {#(#attr)* Self::#name => <#path as Violation>::message_formats(),});
|
||||
rule_autofixable_match_arms
|
||||
.extend(quote! {#(#attr)* Self::#name => <#path as Violation>::AUTOFIX,});
|
||||
rule_explanation_match_arms.extend(quote! {#(#attr)* Self::#name => #path::explanation(),});
|
||||
rule_code_match_arms.extend(quote! {#(#attr)* Self::#name => #code_str,});
|
||||
rule_from_code_match_arms.extend(quote! {#(#attr)* #code_str => Ok(Rule::#name), });
|
||||
diagnostic_kind_code_match_arms
|
||||
|
@ -97,6 +99,10 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
|
|||
match self { #rule_message_formats_match_arms }
|
||||
}
|
||||
|
||||
pub fn explanation(&self) -> Option<&'static str> {
|
||||
match self { #rule_explanation_match_arms }
|
||||
}
|
||||
|
||||
pub fn autofixable(&self) -> Option<crate::violation::AutofixKind> {
|
||||
match self { #rule_autofixable_match_arms }
|
||||
}
|
||||
|
|
75
crates/ruff_macros/src/define_violation.rs
Normal file
75
crates/ruff_macros/src/define_violation.rs
Normal 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
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
//! This crate implements internal macros for the `ruff` library.
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{parse_macro_input, DeriveInput, ItemFn};
|
||||
|
||||
mod config;
|
||||
mod define_rule_mapping;
|
||||
mod define_violation;
|
||||
mod derive_message_formats;
|
||||
mod rule_code_prefix;
|
||||
mod rule_namespace;
|
||||
|
@ -24,6 +24,13 @@ pub fn define_rule_mapping(item: proc_macro::TokenStream) -> proc_macro::TokenSt
|
|||
define_rule_mapping::define_rule_mapping(&mapping).into()
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn define_violation(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let cloned = item.clone();
|
||||
let meta = parse_macro_input!(cloned as define_violation::LintMeta);
|
||||
define_violation::define_violation(&item.into(), meta).into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(RuleNamespace, attributes(prefix))]
|
||||
pub fn derive_rule_namespace(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
@ -34,7 +41,10 @@ pub fn derive_rule_namespace(input: proc_macro::TokenStream) -> proc_macro::Toke
|
|||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn derive_message_formats(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
pub fn derive_message_formats(
|
||||
_attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let func = parse_macro_input!(item as ItemFn);
|
||||
derive_message_formats::derive_message_formats(&func).into()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue