mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:18 +00:00
Use a derive macro for Violations (#14557)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
6fd10e2fe7
commit
14ba469fc0
629 changed files with 2555 additions and 2562 deletions
|
@ -2,7 +2,7 @@ pub use diagnostic::{Diagnostic, DiagnosticKind};
|
|||
pub use edit::Edit;
|
||||
pub use fix::{Applicability, Fix, IsolationLevel};
|
||||
pub use source_map::{SourceMap, SourceMarker};
|
||||
pub use violation::{AlwaysFixableViolation, FixAvailability, Violation};
|
||||
pub use violation::{AlwaysFixableViolation, FixAvailability, Violation, ViolationMetadata};
|
||||
|
||||
mod diagnostic;
|
||||
mod edit;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::DiagnosticKind;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
|
@ -17,7 +18,16 @@ impl Display for FixAvailability {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait Violation: Debug + PartialEq + Eq {
|
||||
pub trait ViolationMetadata {
|
||||
/// Returns the rule name of this violation
|
||||
fn rule_name() -> &'static str;
|
||||
|
||||
/// Returns an explanation of what this violation catches,
|
||||
/// why it's bad, and what users should do instead.
|
||||
fn explain() -> Option<&'static str>;
|
||||
}
|
||||
|
||||
pub trait Violation: ViolationMetadata {
|
||||
/// `None` in the case a fix is never available or otherwise Some
|
||||
/// [`FixAvailability`] describing the available fix.
|
||||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;
|
||||
|
@ -41,7 +51,7 @@ pub trait Violation: Debug + PartialEq + Eq {
|
|||
|
||||
/// This trait exists just to make implementing the [`Violation`] trait more
|
||||
/// convenient for violations that can always be fixed.
|
||||
pub trait AlwaysFixableViolation: Debug + PartialEq + Eq {
|
||||
pub trait AlwaysFixableViolation: ViolationMetadata {
|
||||
/// The message used to describe the violation.
|
||||
fn message(&self) -> String;
|
||||
|
||||
|
@ -69,3 +79,16 @@ impl<V: AlwaysFixableViolation> Violation for V {
|
|||
<Self as AlwaysFixableViolation>::message_formats()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for DiagnosticKind
|
||||
where
|
||||
T: Violation,
|
||||
{
|
||||
fn from(value: T) -> Self {
|
||||
Self {
|
||||
body: Violation::message(&value),
|
||||
suggestion: Violation::fix_title(&value),
|
||||
name: T::rule_name().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue