Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/semantic_index/member.rs (Rust)

Code before:
                        ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
                            value: ast::Number::Int(index),
                            ..
                        }) => {
                            let _ = write!(path, "{index}");
                            segments
                                .push(SegmentInfo::new(SegmentKind::IntSubscript, start_offset));
                        }
                        // Handle negative integer subscripts, like `x[-1]`.
                        ast::Expr::UnaryOp(ast::ExprUnaryOp {
                            op: ast::UnaryOp::USub,
                            operand,
                            ..
                        }) => match operand.as_ref() {
                            ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
                                value: ast::Number::Int(index),
                                ..
                            }) => {
                                let _ = write!(path, "-{index}");
                                segments.push(SegmentInfo::new(
                                    SegmentKind::IntSubscript,
                                    start_offset,
                                ));
                            }
                            _ => return None,
                        },
                        ast::Expr::StringLiteral(string) => {
                            let _ = write!(path, "{}", string.value);
                            segments
                                .push(SegmentInfo::new(SegmentKind::StringSubscript, start_offset));
                        }
                        _ => {
                            return None;
                        }
                    }

                    Some((path, segments))
                }
                _ => None,
            }
        }

        let (path, segments) = visit(expression)?;

        if segments.is_empty() {
            None
        } else {
            Some(Self {
                path,
                segments: Segments::from_vec(segments),
            })

Review comment (suggestion):
For completeness (though it's obviously unlikely to come up), shall we also handle unary `+`? `x[+5]`, etc.

Code after:
                        ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
                            value: ast::Number::Int(index),
                            ..
                        }) => {
                            let _ = write!(path, "{index}");
                            segments
                                .push(SegmentInfo::new(SegmentKind::IntSubscript, start_offset));
                        }
                        // Handle negative integer subscripts, like `x[-1]`.
                        ast::Expr::UnaryOp(ast::ExprUnaryOp {
                            op: ast::UnaryOp::USub,
                            operand,
                            ..
                        }) => match operand.as_ref() {
                            ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
                                value: ast::Number::Int(index),
                                ..
                            }) => {
                                let _ = write!(path, "-{index}");
                                segments.push(SegmentInfo::new(
                                    SegmentKind::IntSubscript,
                                    start_offset,
                                ));
                            }
                            _ => return None,
                        },
                        // Handle positive integer subscripts with explicit plus, like `x[+1]`.
                        ast::Expr::UnaryOp(ast::ExprUnaryOp {
                            op: ast::UnaryOp::UAdd,
                            operand,
                            ..
                        }) => match operand.as_ref() {
                            ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
                                value: ast::Number::Int(index),
                                ..
                            }) => {
                                let _ = write!(path, "{index}");
                                segments.push(SegmentInfo::new(
                                    SegmentKind::IntSubscript,
                                    start_offset,
                                ));
                            }
                            _ => return None,
                        },
                        ast::Expr::StringLiteral(string) => {
                            let _ = write!(path, "{}", string.value);
                            segments
                                .push(SegmentInfo::new(SegmentKind::StringSubscript, start_offset));
                        }
                        _ => {
                            return None;