Skip to content

Commit e1eb188

Browse files
Avoid panic in unfixable redundant-numeric-union (#14402)
## Summary Closes #14396.
1 parent ff19629 commit e1eb188

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

crates/ruff_linter/src/rules/flake8_pyi/rules/duplicate_union_member.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ enum UnionKind {
142142
PEP604,
143143
}
144144

145-
// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`.
145+
/// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`.
146146
fn generate_pep604_fix(
147147
checker: &Checker,
148148
nodes: Vec<&Expr>,
@@ -173,7 +173,7 @@ fn generate_pep604_fix(
173173
)
174174
}
175175

176-
// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`.
176+
/// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`.
177177
fn generate_union_fix(
178178
checker: &Checker,
179179
nodes: Vec<&Expr>,

crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_numeric_union.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,27 @@ fn check_annotation<'a>(checker: &mut Checker, annotation: &'a Expr) {
144144
// Generate the flattened fix once.
145145
let fix = if let &[edit_expr] = necessary_nodes.as_slice() {
146146
// Generate a [`Fix`] for a single type expression, e.g. `int`.
147-
Fix::applicable_edit(
147+
Some(Fix::applicable_edit(
148148
Edit::range_replacement(checker.generator().expr(edit_expr), annotation.range()),
149149
applicability,
150-
)
150+
))
151151
} else {
152152
match union_type {
153-
UnionKind::PEP604 => {
154-
generate_pep604_fix(checker, necessary_nodes, annotation, applicability)
155-
}
153+
UnionKind::PEP604 => Some(generate_pep604_fix(
154+
checker,
155+
necessary_nodes,
156+
annotation,
157+
applicability,
158+
)),
156159
UnionKind::TypingUnion => {
157-
generate_union_fix(checker, necessary_nodes, annotation, applicability)
158-
.ok()
159-
.unwrap()
160+
generate_union_fix(checker, necessary_nodes, annotation, applicability).ok()
160161
}
161162
}
162163
};
163-
diagnostic.set_fix(fix);
164+
165+
if let Some(fix) = fix {
166+
diagnostic.set_fix(fix);
167+
}
164168
};
165169

166170
checker.diagnostics.push(diagnostic);
@@ -224,7 +228,7 @@ enum UnionKind {
224228
PEP604,
225229
}
226230

227-
// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`.
231+
/// Generate a [`Fix`] for two or more type expressions, e.g. `int | float | complex`.
228232
fn generate_pep604_fix(
229233
checker: &Checker,
230234
nodes: Vec<&Expr>,
@@ -255,7 +259,7 @@ fn generate_pep604_fix(
255259
)
256260
}
257261

258-
// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`.
262+
/// Generate a [`Fix`] for two or more type expresisons, e.g. `typing.Union[int, float, complex]`.
259263
fn generate_union_fix(
260264
checker: &Checker,
261265
nodes: Vec<&Expr>,

0 commit comments

Comments
 (0)