Repository: astral-sh/ruff
File: crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs (Rust)

Code before:
                        // Unwrap all nested Optional[...] and wrap once as `X | None`.
                        let mut inner = slice;
                        while let Expr::Subscript(ast::ExprSubscript { value, slice, .. }) = inner {
                            if let Some(Pep604Operator::Optional) =
                                to_pep604_operator(value, slice, checker.semantic())
                            {
                                inner = slice;
                            } else {
                                break;
                            }
                        }

                        // If the inner expression is a `BitOr` union that already
                        // contains `None`, strip it out and re-add it only at the end.
                        // This avoids generating `None | None` which is a runtime
                        // `TypeError`. For example, `Optional[None | int]` should
                        // become `int | None`, not `None | int | None`.
                        let fix_expr = if let Expr::BinOp(ast::ExprBinOp {
                            op: Operator::BitOr,
                            ..
                        }) = inner
                        {
                            let elements = collect_bitor_elements(inner);
                            if elements
                                .iter()
                                .any(|element| matches!(element, Expr::NoneLiteral(_)))
                            {
                                let non_none: Vec<Expr> = elements
                                    .into_iter()
                                    .filter(|element| !matches!(element, Expr::NoneLiteral(_)))
                                    .cloned()
                                    .collect();
                                if non_none.is_empty() {
                                    // All elements were `None`; don't provide a fix.
                                    None
                                } else {
                                    Some(pep_604_optional(&pep_604_union(&non_none)))
                                }
                            } else {
                                Some(pep_604_optional(inner))
                            }
                        } else {
                            Some(pep_604_optional(inner))
                        };

                        if let Some(fix_expr) = fix_expr {
                            diagnostic.set_fix(Fix::applicable_edit(
                                Edit::range_replacement(
                                    pad(
                                        checker.generator().expr(&fix_expr),
                                        expr.range(),

Review comment (suggestion):
```suggestion
                                .any(|element| element.is_none_literal_expr())
```

Code after:
                        // Unwrap all nested Optional[...] and wrap once as `X | None`.
                        let mut inner = slice;
                        while let Expr::Subscript(ast::ExprSubscript { value, slice, .. }) = inner {
                            if let Some(Pep604Operator::Optional) =
                                to_pep604_operator(value, slice, checker.semantic())
                            {
                                inner = slice;
                            } else {
                                break;
                            }
                        }

                        // If the inner expression is a `BitOr` union that already
                        // contains `None`, strip it out and re-add it only at the end.
                        // This avoids generating `None | None` which is a runtime
                        // `TypeError`. For example, `Optional[None | int]` should
                        // become `int | None`, not `None | int | None`.
                        let fix_expr = if let Expr::BinOp(ast::ExprBinOp {
                            op: Operator::BitOr,
                            ..
                        }) = inner
                        {
                            let elements = collect_non_none(inner);
                            if elements.is_empty() {
                                // All elements were `None`; don't provide a fix.
                                None
                            } else {
                                Some(pep_604_optional(&pep_604_union(&elements)))
                            }
                        } else {
                            Some(pep_604_optional(inner))
                        };

                        if let Some(fix_expr) = fix_expr {
                            diagnostic.set_fix(Fix::applicable_edit(
                                Edit::range_replacement(
                                    pad(
                                        checker.generator().expr(&fix_expr),
                                        expr.range(),
                                        checker.locator(),
                                    ),
                                    expr.range(),
                                ),
                                applicability,
                            ));
                        }
                    }
                }
            }
        }
        Pep604Operator::Union => {