Avoid panic in unfixable redundant-numeric-union (#14402)
Some checks are pending
CI / cargo shear (push) Blocked by required conditions
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 (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz (push) Blocked by required conditions
CI / Fuzz the parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (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

## 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,
}
// 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(
checker: &Checker,
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(
checker: &Checker,
nodes: Vec<&Expr>,

View file

@ -144,23 +144,27 @@ fn check_annotation<'a>(checker: &mut Checker, annotation: &'a Expr) {
// Generate the flattened fix once.
let fix = if let &[edit_expr] = necessary_nodes.as_slice() {
// 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()),
applicability,
)
))
} else {
match union_type {
UnionKind::PEP604 => {
generate_pep604_fix(checker, necessary_nodes, annotation, applicability)
}
UnionKind::PEP604 => Some(generate_pep604_fix(
checker,
necessary_nodes,
annotation,
applicability,
)),
UnionKind::TypingUnion => {
generate_union_fix(checker, necessary_nodes, annotation, applicability)
.ok()
.unwrap()
generate_union_fix(checker, necessary_nodes, annotation, applicability).ok()
}
}
};
diagnostic.set_fix(fix);
if let Some(fix) = fix {
diagnostic.set_fix(fix);
}
};
checker.diagnostics.push(diagnostic);
@ -224,7 +228,7 @@ enum UnionKind {
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(
checker: &Checker,
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(
checker: &Checker,
nodes: Vec<&Expr>,