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

Method visit_pattern

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

Source from the content-addressed store, hash-verified

2199
2200impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
2201 fn visit_pattern(&mut self, pattern: &'a Pattern) {
2202 // test_ok class_keyword_in_case_pattern
2203 // match 2:
2204 // case Class(x=x): ...
2205
2206 // test_err multiple_assignment_in_case_pattern
2207 // match 2:
2208 // case [y, z, y]: ... # MatchSequence
2209 // case [y, z, *y]: ... # MatchSequence
2210 // case [y, y, y]: ... # MatchSequence multiple
2211 // case {1: x, 2: x}: ... # MatchMapping duplicate pattern
2212 // case {1: x, **x}: ... # MatchMapping duplicate in **rest
2213 // case Class(x, x): ... # MatchClass positional
2214 // case Class(y=x, z=x): ... # MatchClass keyword
2215 // case [x] | {1: x} | Class(y=x, z=x): ... # MatchOr
2216 // case x as x: ... # MatchAs
2217
2218 // test_err multiple_starred_names_in_sequence_pattern
2219 // match subject:
2220 // case *first, *second, *third: ...
2221 match pattern {
2222 Pattern::MatchValue(_) | Pattern::MatchSingleton(_) => {}
2223 Pattern::MatchStar(ast::PatternMatchStar { name, .. }) => {
2224 if let Some(name) = name {
2225 self.insert(name);
2226 }
2227 }
2228 Pattern::MatchSequence(ast::PatternMatchSequence { patterns, .. }) => {
2229 let mut seen_star_pattern = false;
2230 for pattern in patterns {
2231 if pattern.is_match_star() {
2232 if seen_star_pattern {
2233 SemanticSyntaxChecker::add_error(
2234 self.ctx,
2235 SemanticSyntaxErrorKind::MultipleStarredNamesInSequencePattern,
2236 pattern.range(),
2237 );
2238 }
2239 seen_star_pattern = true;
2240 }
2241 self.visit_pattern(pattern);
2242 }
2243 }
2244 Pattern::MatchMapping(ast::PatternMatchMapping {
2245 keys,
2246 patterns,
2247 rest,
2248 ..
2249 }) => {
2250 for pattern in patterns {
2251 self.visit_pattern(pattern);
2252 }
2253 if let Some(rest) = rest {
2254 self.insert(rest);
2255 }
2256
2257 let mut seen = FxHashSet::default();
2258 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