Use Diagnostic::try_set_fix in bad-generator-return-type (#15873)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

This commit is contained in:
Alex Waygood 2025-02-01 15:44:42 +00:00 committed by GitHub
parent 942d7f395a
commit bcdb3f9840
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -220,16 +220,18 @@ pub(crate) fn bad_generator_return_type(
},
function_def.identifier(),
);
if let Some(fix) = generate_fix(
function_def,
returns,
yield_type_info,
module,
member,
checker,
) {
diagnostic.set_fix(fix);
};
diagnostic.try_set_fix(|| {
generate_fix(
function_def,
returns,
yield_type_info,
module,
member,
checker,
)
});
checker.diagnostics.push(diagnostic);
}
@ -246,17 +248,14 @@ fn generate_fix(
module: Module,
member: Generator,
checker: &Checker,
) -> Option<Fix> {
) -> anyhow::Result<Fix> {
let expr = map_subscript(returns);
let (import_edit, binding) = checker
.importer()
.get_or_import_symbol(
&ImportRequest::import_from(&module.to_string(), &member.to_iter().to_string()),
expr.start(),
checker.semantic(),
)
.ok()?;
let (import_edit, binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import_from(&module.to_string(), &member.to_iter().to_string()),
expr.start(),
checker.semantic(),
)?;
let binding_edit = Edit::range_replacement(binding, expr.range());
let yield_edit = yield_type_info.map(|yield_type_info| {
Edit::range_replacement(
@ -272,7 +271,7 @@ fn generate_fix(
Applicability::Unsafe
};
Some(Fix::applicable_edits(
Ok(Fix::applicable_edits(
import_edit,
std::iter::once(binding_edit).chain(yield_edit),
applicability,