From 45d374d8381f939c2bbfc3d556119de5b1fa3985 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 16 Jan 2024 15:26:18 -0500 Subject: [PATCH] [`refurb`] Avoid bailing when `reimplemented-operator` is called on function (#9556) Closes https://github.com/astral-sh/ruff/issues/9549. --- .../rules/refurb/rules/reimplemented_operator.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/ruff_linter/src/rules/refurb/rules/reimplemented_operator.rs b/crates/ruff_linter/src/rules/refurb/rules/reimplemented_operator.rs index cf11039968216..4fe969121fd0c 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/reimplemented_operator.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/reimplemented_operator.rs @@ -1,4 +1,5 @@ -use anyhow::{bail, Result}; +use anyhow::Result; + use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::{self as ast, Expr, Stmt}; @@ -80,7 +81,8 @@ pub(crate) fn reimplemented_operator(checker: &mut Checker, target: &FunctionLik }, target.range(), ); - diagnostic.try_set_fix(|| target.try_fix(operator, checker.importer(), checker.semantic())); + diagnostic + .try_set_optional_fix(|| target.try_fix(operator, checker.importer(), checker.semantic())); checker.diagnostics.push(diagnostic); } @@ -150,7 +152,7 @@ impl FunctionLike<'_> { operator: &'static str, importer: &Importer, semantic: &SemanticModel, - ) -> Result { + ) -> Result> { match self { Self::Lambda(_) => { let (edit, binding) = importer.get_or_import_symbol( @@ -158,12 +160,12 @@ impl FunctionLike<'_> { self.start(), semantic, )?; - Ok(Fix::safe_edits( + Ok(Some(Fix::safe_edits( Edit::range_replacement(binding, self.range()), [edit], - )) + ))) } - Self::Function(_) => bail!("No fix available"), + Self::Function(_) => Ok(None), } } }