Expand target name for better rule documentation (#9302)

This commit is contained in:
Charlie Marsh 2023-12-28 10:58:26 -04:00 committed by GitHub
parent 465f835cf9
commit 5d5f56d563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 5 deletions

View File

@ -38,8 +38,8 @@ use crate::importer::{ImportRequest, Importer};
/// ## References /// ## References
#[violation] #[violation]
pub struct ReimplementedOperator { pub struct ReimplementedOperator {
target: &'static str,
operator: &'static str, operator: &'static str,
target: FunctionLikeKind,
} }
impl Violation for ReimplementedOperator { impl Violation for ReimplementedOperator {
@ -48,7 +48,14 @@ impl Violation for ReimplementedOperator {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
let ReimplementedOperator { operator, target } = self; let ReimplementedOperator { operator, target } = self;
format!("Use `operator.{operator}` instead of defining a {target}") match target {
FunctionLikeKind::Function => {
format!("Use `operator.{operator}` instead of defining a function")
}
FunctionLikeKind::Lambda => {
format!("Use `operator.{operator}` instead of defining a lambda")
}
}
} }
fn fix_title(&self) -> Option<String> { fn fix_title(&self) -> Option<String> {
@ -129,10 +136,10 @@ impl FunctionLike<'_> {
} }
/// Return the display kind of the function-like node. /// Return the display kind of the function-like node.
fn kind(&self) -> &'static str { fn kind(&self) -> FunctionLikeKind {
match self { match self {
Self::Lambda(_) => "lambda", Self::Lambda(_) => FunctionLikeKind::Lambda,
Self::Function(_) => "function", Self::Function(_) => FunctionLikeKind::Function,
} }
} }
@ -171,6 +178,12 @@ fn get_operator(expr: &Expr, params: &ast::Parameters) -> Option<&'static str> {
} }
} }
#[derive(Debug, PartialEq, Eq)]
enum FunctionLikeKind {
Lambda,
Function,
}
/// Return the name of the `operator` implemented by the given unary expression. /// Return the name of the `operator` implemented by the given unary expression.
fn unary_op(expr: &ast::ExprUnaryOp, params: &ast::Parameters) -> Option<&'static str> { fn unary_op(expr: &ast::ExprUnaryOp, params: &ast::Parameters) -> Option<&'static str> {
let [arg] = params.args.as_slice() else { let [arg] = params.args.as_slice() else {