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

Method visit_stmt

crates/ruff_python_parser/src/semantic_errors.rs:884–928  ·  view source on GitHub ↗

Check `stmt` for semantic syntax errors and update the checker's internal state. Note that this method should only be called when traversing `stmt` *and* its children. For example, if traversal of function bodies needs to be deferred, avoid calling `visit_stmt` on the function itself until the deferred body is visited too. Failing to defer `visit_stmt` in this case will break any internal state t

(&mut self, stmt: &ast::Stmt, ctx: &Ctx)

Source from the content-addressed store, hash-verified

882 /// this case will break any internal state that depends on function scopes, such as `async`
883 /// context detection.
884 pub fn visit_stmt<Ctx: SemanticSyntaxContext>(&mut self, stmt: &ast::Stmt, ctx: &Ctx) {
885 // check for errors
886 self.check_stmt(stmt, ctx);
887
888 // update internal state
889 self.augmented_assignment_target = None;
890
891 match stmt {
892 Stmt::Expr(StmtExpr { value, .. })
893 if !self.seen_module_docstring_boundary && value.is_string_literal_expr() => {}
894 Stmt::ImportFrom(import_from) => {
895 // Allow eager `__future__` imports until we see any other import. Lazy imports,
896 // including `lazy from __future__ import ...`, always close the boundary.
897 if import_from.is_lazy || !import_from.is_future_import(ctx.python_version()) {
898 self.seen_futures_boundary = true;
899 }
900 }
901 Stmt::FunctionDef(StmtFunctionDef { is_async, body, .. }) => {
902 if *is_async {
903 let mut visitor = ReturnVisitor::default();
904 visitor.visit_body(body);
905
906 if visitor.has_yield {
907 if let Some(return_range) = visitor.return_range {
908 Self::add_error(
909 ctx,
910 SemanticSyntaxErrorKind::ReturnInGenerator,
911 return_range,
912 );
913 }
914 }
915 }
916 self.seen_futures_boundary = true;
917 }
918 Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
919 self.augmented_assignment_target = Some(target.range());
920 self.seen_futures_boundary = true;
921 }
922 _ => {
923 self.seen_futures_boundary = true;
924 }
925 }
926
927 self.seen_module_docstring_boundary = true;
928 }
929
930 /// Check `expr` for semantic syntax errors and update the checker's internal state.
931 pub fn visit_expr<Ctx: SemanticSyntaxContext>(&mut self, expr: &Expr, ctx: &Ctx) {

Callers

nothing calls this directly

Calls 8

check_stmtMethod · 0.80
defaultFunction · 0.50
walk_stmtFunction · 0.50
is_future_importMethod · 0.45
python_versionMethod · 0.45
visit_bodyMethod · 0.45
rangeMethod · 0.45

Tested by

no test coverage detected