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

Method visit_pattern

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

Source from the content-addressed store, hash-verified

2120
2121impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
2122 fn visit_pattern(&mut self, pattern: &'a Pattern) {
2123 // test_ok class_keyword_in_case_pattern
2124 // match 2:
2125 // case Class(x=x): ...
2126
2127 // test_err multiple_assignment_in_case_pattern
2128 // match 2:
2129 // case [y, z, y]: ... # MatchSequence
2130 // case [y, z, *y]: ... # MatchSequence
2131 // case [y, y, y]: ... # MatchSequence multiple
2132 // case {1: x, 2: x}: ... # MatchMapping duplicate pattern
2133 // case {1: x, **x}: ... # MatchMapping duplicate in **rest
2134 // case Class(x, x): ... # MatchClass positional
2135 // case Class(y=x, z=x): ... # MatchClass keyword
2136 // case [x] | {1: x} | Class(y=x, z=x): ... # MatchOr
2137 // case x as x: ... # MatchAs
2138
2139 // test_err multiple_starred_names_in_sequence_pattern
2140 // match subject:
2141 // case *first, *second, *third: ...
2142 match pattern {
2143 Pattern::MatchValue(_) | Pattern::MatchSingleton(_) => {}
2144 Pattern::MatchStar(ast::PatternMatchStar { name, .. }) => {
2145 if let Some(name) = name {
2146 self.insert(name);
2147 }
2148 }
2149 Pattern::MatchSequence(ast::PatternMatchSequence { patterns, .. }) => {
2150 let mut seen_star_pattern = false;
2151 for pattern in patterns {
2152 if pattern.is_match_star() {
2153 if seen_star_pattern {
2154 SemanticSyntaxChecker::add_error(
2155 self.ctx,
2156 SemanticSyntaxErrorKind::MultipleStarredNamesInSequencePattern,
2157 pattern.range(),
2158 );
2159 }
2160 seen_star_pattern = true;
2161 }
2162 self.visit_pattern(pattern);
2163 }
2164 }
2165 Pattern::MatchMapping(ast::PatternMatchMapping {
2166 keys,
2167 patterns,
2168 rest,
2169 ..
2170 }) => {
2171 for pattern in patterns {
2172 self.visit_pattern(pattern);
2173 }
2174 if let Some(rest) = rest {
2175 self.insert(rest);
2176 }
2177
2178 let mut seen = FxHashSet::default();
2179 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