Skip to content

Commit

Permalink
Expand target name for better rule documentation (#9302)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Dec 28, 2023
1 parent 465f835 commit 5d5f56d
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use crate::importer::{ImportRequest, Importer};
/// ## References
#[violation]
pub struct ReimplementedOperator {
target: &'static str,
operator: &'static str,
target: FunctionLikeKind,
}

impl Violation for ReimplementedOperator {
Expand All @@ -48,7 +48,14 @@ impl Violation for ReimplementedOperator {
#[derive_message_formats]
fn message(&self) -> String {
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> {
Expand Down Expand Up @@ -129,10 +136,10 @@ impl FunctionLike<'_> {
}

/// Return the display kind of the function-like node.
fn kind(&self) -> &'static str {
fn kind(&self) -> FunctionLikeKind {
match self {
Self::Lambda(_) => "lambda",
Self::Function(_) => "function",
Self::Lambda(_) => FunctionLikeKind::Lambda,
Self::Function(_) => FunctionLikeKind::Function,
}
}

Expand Down Expand Up @@ -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.
fn unary_op(expr: &ast::ExprUnaryOp, params: &ast::Parameters) -> Option<&'static str> {
let [arg] = params.args.as_slice() else {
Expand Down

0 comments on commit 5d5f56d

Please sign in to comment.