[`refurb`] Avoid bailing when `reimplemented-operator` is called on function (#9556)

Closes https://github.com/astral-sh/ruff/issues/9549.
This commit is contained in:
Charlie Marsh 2024-01-16 15:26:18 -05:00 committed by GitHub
parent 8788d57030
commit 45d374d838
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 6 deletions

View File

@ -1,4 +1,5 @@
use anyhow::{bail, Result}; use anyhow::Result;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Expr, Stmt}; 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(), 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); checker.diagnostics.push(diagnostic);
} }
@ -150,7 +152,7 @@ impl FunctionLike<'_> {
operator: &'static str, operator: &'static str,
importer: &Importer, importer: &Importer,
semantic: &SemanticModel, semantic: &SemanticModel,
) -> Result<Fix> { ) -> Result<Option<Fix>> {
match self { match self {
Self::Lambda(_) => { Self::Lambda(_) => {
let (edit, binding) = importer.get_or_import_symbol( let (edit, binding) = importer.get_or_import_symbol(
@ -158,12 +160,12 @@ impl FunctionLike<'_> {
self.start(), self.start(),
semantic, semantic,
)?; )?;
Ok(Fix::safe_edits( Ok(Some(Fix::safe_edits(
Edit::range_replacement(binding, self.range()), Edit::range_replacement(binding, self.range()),
[edit], [edit],
)) )))
} }
Self::Function(_) => bail!("No fix available"), Self::Function(_) => Ok(None),
} }
} }
} }