MCPcopy Create free account
hub / github.com/astral-sh/ruff / visit_pattern

Method visit_pattern

crates/ruff_python_parser/src/semantic_errors.rs:2286–2495  ·  view source on GitHub ↗
(&mut self, pattern: &'a Pattern)

Source from the content-addressed store, hash-verified

2284
2285impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
2286 fn visit_pattern(&mut self, pattern: &'a Pattern) {
2287 // test_ok class_keyword_in_case_pattern
2288 // match 2:
2289 // case Class(x=x): ...
2290
2291 // test_err multiple_assignment_in_case_pattern
2292 // match 2:
2293 // case [y, z, y]: ... # MatchSequence
2294 // case [y, z, *y]: ... # MatchSequence
2295 // case [y, y, y]: ... # MatchSequence multiple
2296 // case {1: x, 2: x}: ... # MatchMapping duplicate pattern
2297 // case {1: x, **x}: ... # MatchMapping duplicate in **rest
2298 // case Class(x, x): ... # MatchClass positional
2299 // case Class(y=x, z=x): ... # MatchClass keyword
2300 // case [x] | {1: x} | Class(y=x, z=x): ... # MatchOr
2301 // case x as x: ... # MatchAs
2302
2303 // test_err multiple_starred_names_in_sequence_pattern
2304 // match subject:
2305 // case *first, *second, *third: ...
2306 match pattern {
2307 Pattern::MatchValue(_) | Pattern::MatchSingleton(_) => {}
2308 Pattern::MatchStar(ast::PatternMatchStar { name, .. }) => {
2309 if let Some(name) = name {
2310 self.insert(name);
2311 }
2312 }
2313 Pattern::MatchSequence(ast::PatternMatchSequence { patterns, .. }) => {
2314 let mut seen_star_pattern = false;
2315 for pattern in patterns {
2316 if pattern.is_match_star() {
2317 if seen_star_pattern {
2318 SemanticSyntaxChecker::add_error(
2319 self.ctx,
2320 SemanticSyntaxErrorKind::MultipleStarredNamesInSequencePattern,
2321 pattern.range(),
2322 );
2323 }
2324 seen_star_pattern = true;
2325 }
2326 self.visit_pattern(pattern);
2327 }
2328 }
2329 Pattern::MatchMapping(ast::PatternMatchMapping {
2330 keys,
2331 patterns,
2332 rest,
2333 ..
2334 }) => {
2335 for pattern in patterns {
2336 self.visit_pattern(pattern);
2337 }
2338 if let Some(rest) = rest {
2339 self.insert(rest);
2340 }
2341
2342 let mut seen = FxHashSet::default();
2343 for key in keys

Callers 1

check_stmtMethod · 0.45

Calls 14

is_match_starMethod · 0.80
is_literal_exprMethod · 0.80
is_unary_op_exprMethod · 0.80
is_bin_op_exprMethod · 0.80
defaultFunction · 0.50
insertMethod · 0.45
rangeMethod · 0.45
filterMethod · 0.45
iterMethod · 0.45
to_stringMethod · 0.45
sourceMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected