mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +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
|
@ -3,33 +3,33 @@ use regex::Regex;
|
||||||
use rustpython_parser::lexer::LexResult;
|
use rustpython_parser::lexer::LexResult;
|
||||||
use rustpython_parser::Tok;
|
use rustpython_parser::Tok;
|
||||||
|
|
||||||
use ruff_macros::{define_violation, derive_message_formats};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::violation::Violation;
|
use crate::violation::Violation;
|
||||||
use crate::Range;
|
use crate::Range;
|
||||||
|
|
||||||
define_violation!(
|
/// ## What it does
|
||||||
/// ## What it does
|
/// Checks for the use of type comments (e.g., `x = 1 # type: int`) in stub
|
||||||
/// Checks for the use of type comments (e.g., `x = 1 # type: int`) in stub
|
/// files.
|
||||||
/// files.
|
///
|
||||||
///
|
/// ## Why is this bad?
|
||||||
/// ## Why is this bad?
|
/// Stub (`.pyi`) files should use type annotations directly, rather
|
||||||
/// Stub (`.pyi`) files should use type annotations directly, rather
|
/// than type comments, even if they're intended to support Python 2, since
|
||||||
/// than type comments, even if they're intended to support Python 2, since
|
/// stub files are not executed at runtime. The one exception is `# type: ignore`.
|
||||||
/// stub files are not executed at runtime. The one exception is `# type: ignore`.
|
///
|
||||||
///
|
/// ## Example
|
||||||
/// ## Example
|
/// ```python
|
||||||
/// ```python
|
/// x = 1 # type: int
|
||||||
/// x = 1 # type: int
|
/// ```
|
||||||
/// ```
|
///
|
||||||
///
|
/// Use instead:
|
||||||
/// Use instead:
|
/// ```python
|
||||||
/// ```python
|
/// x: int = 1
|
||||||
/// x: int = 1
|
/// ```
|
||||||
/// ```
|
#[violation]
|
||||||
pub struct TypeCommentInStub;
|
pub struct TypeCommentInStub;
|
||||||
);
|
|
||||||
impl Violation for TypeCommentInStub {
|
impl Violation for TypeCommentInStub {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use rustpython_parser::ast::Location;
|
use rustpython_parser::ast::Location;
|
||||||
use rustpython_parser::Tok;
|
use rustpython_parser::Tok;
|
||||||
|
|
||||||
use ruff_macros::{define_violation, derive_message_formats};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
|
@ -13,11 +13,11 @@ use crate::rules::pycodestyle::helpers::{is_keyword_token, is_singleton_token};
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
use crate::violation::Violation;
|
use crate::violation::Violation;
|
||||||
|
|
||||||
define_violation!(
|
#[violation]
|
||||||
pub struct MissingWhitespace {
|
pub struct MissingWhitespace {
|
||||||
pub token: String,
|
pub token: String,
|
||||||
}
|
}
|
||||||
);
|
|
||||||
impl AlwaysAutofixableViolation for MissingWhitespace {
|
impl AlwaysAutofixableViolation for MissingWhitespace {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use rustpython_parser::ast::Location;
|
use rustpython_parser::ast::Location;
|
||||||
use rustpython_parser::Tok;
|
use rustpython_parser::Tok;
|
||||||
|
|
||||||
use ruff_macros::{define_violation, derive_message_formats};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
use crate::registry::DiagnosticKind;
|
use crate::registry::DiagnosticKind;
|
||||||
use crate::rules::pycodestyle::helpers::{
|
use crate::rules::pycodestyle::helpers::{
|
||||||
|
@ -13,9 +13,9 @@ use crate::rules::pycodestyle::helpers::{
|
||||||
use crate::violation::Violation;
|
use crate::violation::Violation;
|
||||||
|
|
||||||
// E225
|
// E225
|
||||||
define_violation!(
|
#[violation]
|
||||||
pub struct MissingWhitespaceAroundOperator;
|
pub struct MissingWhitespaceAroundOperator;
|
||||||
);
|
|
||||||
impl Violation for MissingWhitespaceAroundOperator {
|
impl Violation for MissingWhitespaceAroundOperator {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
|
@ -24,9 +24,9 @@ impl Violation for MissingWhitespaceAroundOperator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// E226
|
// E226
|
||||||
define_violation!(
|
#[violation]
|
||||||
pub struct MissingWhitespaceAroundArithmeticOperator;
|
pub struct MissingWhitespaceAroundArithmeticOperator;
|
||||||
);
|
|
||||||
impl Violation for MissingWhitespaceAroundArithmeticOperator {
|
impl Violation for MissingWhitespaceAroundArithmeticOperator {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
|
@ -35,9 +35,9 @@ impl Violation for MissingWhitespaceAroundArithmeticOperator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// E227
|
// E227
|
||||||
define_violation!(
|
#[violation]
|
||||||
pub struct MissingWhitespaceAroundBitwiseOrShiftOperator;
|
pub struct MissingWhitespaceAroundBitwiseOrShiftOperator;
|
||||||
);
|
|
||||||
impl Violation for MissingWhitespaceAroundBitwiseOrShiftOperator {
|
impl Violation for MissingWhitespaceAroundBitwiseOrShiftOperator {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
|
@ -46,9 +46,9 @@ impl Violation for MissingWhitespaceAroundBitwiseOrShiftOperator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// E228
|
// E228
|
||||||
define_violation!(
|
#[violation]
|
||||||
pub struct MissingWhitespaceAroundModuloOperator;
|
pub struct MissingWhitespaceAroundModuloOperator;
|
||||||
);
|
|
||||||
impl Violation for MissingWhitespaceAroundModuloOperator {
|
impl Violation for MissingWhitespaceAroundModuloOperator {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use rustpython_parser::ast::Location;
|
use rustpython_parser::ast::Location;
|
||||||
use rustpython_parser::Tok;
|
use rustpython_parser::Tok;
|
||||||
|
|
||||||
use ruff_macros::{define_violation, derive_message_formats};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
|
@ -11,11 +11,10 @@ use crate::registry::Diagnostic;
|
||||||
use crate::rules::pycodestyle::helpers::{is_keyword_token, is_op_token, is_soft_keyword_token};
|
use crate::rules::pycodestyle::helpers::{is_keyword_token, is_op_token, is_soft_keyword_token};
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
||||||
define_violation!(
|
#[violation]
|
||||||
pub struct WhitespaceBeforeParameters {
|
pub struct WhitespaceBeforeParameters {
|
||||||
pub bracket: String,
|
pub bracket: String,
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
impl AlwaysAutofixableViolation for WhitespaceBeforeParameters {
|
impl AlwaysAutofixableViolation for WhitespaceBeforeParameters {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
use ruff_macros::{define_violation, derive_message_formats};
|
use std::fmt;
|
||||||
|
|
||||||
use rustpython_parser::ast::{Expr, ExprKind, Location, Operator};
|
use rustpython_parser::ast::{Expr, ExprKind, Location, Operator};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
|
||||||
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
use crate::ast::helpers::unparse_expr;
|
use crate::ast::helpers::unparse_expr;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
|
@ -35,12 +37,12 @@ impl CallKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
define_violation!(
|
// TODO: document referencing [PEP 604]: https://peps.python.org/pep-0604/
|
||||||
// TODO: document referencing [PEP 604]: https://peps.python.org/pep-0604/
|
#[violation]
|
||||||
pub struct IsinstanceWithTuple {
|
pub struct IsinstanceWithTuple {
|
||||||
pub kind: CallKind,
|
pub kind: CallKind,
|
||||||
}
|
}
|
||||||
);
|
|
||||||
impl AlwaysAutofixableViolation for IsinstanceWithTuple {
|
impl AlwaysAutofixableViolation for IsinstanceWithTuple {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
|
|
|
@ -6,12 +6,12 @@ use syn::{parse_macro_input, DeriveInput, ItemFn, ItemStruct};
|
||||||
|
|
||||||
mod cache_key;
|
mod cache_key;
|
||||||
mod config;
|
mod config;
|
||||||
mod define_violation;
|
|
||||||
mod derive_message_formats;
|
mod derive_message_formats;
|
||||||
mod map_codes;
|
mod map_codes;
|
||||||
mod register_rules;
|
mod register_rules;
|
||||||
mod rule_code_prefix;
|
mod rule_code_prefix;
|
||||||
mod rule_namespace;
|
mod rule_namespace;
|
||||||
|
mod violation;
|
||||||
|
|
||||||
#[proc_macro_derive(ConfigurationOptions, attributes(option, doc, option_group))]
|
#[proc_macro_derive(ConfigurationOptions, attributes(option, doc, option_group))]
|
||||||
pub fn derive_config(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
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()
|
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
|
/// Adds an `explanation()` method from the doc comment and
|
||||||
/// `#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]`
|
/// `#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]`
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn violation(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn violation(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
let violation = parse_macro_input!(item as ItemStruct);
|
let violation = parse_macro_input!(item as ItemStruct);
|
||||||
define_violation::violation(&violation)
|
violation::violation(&violation)
|
||||||
.unwrap_or_else(syn::Error::into_compile_error)
|
.unwrap_or_else(syn::Error::into_compile_error)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::parse::{Parse, ParseStream};
|
use syn::{Attribute, Error, ItemStruct, Lit, LitStr, Meta, Result};
|
||||||
use syn::{Attribute, Error, Ident, ItemStruct, Lit, LitStr, Meta, Result, Token};
|
|
||||||
|
|
||||||
fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) -> Option<LitStr> {
|
fn parse_attr<const LEN: usize>(path: [&'static str; LEN], attr: &Attribute) -> Option<LitStr> {
|
||||||
if let Meta::NameValue(name_value) = attr.parse_meta().ok()? {
|
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
|
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
|
/// Collect all doc comment attributes into a string
|
||||||
fn get_docs(attrs: &[Attribute]) -> Result<String> {
|
fn get_docs(attrs: &[Attribute]) -> Result<String> {
|
||||||
let mut explanation = String::new();
|
let mut explanation = String::new();
|
Loading…
Add table
Add a link
Reference in a new issue