Avoid panic in unfixable redundant-numeric-union (#14402)

## Summary

Closes https://github.com/astral-sh/ruff/issues/14396.
This commit is contained in:
Charlie Marsh 2024-11-17 12:15:44 -05:00 committed by GitHub
parent ff19629b11
commit e1eb188049
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 13 deletions

View file

@ -142,7 +142,7 @@ enum UnionKind {
PEP604, PEP604,
} }
// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`. /// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`.
fn generate_pep604_fix( fn generate_pep604_fix(
checker: &Checker, checker: &Checker,
nodes: Vec<&Expr>, nodes: Vec<&Expr>,
@ -173,7 +173,7 @@ fn generate_pep604_fix(
) )
} }
// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`. /// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`.
fn generate_union_fix( fn generate_union_fix(
checker: &Checker, checker: &Checker,
nodes: Vec<&Expr>, nodes: Vec<&Expr>,

View file

@ -144,23 +144,27 @@ fn check_annotation<'a>(checker: &mut Checker, annotation: &'a Expr) {
// Generate the flattened fix once. // Generate the flattened fix once.
let fix = if let &[edit_expr] = necessary_nodes.as_slice() { let fix = if let &[edit_expr] = necessary_nodes.as_slice() {
// Generate a [`Fix`] for a single type expression, e.g. `int`. // Generate a [`Fix`] for a single type expression, e.g. `int`.
Fix::applicable_edit( Some(Fix::applicable_edit(
Edit::range_replacement(checker.generator().expr(edit_expr), annotation.range()), Edit::range_replacement(checker.generator().expr(edit_expr), annotation.range()),
applicability, applicability,
) ))
} else { } else {
match union_type { match union_type {
UnionKind::PEP604 => { UnionKind::PEP604 => Some(generate_pep604_fix(
generate_pep604_fix(checker, necessary_nodes, annotation, applicability) checker,
} necessary_nodes,
annotation,
applicability,
)),
UnionKind::TypingUnion => { UnionKind::TypingUnion => {
generate_union_fix(checker, necessary_nodes, annotation, applicability) generate_union_fix(checker, necessary_nodes, annotation, applicability).ok()
.ok()
.unwrap()
} }
} }
}; };
if let Some(fix) = fix {
diagnostic.set_fix(fix); diagnostic.set_fix(fix);
}
}; };
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);
@ -224,7 +228,7 @@ enum UnionKind {
PEP604, PEP604,
} }
// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`. /// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`.
fn generate_pep604_fix( fn generate_pep604_fix(
checker: &Checker, checker: &Checker,
nodes: Vec<&Expr>, nodes: Vec<&Expr>,
@ -255,7 +259,7 @@ fn generate_pep604_fix(
) )
} }
// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`. /// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`.
fn generate_union_fix( fn generate_union_fix(
checker: &Checker, checker: &Checker,
nodes: Vec<&Expr>, nodes: Vec<&Expr>,