[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

View file

@ -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<Fix> {
) -> Result<Option<Fix>> {
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),
}
}
}