mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 13:33:50 +00:00
Remove old define_violation!
(in favor of #[violation]
) (#3310)
This commit is contained in:
parent
d1c48016eb
commit
709dba2e71
7 changed files with 59 additions and 119 deletions
|
@ -6,12 +6,12 @@ use syn::{parse_macro_input, DeriveInput, ItemFn, ItemStruct};
|
|||
|
||||
mod cache_key;
|
||||
mod config;
|
||||
mod define_violation;
|
||||
mod derive_message_formats;
|
||||
mod map_codes;
|
||||
mod register_rules;
|
||||
mod rule_code_prefix;
|
||||
mod rule_namespace;
|
||||
mod violation;
|
||||
|
||||
#[proc_macro_derive(ConfigurationOptions, attributes(option, doc, option_group))]
|
||||
pub fn derive_config(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
|
@ -38,19 +38,12 @@ pub fn register_rules(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
|||
register_rules::register_rules(&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()
|
||||
}
|
||||
|
||||
/// Adds an `explanation()` method from the doc comment and
|
||||
/// `#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]`
|
||||
#[proc_macro_attribute]
|
||||
pub fn violation(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let violation = parse_macro_input!(item as ItemStruct);
|
||||
define_violation::violation(&violation)
|
||||
violation::violation(&violation)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
.into()
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
use syn::{Attribute, Error, Ident, ItemStruct, Lit, LitStr, Meta, Result, Token};
|
||||
use syn::{Attribute, Error, ItemStruct, Lit, LitStr, Meta, Result};
|
||||
|
||||
fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) -> Option<LitStr> {
|
||||
if let Meta::NameValue(name_value) = attr.parse_meta().ok()? {
|
||||
|
@ -21,59 +20,6 @@ fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) ->
|
|||
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 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);
|
||||
explanation.push_str(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;
|
||||
if explanation.is_empty() {
|
||||
quote! {
|
||||
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#input
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#input
|
||||
|
||||
impl #name {
|
||||
pub fn explanation() -> Option<&'static str> {
|
||||
Some(#explanation)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect all doc comment attributes into a string
|
||||
fn get_docs(attrs: &[Attribute]) -> Result<String> {
|
||||
let mut explanation = String::new();
|
Loading…
Add table
Add a link
Reference in a new issue