From d3b6e8f58bace13b3f5c791880b61b89aef8d2b3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 1 Sep 2024 02:55:24 +0100 Subject: [PATCH] Remove `pylint::helpers::CmpOpExt` (#13189) --- .../ruff_linter/src/rules/pylint/helpers.rs | 32 +------------------ .../pylint/rules/comparison_of_constant.rs | 4 +-- .../pylint/rules/comparison_with_itself.rs | 5 ++- 3 files changed, 4 insertions(+), 37 deletions(-) diff --git a/crates/ruff_linter/src/rules/pylint/helpers.rs b/crates/ruff_linter/src/rules/pylint/helpers.rs index 6c41fad31d3ceb..081f034f97191a 100644 --- a/crates/ruff_linter/src/rules/pylint/helpers.rs +++ b/crates/ruff_linter/src/rules/pylint/helpers.rs @@ -1,8 +1,6 @@ -use std::fmt; - use ruff_python_ast as ast; use ruff_python_ast::visitor::Visitor; -use ruff_python_ast::{visitor, Arguments, CmpOp, Expr, Stmt}; +use ruff_python_ast::{visitor, Arguments, Expr, Stmt}; use ruff_python_semantic::analyze::function_type; use ruff_python_semantic::{ScopeKind, SemanticModel}; use ruff_text_size::TextRange; @@ -57,34 +55,6 @@ pub(super) fn in_dunder_method( true } -/// A wrapper around [`CmpOp`] that implements `Display`. -#[derive(Debug)] -pub(super) struct CmpOpExt(CmpOp); - -impl From<&CmpOp> for CmpOpExt { - fn from(cmp_op: &CmpOp) -> Self { - CmpOpExt(*cmp_op) - } -} - -impl fmt::Display for CmpOpExt { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let representation = match self.0 { - CmpOp::Eq => "==", - CmpOp::NotEq => "!=", - CmpOp::Lt => "<", - CmpOp::LtE => "<=", - CmpOp::Gt => ">", - CmpOp::GtE => ">=", - CmpOp::Is => "is", - CmpOp::IsNot => "is not", - CmpOp::In => "in", - CmpOp::NotIn => "not in", - }; - write!(f, "{representation}") - } -} - /// Visitor to track reads from an iterable in a loop. #[derive(Debug)] pub(crate) struct SequenceIndexVisitor<'a> { diff --git a/crates/ruff_linter/src/rules/pylint/rules/comparison_of_constant.rs b/crates/ruff_linter/src/rules/pylint/rules/comparison_of_constant.rs index aaebf29fceddeb..df46b79883a7d4 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/comparison_of_constant.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/comparison_of_constant.rs @@ -6,7 +6,6 @@ use ruff_macros::{derive_message_formats, violation}; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; -use crate::rules::pylint::helpers::CmpOpExt; /// ## What it does /// Checks for comparisons between constants. @@ -45,8 +44,7 @@ impl Violation for ComparisonOfConstant { } = self; format!( - "Two constants compared in a comparison, consider replacing `{left_constant} {} {right_constant}`", - CmpOpExt::from(op) + "Two constants compared in a comparison, consider replacing `{left_constant} {op} {right_constant}`", ) } } diff --git a/crates/ruff_linter/src/rules/pylint/rules/comparison_with_itself.rs b/crates/ruff_linter/src/rules/pylint/rules/comparison_with_itself.rs index 2e28bddc684f8c..27533f35e2eab7 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/comparison_with_itself.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/comparison_with_itself.rs @@ -7,7 +7,6 @@ use ruff_python_ast::{CmpOp, Expr}; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; -use crate::rules::pylint::helpers::CmpOpExt; /// ## What it does /// Checks for operations that compare a name to itself. @@ -66,7 +65,7 @@ pub(crate) fn comparison_with_itself( let actual = format!( "{} {} {}", checker.locator().slice(left), - CmpOpExt::from(op), + op, checker.locator().slice(right) ); checker.diagnostics.push(Diagnostic::new( @@ -114,7 +113,7 @@ pub(crate) fn comparison_with_itself( let actual = format!( "{} {} {}", checker.locator().slice(left), - CmpOpExt::from(op), + op, checker.locator().slice(right) ); checker.diagnostics.push(Diagnostic::new(