diff --git a/crates/ruff_python_parser/resources/inline/ok/match_as_pattern.py b/crates/ruff_python_parser/resources/inline/ok/match_as_pattern.py new file mode 100644 index 00000000000000..378ec61b9928d4 --- /dev/null +++ b/crates/ruff_python_parser/resources/inline/ok/match_as_pattern.py @@ -0,0 +1,3 @@ +match foo: + case foo_bar: ... + case _: ... diff --git a/crates/ruff_python_parser/src/parser/pattern.rs b/crates/ruff_python_parser/src/parser/pattern.rs index 45b57e494e108e..7d8f69c9b44d6c 100644 --- a/crates/ruff_python_parser/src/parser/pattern.rs +++ b/crates/ruff_python_parser/src/parser/pattern.rs @@ -494,10 +494,14 @@ impl<'src> Parser<'src> { }; let range = self.node_range(start); + // test_ok match_as_pattern + // match foo: + // case foo_bar: ... + // case _: ... Pattern::MatchAs(ast::PatternMatchAs { range, pattern: None, - name: if name.contains('_') { + name: if &*name == "_" { None } else { Some(ast::Identifier { diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern.py.snap b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern.py.snap new file mode 100644 index 00000000000000..4fea0dcc1870c4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern.py.snap @@ -0,0 +1,80 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +input_file: crates/ruff_python_parser/resources/inline/ok/match_as_pattern.py +--- +## AST + +``` +Module( + ModModule { + range: 0..49, + body: [ + Match( + StmtMatch { + range: 0..48, + subject: Name( + ExprName { + range: 6..9, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 15..32, + pattern: MatchAs( + PatternMatchAs { + range: 20..27, + pattern: None, + name: Some( + Identifier { + id: "foo_bar", + range: 20..27, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 29..32, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 29..32, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 37..48, + pattern: MatchAs( + PatternMatchAs { + range: 42..43, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 45..48, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 45..48, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +```