mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Remove async AST node variants for with
, for
, and def
(#6369)
## Summary Per the suggestion in https://github.com/astral-sh/ruff/discussions/6183, this PR removes `AsyncWith`, `AsyncFor`, and `AsyncFunctionDef`, replacing them with an `is_async` field on the non-async variants of those structs. Unlike an interpreter, we _generally_ have identical handling for these nodes, so separating them into distinct variants adds complexity from which we don't really benefit. This can be seen below, where we get to remove a _ton_ of code related to adding generic `Any*` wrappers, and a ton of duplicate branches for these cases. ## Test Plan `cargo test` is unchanged, apart from parser snapshots.
This commit is contained in:
parent
c895252aae
commit
daefa74e9a
91 changed files with 375 additions and 1478 deletions
|
@ -179,16 +179,13 @@ fn is_only<T: PartialEq>(vec: &[T], value: &T) -> bool {
|
||||||
fn is_lone_child(child: &Stmt, parent: &Stmt) -> bool {
|
fn is_lone_child(child: &Stmt, parent: &Stmt) -> bool {
|
||||||
match parent {
|
match parent {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
|
||||||
| Stmt::ClassDef(ast::StmtClassDef { body, .. })
|
| Stmt::ClassDef(ast::StmtClassDef { body, .. })
|
||||||
| Stmt::With(ast::StmtWith { body, .. })
|
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
if is_only(body, child) {
|
if is_only(body, child) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
|
||||||
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
if is_only(body, child) || is_only(orelse, child) {
|
if is_only(body, child) || is_only(orelse, child) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -11,21 +11,18 @@ pub(crate) fn deferred_for_loops(checker: &mut Checker) {
|
||||||
for snapshot in for_loops {
|
for snapshot in for_loops {
|
||||||
checker.semantic.restore(snapshot);
|
checker.semantic.restore(snapshot);
|
||||||
|
|
||||||
if let Stmt::For(ast::StmtFor {
|
let Stmt::For(ast::StmtFor {
|
||||||
target, iter, body, ..
|
target, iter, body, ..
|
||||||
})
|
}) = checker.semantic.current_statement()
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
else {
|
||||||
target, iter, body, ..
|
unreachable!("Expected Stmt::For");
|
||||||
}) = &checker.semantic.current_statement()
|
};
|
||||||
{
|
|
||||||
if checker.enabled(Rule::UnusedLoopControlVariable) {
|
if checker.enabled(Rule::UnusedLoopControlVariable) {
|
||||||
flake8_bugbear::rules::unused_loop_control_variable(checker, target, body);
|
flake8_bugbear::rules::unused_loop_control_variable(checker, target, body);
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::IncorrectDictIterator) {
|
if checker.enabled(Rule::IncorrectDictIterator) {
|
||||||
perflint::rules::incorrect_dict_iterator(checker, target, iter);
|
perflint::rules::incorrect_dict_iterator(checker, target, iter);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unreachable!("Expected Expr::For | Expr::AsyncFor");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,10 +241,7 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
|
||||||
flake8_pyi::rules::unused_private_typed_dict(checker, scope, &mut diagnostics);
|
flake8_pyi::rules::unused_private_typed_dict(checker, scope, &mut diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(
|
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Lambda(_)) {
|
||||||
scope.kind,
|
|
||||||
ScopeKind::Function(_) | ScopeKind::AsyncFunction(_) | ScopeKind::Lambda(_)
|
|
||||||
) {
|
|
||||||
if checker.enabled(Rule::UnusedVariable) {
|
if checker.enabled(Rule::UnusedVariable) {
|
||||||
pyflakes::rules::unused_variable(checker, scope, &mut diagnostics);
|
pyflakes::rules::unused_variable(checker, scope, &mut diagnostics);
|
||||||
}
|
}
|
||||||
|
@ -270,10 +267,7 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(
|
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Module) {
|
||||||
scope.kind,
|
|
||||||
ScopeKind::Function(_) | ScopeKind::AsyncFunction(_) | ScopeKind::Module
|
|
||||||
) {
|
|
||||||
if enforce_typing_imports {
|
if enforce_typing_imports {
|
||||||
let runtime_imports: Vec<&Binding> = checker
|
let runtime_imports: Vec<&Binding> = checker
|
||||||
.semantic
|
.semantic
|
||||||
|
|
|
@ -206,7 +206,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
|
||||||
}
|
}
|
||||||
ExprContext::Store => {
|
ExprContext::Store => {
|
||||||
if checker.enabled(Rule::NonLowercaseVariableInFunction) {
|
if checker.enabled(Rule::NonLowercaseVariableInFunction) {
|
||||||
if checker.semantic.current_scope().kind.is_any_function() {
|
if checker.semantic.current_scope().kind.is_function() {
|
||||||
// Ignore globals.
|
// Ignore globals.
|
||||||
if !checker
|
if !checker
|
||||||
.semantic
|
.semantic
|
||||||
|
|
|
@ -70,22 +70,14 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
|
is_async,
|
||||||
name,
|
name,
|
||||||
decorator_list,
|
decorator_list,
|
||||||
returns,
|
returns,
|
||||||
parameters,
|
parameters,
|
||||||
body,
|
body,
|
||||||
type_params,
|
type_params,
|
||||||
..
|
range: _,
|
||||||
})
|
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
name,
|
|
||||||
decorator_list,
|
|
||||||
returns,
|
|
||||||
parameters,
|
|
||||||
body,
|
|
||||||
type_params,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
if checker.enabled(Rule::DjangoNonLeadingReceiverDecorator) {
|
if checker.enabled(Rule::DjangoNonLeadingReceiverDecorator) {
|
||||||
flake8_django::rules::non_leading_receiver_decorator(checker, decorator_list);
|
flake8_django::rules::non_leading_receiver_decorator(checker, decorator_list);
|
||||||
|
@ -151,11 +143,11 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
flake8_pyi::rules::non_self_return_type(
|
flake8_pyi::rules::non_self_return_type(
|
||||||
checker,
|
checker,
|
||||||
stmt,
|
stmt,
|
||||||
|
*is_async,
|
||||||
name,
|
name,
|
||||||
decorator_list,
|
decorator_list,
|
||||||
returns.as_ref().map(AsRef::as_ref),
|
returns.as_ref().map(AsRef::as_ref),
|
||||||
parameters,
|
parameters,
|
||||||
stmt.is_async_function_def_stmt(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::CustomTypeVarReturnType) {
|
if checker.enabled(Rule::CustomTypeVarReturnType) {
|
||||||
|
@ -181,12 +173,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::BadExitAnnotation) {
|
if checker.enabled(Rule::BadExitAnnotation) {
|
||||||
flake8_pyi::rules::bad_exit_annotation(
|
flake8_pyi::rules::bad_exit_annotation(checker, *is_async, name, parameters);
|
||||||
checker,
|
|
||||||
stmt.is_async_function_def_stmt(),
|
|
||||||
name,
|
|
||||||
parameters,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::RedundantNumericUnion) {
|
if checker.enabled(Rule::RedundantNumericUnion) {
|
||||||
flake8_pyi::rules::redundant_numeric_union(checker, parameters);
|
flake8_pyi::rules::redundant_numeric_union(checker, parameters);
|
||||||
|
@ -1097,8 +1084,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
pygrep_hooks::rules::non_existent_mock_method(checker, test);
|
pygrep_hooks::rules::non_existent_mock_method(checker, test);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::With(ast::StmtWith { items, body, .. })
|
Stmt::With(with_ @ ast::StmtWith { items, body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
|
||||||
if checker.enabled(Rule::AssertRaisesException) {
|
if checker.enabled(Rule::AssertRaisesException) {
|
||||||
flake8_bugbear::rules::assert_raises_exception(checker, items);
|
flake8_bugbear::rules::assert_raises_exception(checker, items);
|
||||||
}
|
}
|
||||||
|
@ -1108,8 +1094,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
if checker.enabled(Rule::MultipleWithStatements) {
|
if checker.enabled(Rule::MultipleWithStatements) {
|
||||||
flake8_simplify::rules::multiple_with_statements(
|
flake8_simplify::rules::multiple_with_statements(
|
||||||
checker,
|
checker,
|
||||||
stmt,
|
with_,
|
||||||
body,
|
|
||||||
checker.semantic.current_statement_parent(),
|
checker.semantic.current_statement_parent(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1134,13 +1119,6 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
iter,
|
iter,
|
||||||
orelse,
|
orelse,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
|
||||||
target,
|
|
||||||
body,
|
|
||||||
iter,
|
|
||||||
orelse,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
if checker.any_enabled(&[Rule::UnusedLoopControlVariable, Rule::IncorrectDictIterator])
|
if checker.any_enabled(&[Rule::UnusedLoopControlVariable, Rule::IncorrectDictIterator])
|
||||||
{
|
{
|
||||||
|
@ -1339,7 +1317,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
if !checker
|
if !checker
|
||||||
.semantic
|
.semantic
|
||||||
.current_scopes()
|
.current_scopes()
|
||||||
.any(|scope| scope.kind.is_any_function())
|
.any(|scope| scope.kind.is_function())
|
||||||
{
|
{
|
||||||
if checker.enabled(Rule::UnprefixedTypeParam) {
|
if checker.enabled(Rule::UnprefixedTypeParam) {
|
||||||
flake8_pyi::rules::prefix_type_params(checker, value, targets);
|
flake8_pyi::rules::prefix_type_params(checker, value, targets);
|
||||||
|
@ -1404,7 +1382,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||||
if !checker
|
if !checker
|
||||||
.semantic
|
.semantic
|
||||||
.current_scopes()
|
.current_scopes()
|
||||||
.any(|scope| scope.kind.is_any_function())
|
.any(|scope| scope.kind.is_function())
|
||||||
{
|
{
|
||||||
flake8_pyi::rules::annotated_assignment_default_in_stub(
|
flake8_pyi::rules::annotated_assignment_default_in_stub(
|
||||||
checker, target, value, annotation,
|
checker, target, value, annotation,
|
||||||
|
|
|
@ -462,14 +462,6 @@ where
|
||||||
returns,
|
returns,
|
||||||
type_params,
|
type_params,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
body,
|
|
||||||
parameters,
|
|
||||||
decorator_list,
|
|
||||||
type_params,
|
|
||||||
returns,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
// Visit the decorators and arguments, but avoid the body, which will be
|
// Visit the decorators and arguments, but avoid the body, which will be
|
||||||
// deferred.
|
// deferred.
|
||||||
|
@ -540,8 +532,7 @@ where
|
||||||
|
|
||||||
self.semantic.push_scope(match &stmt {
|
self.semantic.push_scope(match &stmt {
|
||||||
Stmt::FunctionDef(stmt) => ScopeKind::Function(stmt),
|
Stmt::FunctionDef(stmt) => ScopeKind::Function(stmt),
|
||||||
Stmt::AsyncFunctionDef(stmt) => ScopeKind::AsyncFunction(stmt),
|
_ => unreachable!("Expected Stmt::FunctionDef"),
|
||||||
_ => unreachable!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef"),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.deferred.functions.push(self.semantic.snapshot());
|
self.deferred.functions.push(self.semantic.snapshot());
|
||||||
|
@ -743,8 +734,7 @@ where
|
||||||
|
|
||||||
// Step 3: Clean-up
|
// Step 3: Clean-up
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => {
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => {
|
|
||||||
let scope_id = self.semantic.scope_id;
|
let scope_id = self.semantic.scope_id;
|
||||||
self.deferred.scopes.push(scope_id);
|
self.deferred.scopes.push(scope_id);
|
||||||
self.semantic.pop_scope(); // Function scope
|
self.semantic.pop_scope(); // Function scope
|
||||||
|
@ -1626,7 +1616,7 @@ impl<'a> Checker<'a> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(parent, Stmt::For(_) | Stmt::AsyncFor(_)) {
|
if parent.is_for_stmt() {
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
id,
|
id,
|
||||||
expr.range(),
|
expr.range(),
|
||||||
|
@ -1825,19 +1815,14 @@ impl<'a> Checker<'a> {
|
||||||
for snapshot in deferred_functions {
|
for snapshot in deferred_functions {
|
||||||
self.semantic.restore(snapshot);
|
self.semantic.restore(snapshot);
|
||||||
|
|
||||||
match &self.semantic.current_statement() {
|
if let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
body, parameters, ..
|
||||||
body, parameters, ..
|
}) = self.semantic.current_statement()
|
||||||
})
|
{
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
self.visit_parameters(parameters);
|
||||||
body, parameters, ..
|
self.visit_body(body);
|
||||||
}) => {
|
} else {
|
||||||
self.visit_parameters(parameters);
|
unreachable!("Expected Stmt::FunctionDef")
|
||||||
self.visit_body(body);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
unreachable!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,8 +30,7 @@ pub(crate) fn extract_docstring<'a>(definition: &'a Definition<'a>) -> Option<&'
|
||||||
Definition::Module(module) => docstring_from(module.python_ast),
|
Definition::Module(module) => docstring_from(module.python_ast),
|
||||||
Definition::Member(member) => {
|
Definition::Member(member) => {
|
||||||
if let Stmt::ClassDef(ast::StmtClassDef { body, .. })
|
if let Stmt::ClassDef(ast::StmtClassDef { body, .. })
|
||||||
| Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
| Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) = &member.stmt
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) = &member.stmt
|
|
||||||
{
|
{
|
||||||
docstring_from(body)
|
docstring_from(body)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -15,14 +15,6 @@ pub(super) fn match_function_def(
|
||||||
body,
|
body,
|
||||||
decorator_list,
|
decorator_list,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
name,
|
|
||||||
parameters,
|
|
||||||
returns,
|
|
||||||
body,
|
|
||||||
decorator_list,
|
|
||||||
..
|
|
||||||
}) => (
|
}) => (
|
||||||
name,
|
name,
|
||||||
parameters,
|
parameters,
|
||||||
|
|
|
@ -159,18 +159,12 @@ pub(crate) fn abstract_base_class(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (Stmt::FunctionDef(ast::StmtFunctionDef {
|
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
decorator_list,
|
decorator_list,
|
||||||
body,
|
body,
|
||||||
name: method_name,
|
name: method_name,
|
||||||
..
|
..
|
||||||
})
|
}) = stmt
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
decorator_list,
|
|
||||||
body,
|
|
||||||
name: method_name,
|
|
||||||
..
|
|
||||||
})) = stmt
|
|
||||||
else {
|
else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
|
@ -86,9 +86,6 @@ impl<'a> Visitor<'a> for SuspiciousVariablesVisitor<'a> {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
parameters, body, ..
|
parameters, body, ..
|
||||||
})
|
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
parameters, body, ..
|
|
||||||
}) => {
|
}) => {
|
||||||
// Collect all loaded variable names.
|
// Collect all loaded variable names.
|
||||||
let mut visitor = LoadedNamesVisitor::default();
|
let mut visitor = LoadedNamesVisitor::default();
|
||||||
|
@ -236,7 +233,7 @@ struct AssignedNamesVisitor<'a> {
|
||||||
/// `Visitor` to collect all used identifiers in a statement.
|
/// `Visitor` to collect all used identifiers in a statement.
|
||||||
impl<'a> Visitor<'a> for AssignedNamesVisitor<'a> {
|
impl<'a> Visitor<'a> for AssignedNamesVisitor<'a> {
|
||||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||||
if matches!(stmt, Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_)) {
|
if stmt.is_function_def_stmt() {
|
||||||
// Don't recurse.
|
// Don't recurse.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -251,8 +248,7 @@ impl<'a> Visitor<'a> for AssignedNamesVisitor<'a> {
|
||||||
}
|
}
|
||||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. })
|
Stmt::AugAssign(ast::StmtAugAssign { target, .. })
|
||||||
| Stmt::AnnAssign(ast::StmtAnnAssign { target, .. })
|
| Stmt::AnnAssign(ast::StmtAnnAssign { target, .. })
|
||||||
| Stmt::For(ast::StmtFor { target, .. })
|
| Stmt::For(ast::StmtFor { target, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { target, .. }) => {
|
|
||||||
let mut visitor = NamesFromAssignmentsVisitor::default();
|
let mut visitor = NamesFromAssignmentsVisitor::default();
|
||||||
visitor.visit_expr(target);
|
visitor.visit_expr(target);
|
||||||
self.names.extend(visitor.names);
|
self.names.extend(visitor.names);
|
||||||
|
|
|
@ -69,16 +69,13 @@ fn walk_stmt(checker: &mut Checker, body: &[Stmt], f: fn(&Stmt) -> bool) {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::While(ast::StmtWhile { body, .. })
|
Stmt::While(ast::StmtWhile { body, .. }) | Stmt::For(ast::StmtFor { body, .. }) => {
|
||||||
| Stmt::For(ast::StmtFor { body, .. })
|
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, .. }) => {
|
|
||||||
walk_stmt(checker, body, Stmt::is_return_stmt);
|
walk_stmt(checker, body, Stmt::is_return_stmt);
|
||||||
}
|
}
|
||||||
Stmt::If(ast::StmtIf { body, .. })
|
Stmt::If(ast::StmtIf { body, .. })
|
||||||
| Stmt::Try(ast::StmtTry { body, .. })
|
| Stmt::Try(ast::StmtTry { body, .. })
|
||||||
| Stmt::TryStar(ast::StmtTryStar { body, .. })
|
| Stmt::TryStar(ast::StmtTryStar { body, .. })
|
||||||
| Stmt::With(ast::StmtWith { body, .. })
|
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
walk_stmt(checker, body, f);
|
walk_stmt(checker, body, f);
|
||||||
}
|
}
|
||||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||||
|
|
|
@ -49,14 +49,14 @@ use crate::checkers::ast::Checker;
|
||||||
/// ```
|
/// ```
|
||||||
#[violation]
|
#[violation]
|
||||||
pub struct IterMethodReturnIterable {
|
pub struct IterMethodReturnIterable {
|
||||||
async_: bool,
|
is_async: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Violation for IterMethodReturnIterable {
|
impl Violation for IterMethodReturnIterable {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
let IterMethodReturnIterable { async_ } = self;
|
let IterMethodReturnIterable { is_async } = self;
|
||||||
if *async_ {
|
if *is_async {
|
||||||
format!("`__aiter__` methods should return an `AsyncIterator`, not an `AsyncIterable`")
|
format!("`__aiter__` methods should return an `AsyncIterator`, not an `AsyncIterable`")
|
||||||
} else {
|
} else {
|
||||||
format!("`__iter__` methods should return an `Iterator`, not an `Iterable`")
|
format!("`__iter__` methods should return an `Iterator`, not an `Iterable`")
|
||||||
|
@ -91,7 +91,7 @@ pub(crate) fn iter_method_return_iterable(checker: &mut Checker, definition: &De
|
||||||
returns
|
returns
|
||||||
};
|
};
|
||||||
|
|
||||||
let async_ = match name.as_str() {
|
let is_async = match name.as_str() {
|
||||||
"__iter__" => false,
|
"__iter__" => false,
|
||||||
"__aiter__" => true,
|
"__aiter__" => true,
|
||||||
_ => return,
|
_ => return,
|
||||||
|
@ -101,7 +101,7 @@ pub(crate) fn iter_method_return_iterable(checker: &mut Checker, definition: &De
|
||||||
.semantic()
|
.semantic()
|
||||||
.resolve_call_path(annotation)
|
.resolve_call_path(annotation)
|
||||||
.is_some_and(|call_path| {
|
.is_some_and(|call_path| {
|
||||||
if async_ {
|
if is_async {
|
||||||
matches!(
|
matches!(
|
||||||
call_path.as_slice(),
|
call_path.as_slice(),
|
||||||
["typing", "AsyncIterable"] | ["collections", "abc", "AsyncIterable"]
|
["typing", "AsyncIterable"] | ["collections", "abc", "AsyncIterable"]
|
||||||
|
@ -115,7 +115,7 @@ pub(crate) fn iter_method_return_iterable(checker: &mut Checker, definition: &De
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
IterMethodReturnIterable { async_ },
|
IterMethodReturnIterable { is_async },
|
||||||
returns.range(),
|
returns.range(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,11 +113,11 @@ impl Violation for NonSelfReturnType {
|
||||||
pub(crate) fn non_self_return_type(
|
pub(crate) fn non_self_return_type(
|
||||||
checker: &mut Checker,
|
checker: &mut Checker,
|
||||||
stmt: &Stmt,
|
stmt: &Stmt,
|
||||||
|
is_async: bool,
|
||||||
name: &str,
|
name: &str,
|
||||||
decorator_list: &[Decorator],
|
decorator_list: &[Decorator],
|
||||||
returns: Option<&Expr>,
|
returns: Option<&Expr>,
|
||||||
parameters: &Parameters,
|
parameters: &Parameters,
|
||||||
async_: bool,
|
|
||||||
) {
|
) {
|
||||||
let ScopeKind::Class(class_def) = checker.semantic().current_scope().kind else {
|
let ScopeKind::Class(class_def) = checker.semantic().current_scope().kind else {
|
||||||
return;
|
return;
|
||||||
|
@ -138,7 +138,7 @@ pub(crate) fn non_self_return_type(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if async_ {
|
if is_async {
|
||||||
if name == "__aenter__"
|
if name == "__aenter__"
|
||||||
&& is_name(returns, &class_def.name)
|
&& is_name(returns, &class_def.name)
|
||||||
&& !is_final(&class_def.decorator_list, checker.semantic())
|
&& !is_final(&class_def.decorator_list, checker.semantic())
|
||||||
|
|
|
@ -448,7 +448,7 @@ where
|
||||||
self.has_return_with_value = true;
|
self.has_return_with_value = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => {}
|
Stmt::FunctionDef(_) => {}
|
||||||
_ => visitor::walk_stmt(self, stmt),
|
_ => visitor::walk_stmt(self, stmt),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -195,12 +195,9 @@ pub(crate) fn complex_raises(
|
||||||
if raises_called {
|
if raises_called {
|
||||||
let is_too_complex = if let [stmt] = body {
|
let is_too_complex = if let [stmt] = body {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::With(ast::StmtWith { body, .. })
|
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
is_non_trivial_with_body(body)
|
|
||||||
}
|
|
||||||
// Allow function and class definitions to test decorators
|
// Allow function and class definitions to test decorators
|
||||||
Stmt::ClassDef(_) | Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => false,
|
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
|
||||||
stmt => is_compound_statement(stmt),
|
stmt => is_compound_statement(stmt),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -425,9 +425,7 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
|
||||||
}
|
}
|
||||||
Stmt::Assert(ast::StmtAssert { test, .. }) if is_const_false(test) => {}
|
Stmt::Assert(ast::StmtAssert { test, .. }) if is_const_false(test) => {}
|
||||||
Stmt::While(ast::StmtWhile { test, .. }) if is_const_true(test) => {}
|
Stmt::While(ast::StmtWhile { test, .. }) if is_const_true(test) => {}
|
||||||
Stmt::For(ast::StmtFor { orelse, .. })
|
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
|
||||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
|
||||||
if let Some(last_stmt) = orelse.last() {
|
if let Some(last_stmt) = orelse.last() {
|
||||||
implicit_return(checker, last_stmt);
|
implicit_return(checker, last_stmt);
|
||||||
} else {
|
} else {
|
||||||
|
@ -454,8 +452,7 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::With(ast::StmtWith { body, .. })
|
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
if let Some(last_stmt) = body.last() {
|
if let Some(last_stmt) = body.last() {
|
||||||
implicit_return(checker, last_stmt);
|
implicit_return(checker, last_stmt);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,12 +50,6 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> {
|
||||||
decorator_list,
|
decorator_list,
|
||||||
returns,
|
returns,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
parameters,
|
|
||||||
decorator_list,
|
|
||||||
returns,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
// Visit the decorators, etc.
|
// Visit the decorators, etc.
|
||||||
self.sibling = Some(stmt);
|
self.sibling = Some(stmt);
|
||||||
|
@ -101,8 +95,7 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> {
|
||||||
// x = f.read()
|
// x = f.read()
|
||||||
// return x
|
// return x
|
||||||
// ```
|
// ```
|
||||||
Stmt::With(ast::StmtWith { body, .. })
|
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
if let Some(stmt_assign) = body.last().and_then(Stmt::as_assign_stmt) {
|
if let Some(stmt_assign) = body.last().and_then(Stmt::as_assign_stmt) {
|
||||||
self.stack
|
self.stack
|
||||||
.assignment_return
|
.assignment_return
|
||||||
|
|
|
@ -160,8 +160,7 @@ pub(crate) fn negation_with_equal_op(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid flagging issues in dunder implementations.
|
// Avoid flagging issues in dunder implementations.
|
||||||
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. })
|
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. }) =
|
||||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef { name, .. }) =
|
|
||||||
&checker.semantic().current_scope().kind
|
&checker.semantic().current_scope().kind
|
||||||
{
|
{
|
||||||
if is_dunder_method(name) {
|
if is_dunder_method(name) {
|
||||||
|
@ -218,8 +217,7 @@ pub(crate) fn negation_with_not_equal_op(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid flagging issues in dunder implementations.
|
// Avoid flagging issues in dunder implementations.
|
||||||
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. })
|
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. }) =
|
||||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef { name, .. }) =
|
|
||||||
&checker.semantic().current_scope().kind
|
&checker.semantic().current_scope().kind
|
||||||
{
|
{
|
||||||
if is_dunder_method(name) {
|
if is_dunder_method(name) {
|
||||||
|
|
|
@ -63,18 +63,22 @@ impl Violation for MultipleWithStatements {
|
||||||
/// Returns a boolean indicating whether it's an async with statement, the items
|
/// Returns a boolean indicating whether it's an async with statement, the items
|
||||||
/// and body.
|
/// and body.
|
||||||
fn next_with(body: &[Stmt]) -> Option<(bool, &[WithItem], &[Stmt])> {
|
fn next_with(body: &[Stmt]) -> Option<(bool, &[WithItem], &[Stmt])> {
|
||||||
match body {
|
let [Stmt::With(ast::StmtWith {
|
||||||
[Stmt::With(ast::StmtWith { items, body, .. })] => Some((false, items, body)),
|
is_async,
|
||||||
[Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. })] => Some((true, items, body)),
|
items,
|
||||||
_ => None,
|
body,
|
||||||
}
|
..
|
||||||
|
})] = body
|
||||||
|
else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
Some((*is_async, items, body))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SIM117
|
/// SIM117
|
||||||
pub(crate) fn multiple_with_statements(
|
pub(crate) fn multiple_with_statements(
|
||||||
checker: &mut Checker,
|
checker: &mut Checker,
|
||||||
with_stmt: &Stmt,
|
with_stmt: &ast::StmtWith,
|
||||||
with_body: &[Stmt],
|
|
||||||
with_parent: Option<&Stmt>,
|
with_parent: Option<&Stmt>,
|
||||||
) {
|
) {
|
||||||
// Make sure we fix from top to bottom for nested with statements, e.g. for
|
// Make sure we fix from top to bottom for nested with statements, e.g. for
|
||||||
|
@ -102,8 +106,8 @@ pub(crate) fn multiple_with_statements(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((is_async, items, body)) = next_with(with_body) {
|
if let Some((is_async, items, body)) = next_with(&with_stmt.body) {
|
||||||
if is_async != with_stmt.is_async_with_stmt() {
|
if is_async != with_stmt.is_async {
|
||||||
// One of the statements is an async with, while the other is not,
|
// One of the statements is an async with, while the other is not,
|
||||||
// we can't merge those statements.
|
// we can't merge those statements.
|
||||||
return;
|
return;
|
||||||
|
@ -133,7 +137,7 @@ pub(crate) fn multiple_with_statements(
|
||||||
if !checker
|
if !checker
|
||||||
.indexer()
|
.indexer()
|
||||||
.comment_ranges()
|
.comment_ranges()
|
||||||
.intersects(TextRange::new(with_stmt.start(), with_body[0].start()))
|
.intersects(TextRange::new(with_stmt.start(), with_stmt.body[0].start()))
|
||||||
{
|
{
|
||||||
match fix_with::fix_multiple_with_statements(
|
match fix_with::fix_multiple_with_statements(
|
||||||
checker.locator(),
|
checker.locator(),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use libcst_native::{CompoundStatement, Statement, Suite, With};
|
use libcst_native::{CompoundStatement, Statement, Suite, With};
|
||||||
use ruff_python_ast::Ranged;
|
use ruff_python_ast::{self as ast, Ranged};
|
||||||
|
|
||||||
use crate::autofix::codemods::CodegenStylist;
|
use crate::autofix::codemods::CodegenStylist;
|
||||||
use ruff_diagnostics::Edit;
|
use ruff_diagnostics::Edit;
|
||||||
|
@ -14,15 +14,15 @@ use crate::cst::matchers::{match_function_def, match_indented_block, match_state
|
||||||
pub(crate) fn fix_multiple_with_statements(
|
pub(crate) fn fix_multiple_with_statements(
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
stylist: &Stylist,
|
stylist: &Stylist,
|
||||||
stmt: &ruff_python_ast::Stmt,
|
with_stmt: &ast::StmtWith,
|
||||||
) -> Result<Edit> {
|
) -> Result<Edit> {
|
||||||
// Infer the indentation of the outer block.
|
// Infer the indentation of the outer block.
|
||||||
let Some(outer_indent) = whitespace::indentation(locator, stmt) else {
|
let Some(outer_indent) = whitespace::indentation(locator, with_stmt) else {
|
||||||
bail!("Unable to fix multiline statement");
|
bail!("Unable to fix multiline statement");
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extract the module text.
|
// Extract the module text.
|
||||||
let contents = locator.lines(stmt.range());
|
let contents = locator.lines(with_stmt.range());
|
||||||
|
|
||||||
// If the block is indented, "embed" it in a function definition, to preserve
|
// If the block is indented, "embed" it in a function definition, to preserve
|
||||||
// indentation while retaining valid source code. (We'll strip the prefix later
|
// indentation while retaining valid source code. (We'll strip the prefix later
|
||||||
|
@ -82,7 +82,7 @@ pub(crate) fn fix_multiple_with_statements(
|
||||||
.to_string()
|
.to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let range = locator.lines_range(stmt.range());
|
let range = locator.lines_range(with_stmt.range());
|
||||||
|
|
||||||
Ok(Edit::range_replacement(contents, range))
|
Ok(Edit::range_replacement(contents, range))
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ impl Violation for ReimplementedBuiltin {
|
||||||
|
|
||||||
/// SIM110, SIM111
|
/// SIM110, SIM111
|
||||||
pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
|
pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
|
||||||
if !checker.semantic().current_scope().kind.is_any_function() {
|
if !checker.semantic().current_scope().kind.is_function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -328,13 +328,6 @@ pub(crate) fn unused_arguments(
|
||||||
body,
|
body,
|
||||||
decorator_list,
|
decorator_list,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef {
|
|
||||||
name,
|
|
||||||
parameters,
|
|
||||||
body,
|
|
||||||
decorator_list,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
match function_type::classify(
|
match function_type::classify(
|
||||||
name,
|
name,
|
||||||
|
|
|
@ -89,7 +89,7 @@ impl<'a> BlockBuilder<'a> {
|
||||||
// sibling (i.e., as if the comment is the next statement, as
|
// sibling (i.e., as if the comment is the next statement, as
|
||||||
// opposed to the class or function).
|
// opposed to the class or function).
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => {
|
Stmt::FunctionDef(_) => {
|
||||||
if helpers::has_comment_break(stmt, self.locator) {
|
if helpers::has_comment_break(stmt, self.locator) {
|
||||||
Trailer::Sibling
|
Trailer::Sibling
|
||||||
} else {
|
} else {
|
||||||
|
@ -196,12 +196,6 @@ where
|
||||||
}
|
}
|
||||||
self.finalize(None);
|
self.finalize(None);
|
||||||
}
|
}
|
||||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => {
|
|
||||||
for stmt in body {
|
|
||||||
self.visit_stmt(stmt);
|
|
||||||
}
|
|
||||||
self.finalize(None);
|
|
||||||
}
|
|
||||||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
||||||
for stmt in body {
|
for stmt in body {
|
||||||
self.visit_stmt(stmt);
|
self.visit_stmt(stmt);
|
||||||
|
@ -219,17 +213,6 @@ where
|
||||||
}
|
}
|
||||||
self.finalize(None);
|
self.finalize(None);
|
||||||
}
|
}
|
||||||
Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
|
||||||
for stmt in body {
|
|
||||||
self.visit_stmt(stmt);
|
|
||||||
}
|
|
||||||
self.finalize(None);
|
|
||||||
|
|
||||||
for stmt in orelse {
|
|
||||||
self.visit_stmt(stmt);
|
|
||||||
}
|
|
||||||
self.finalize(None);
|
|
||||||
}
|
|
||||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
for stmt in body {
|
for stmt in body {
|
||||||
self.visit_stmt(stmt);
|
self.visit_stmt(stmt);
|
||||||
|
@ -261,12 +244,6 @@ where
|
||||||
}
|
}
|
||||||
self.finalize(None);
|
self.finalize(None);
|
||||||
}
|
}
|
||||||
Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
for stmt in body {
|
|
||||||
self.visit_stmt(stmt);
|
|
||||||
}
|
|
||||||
self.finalize(None);
|
|
||||||
}
|
|
||||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||||
for match_case in cases {
|
for match_case in cases {
|
||||||
self.visit_match_case(match_case);
|
self.visit_match_case(match_case);
|
||||||
|
|
|
@ -82,14 +82,12 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
|
||||||
complexity += get_complexity_number(&clause.body);
|
complexity += get_complexity_number(&clause.body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
|
||||||
complexity += 1;
|
complexity += 1;
|
||||||
complexity += get_complexity_number(body);
|
complexity += get_complexity_number(body);
|
||||||
complexity += get_complexity_number(orelse);
|
complexity += get_complexity_number(orelse);
|
||||||
}
|
}
|
||||||
Stmt::With(ast::StmtWith { body, .. })
|
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
complexity += get_complexity_number(body);
|
complexity += get_complexity_number(body);
|
||||||
}
|
}
|
||||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
|
@ -131,8 +129,7 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
|
||||||
complexity += get_complexity_number(body);
|
complexity += get_complexity_number(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => {
|
|
||||||
complexity += 1;
|
complexity += 1;
|
||||||
complexity += get_complexity_number(body);
|
complexity += get_complexity_number(body);
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,6 +220,7 @@ fn function(
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let func = Stmt::FunctionDef(ast::StmtFunctionDef {
|
let func = Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
|
is_async: false,
|
||||||
name: Identifier::new(name.to_string(), TextRange::default()),
|
name: Identifier::new(name.to_string(), TextRange::default()),
|
||||||
parameters: Box::new(Parameters {
|
parameters: Box::new(Parameters {
|
||||||
posonlyargs: new_posonlyargs,
|
posonlyargs: new_posonlyargs,
|
||||||
|
@ -236,6 +237,7 @@ fn function(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let func = Stmt::FunctionDef(ast::StmtFunctionDef {
|
let func = Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
|
is_async: false,
|
||||||
name: Identifier::new(name.to_string(), TextRange::default()),
|
name: Identifier::new(name.to_string(), TextRange::default()),
|
||||||
parameters: Box::new(parameters.clone()),
|
parameters: Box::new(parameters.clone()),
|
||||||
body: vec![body],
|
body: vec![body],
|
||||||
|
|
|
@ -1726,9 +1726,7 @@ fn missing_args(checker: &mut Checker, docstring: &Docstring, docstrings_args: &
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let (Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. })
|
let Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. }) = stmt else {
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { parameters, .. })) = stmt
|
|
||||||
else {
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -36,14 +36,12 @@ pub(crate) fn break_outside_loop<'a>(
|
||||||
let mut child = stmt;
|
let mut child = stmt;
|
||||||
for parent in parents {
|
for parent in parents {
|
||||||
match parent {
|
match parent {
|
||||||
Stmt::For(ast::StmtFor { orelse, .. })
|
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
|
||||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
|
||||||
if !orelse.contains(child) {
|
if !orelse.contains(child) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -36,14 +36,12 @@ pub(crate) fn continue_outside_loop<'a>(
|
||||||
let mut child = stmt;
|
let mut child = stmt;
|
||||||
for parent in parents {
|
for parent in parents {
|
||||||
match parent {
|
match parent {
|
||||||
Stmt::For(ast::StmtFor { orelse, .. })
|
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
|
||||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
|
||||||
if !orelse.contains(child) {
|
if !orelse.contains(child) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -51,7 +51,7 @@ pub(crate) fn undefined_local(
|
||||||
scope: &Scope,
|
scope: &Scope,
|
||||||
diagnostics: &mut Vec<Diagnostic>,
|
diagnostics: &mut Vec<Diagnostic>,
|
||||||
) {
|
) {
|
||||||
if scope.kind.is_any_function() {
|
if scope.kind.is_function() {
|
||||||
for (name, binding_id) in scope.bindings() {
|
for (name, binding_id) in scope.bindings() {
|
||||||
// If the variable shadows a binding in a parent scope...
|
// If the variable shadows a binding in a parent scope...
|
||||||
if let Some(shadowed_id) = checker.semantic().shadowed_binding(binding_id) {
|
if let Some(shadowed_id) = checker.semantic().shadowed_binding(binding_id) {
|
||||||
|
|
|
@ -299,7 +299,7 @@ fn remove_unused_variable(
|
||||||
|
|
||||||
/// F841
|
/// F841
|
||||||
pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mut Vec<Diagnostic>) {
|
pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mut Vec<Diagnostic>) {
|
||||||
if scope.uses_locals() && scope.kind.is_any_function() {
|
if scope.uses_locals() && scope.kind.is_function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,16 +24,11 @@ pub(super) fn type_param_name(arguments: &Arguments) -> Option<&str> {
|
||||||
|
|
||||||
pub(super) fn in_dunder_init(semantic: &SemanticModel, settings: &Settings) -> bool {
|
pub(super) fn in_dunder_init(semantic: &SemanticModel, settings: &Settings) -> bool {
|
||||||
let scope = semantic.current_scope();
|
let scope = semantic.current_scope();
|
||||||
let (ScopeKind::Function(ast::StmtFunctionDef {
|
let ScopeKind::Function(ast::StmtFunctionDef {
|
||||||
name,
|
name,
|
||||||
decorator_list,
|
decorator_list,
|
||||||
..
|
..
|
||||||
})
|
}) = scope.kind
|
||||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef {
|
|
||||||
name,
|
|
||||||
decorator_list,
|
|
||||||
..
|
|
||||||
})) = scope.kind
|
|
||||||
else {
|
else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
|
@ -69,11 +69,10 @@ fn traverse_body(checker: &mut Checker, body: &[Stmt]) {
|
||||||
traverse_body(checker, body);
|
traverse_body(checker, body);
|
||||||
traverse_body(checker, orelse);
|
traverse_body(checker, orelse);
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor { orelse, .. })
|
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
traverse_body(checker, orelse);
|
||||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => traverse_body(checker, orelse),
|
}
|
||||||
Stmt::With(ast::StmtWith { body, .. })
|
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
traverse_body(checker, body);
|
traverse_body(checker, body);
|
||||||
}
|
}
|
||||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||||
|
|
|
@ -147,9 +147,7 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
||||||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||||
// Collect target expressions.
|
// Collect target expressions.
|
||||||
match stmt {
|
match stmt {
|
||||||
// For and async for.
|
Stmt::For(ast::StmtFor { target, .. }) => {
|
||||||
Stmt::For(ast::StmtFor { target, .. })
|
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { target, .. }) => {
|
|
||||||
self.assignment_targets.extend(
|
self.assignment_targets.extend(
|
||||||
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
|
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
|
||||||
ExprWithInnerBindingKind {
|
ExprWithInnerBindingKind {
|
||||||
|
@ -159,7 +157,6 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// With.
|
|
||||||
Stmt::With(ast::StmtWith { items, .. }) => {
|
Stmt::With(ast::StmtWith { items, .. }) => {
|
||||||
self.assignment_targets.extend(
|
self.assignment_targets.extend(
|
||||||
assignment_targets_from_with_items(items, self.dummy_variable_rgx).map(
|
assignment_targets_from_with_items(items, self.dummy_variable_rgx).map(
|
||||||
|
@ -170,7 +167,6 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Assignment, augmented assignment, and annotated assignment.
|
|
||||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||||
// Check for single-target assignments which are of the
|
// Check for single-target assignments which are of the
|
||||||
// form `x = cast(..., x)`.
|
// form `x = cast(..., x)`.
|
||||||
|
@ -217,8 +213,7 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
||||||
// Decide whether to recurse.
|
// Decide whether to recurse.
|
||||||
match stmt {
|
match stmt {
|
||||||
// Don't recurse into blocks that create a new scope.
|
// Don't recurse into blocks that create a new scope.
|
||||||
Stmt::ClassDef(_) => {}
|
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => {}
|
||||||
Stmt::FunctionDef(_) => {}
|
|
||||||
// Otherwise, do recurse.
|
// Otherwise, do recurse.
|
||||||
_ => {
|
_ => {
|
||||||
walk_stmt(self, stmt);
|
walk_stmt(self, stmt);
|
||||||
|
@ -339,8 +334,7 @@ fn assignment_targets_from_assign_targets<'a>(
|
||||||
/// PLW2901
|
/// PLW2901
|
||||||
pub(crate) fn redefined_loop_name(checker: &mut Checker, stmt: &Stmt) {
|
pub(crate) fn redefined_loop_name(checker: &mut Checker, stmt: &Stmt) {
|
||||||
let (outer_assignment_targets, inner_assignment_targets) = match stmt {
|
let (outer_assignment_targets, inner_assignment_targets) = match stmt {
|
||||||
Stmt::With(ast::StmtWith { items, body, .. })
|
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
|
||||||
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
||||||
assignment_targets_from_with_items(items, &checker.settings.dummy_variable_rgx)
|
assignment_targets_from_with_items(items, &checker.settings.dummy_variable_rgx)
|
||||||
.map(|expr| ExprWithOuterBindingKind {
|
.map(|expr| ExprWithOuterBindingKind {
|
||||||
|
@ -358,8 +352,7 @@ pub(crate) fn redefined_loop_name(checker: &mut Checker, stmt: &Stmt) {
|
||||||
}
|
}
|
||||||
(outer_assignment_targets, visitor.assignment_targets)
|
(outer_assignment_targets, visitor.assignment_targets)
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor { target, body, .. })
|
Stmt::For(ast::StmtFor { target, body, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { target, body, .. }) => {
|
|
||||||
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
||||||
assignment_targets_from_expr(target, &checker.settings.dummy_variable_rgx)
|
assignment_targets_from_expr(target, &checker.settings.dummy_variable_rgx)
|
||||||
.map(|expr| ExprWithOuterBindingKind {
|
.map(|expr| ExprWithOuterBindingKind {
|
||||||
|
@ -377,9 +370,7 @@ pub(crate) fn redefined_loop_name(checker: &mut Checker, stmt: &Stmt) {
|
||||||
}
|
}
|
||||||
(outer_assignment_targets, visitor.assignment_targets)
|
(outer_assignment_targets, visitor.assignment_targets)
|
||||||
}
|
}
|
||||||
_ => panic!(
|
_ => panic!("redefined_loop_name called on Statement that is not a `With` or `For`"),
|
||||||
"redefined_loop_name called on Statement that is not a With, For, AsyncWith, or AsyncFor"
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut diagnostics = Vec::new();
|
let mut diagnostics = Vec::new();
|
||||||
|
|
|
@ -108,7 +108,6 @@ fn num_branches(stmts: &[Stmt]) -> usize {
|
||||||
.sum::<usize>()
|
.sum::<usize>()
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
|
||||||
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
1 + num_branches(body)
|
1 + num_branches(body)
|
||||||
+ (if orelse.is_empty() {
|
+ (if orelse.is_empty() {
|
||||||
|
|
|
@ -78,8 +78,7 @@ fn num_statements(stmts: &[Stmt]) -> usize {
|
||||||
count += num_statements(&clause.body);
|
count += num_statements(&clause.body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
|
||||||
count += num_statements(body);
|
count += num_statements(body);
|
||||||
count += num_statements(orelse);
|
count += num_statements(orelse);
|
||||||
}
|
}
|
||||||
|
@ -129,7 +128,6 @@ fn num_statements(stmts: &[Stmt]) -> usize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
|
||||||
| Stmt::With(ast::StmtWith { body, .. }) => {
|
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
count += 1;
|
count += 1;
|
||||||
count += num_statements(body);
|
count += num_statements(body);
|
||||||
|
|
|
@ -63,8 +63,7 @@ fn loop_exits_early(body: &[Stmt]) -> bool {
|
||||||
.iter()
|
.iter()
|
||||||
.any(|clause| loop_exits_early(&clause.body))
|
.any(|clause| loop_exits_early(&clause.body))
|
||||||
}
|
}
|
||||||
Stmt::With(ast::StmtWith { body, .. })
|
Stmt::With(ast::StmtWith { body, .. }) => loop_exits_early(body),
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => loop_exits_early(body),
|
|
||||||
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
|
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
|
||||||
.iter()
|
.iter()
|
||||||
.any(|MatchCase { body, .. }| loop_exits_early(body)),
|
.any(|MatchCase { body, .. }| loop_exits_early(body)),
|
||||||
|
@ -91,9 +90,9 @@ fn loop_exits_early(body: &[Stmt]) -> bool {
|
||||||
}) => loop_exits_early(body),
|
}) => loop_exits_early(body),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor { orelse, .. })
|
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
loop_exits_early(orelse)
|
||||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => loop_exits_early(orelse),
|
}
|
||||||
Stmt::Break(_) => true,
|
Stmt::Break(_) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use ruff_python_ast::{ExprYieldFrom, Ranged};
|
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::{self as ast, Ranged};
|
||||||
|
use ruff_python_semantic::ScopeKind;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
|
||||||
|
@ -37,9 +37,11 @@ impl Violation for YieldFromInAsyncFunction {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// PLE1700
|
/// PLE1700
|
||||||
pub(crate) fn yield_from_in_async_function(checker: &mut Checker, expr: &ExprYieldFrom) {
|
pub(crate) fn yield_from_in_async_function(checker: &mut Checker, expr: &ast::ExprYieldFrom) {
|
||||||
let scope = checker.semantic().current_scope();
|
if matches!(
|
||||||
if scope.kind.is_async_function() {
|
checker.semantic().current_scope().kind,
|
||||||
|
ScopeKind::Function(ast::StmtFunctionDef { is_async: true, .. })
|
||||||
|
) {
|
||||||
checker
|
checker
|
||||||
.diagnostics
|
.diagnostics
|
||||||
.push(Diagnostic::new(YieldFromInAsyncFunction, expr.range()));
|
.push(Diagnostic::new(YieldFromInAsyncFunction, expr.range()));
|
||||||
|
|
|
@ -83,7 +83,7 @@ pub(crate) fn super_call_with_parameters(
|
||||||
let scope = checker.semantic().current_scope();
|
let scope = checker.semantic().current_scope();
|
||||||
|
|
||||||
// Check: are we in a Function scope?
|
// Check: are we in a Function scope?
|
||||||
if !scope.kind.is_any_function() {
|
if !scope.kind.is_function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use ruff_python_ast::{self as ast, Expr, ExprContext, Ranged, Stmt};
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::statement_visitor::StatementVisitor;
|
use ruff_python_ast::statement_visitor::StatementVisitor;
|
||||||
use ruff_python_ast::visitor::Visitor;
|
use ruff_python_ast::visitor::Visitor;
|
||||||
|
use ruff_python_ast::{self as ast, Expr, ExprContext, Ranged, Stmt};
|
||||||
use ruff_python_ast::{statement_visitor, visitor};
|
use ruff_python_ast::{statement_visitor, visitor};
|
||||||
use ruff_python_semantic::StatementKey;
|
use ruff_python_semantic::StatementKey;
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ impl<'a> StatementVisitor<'a> for YieldFromVisitor<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||||
// Don't recurse into anything that defines a new scope.
|
// Don't recurse into anything that defines a new scope.
|
||||||
}
|
}
|
||||||
_ => statement_visitor::walk_stmt(self, stmt),
|
_ => statement_visitor::walk_stmt(self, stmt),
|
||||||
|
@ -162,39 +162,46 @@ impl<'a> Visitor<'a> for ReferenceVisitor<'a> {
|
||||||
/// UP028
|
/// UP028
|
||||||
pub(crate) fn yield_in_for_loop(checker: &mut Checker, stmt: &Stmt) {
|
pub(crate) fn yield_in_for_loop(checker: &mut Checker, stmt: &Stmt) {
|
||||||
// Intentionally omit async functions.
|
// Intentionally omit async functions.
|
||||||
if let Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) = stmt {
|
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
let yields = {
|
is_async: false,
|
||||||
let mut visitor = YieldFromVisitor::default();
|
body,
|
||||||
visitor.visit_body(body);
|
..
|
||||||
visitor.yields
|
}) = stmt
|
||||||
};
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let references = {
|
let yields = {
|
||||||
let mut visitor = ReferenceVisitor::default();
|
let mut visitor = YieldFromVisitor::default();
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
visitor.references
|
visitor.yields
|
||||||
};
|
};
|
||||||
|
|
||||||
for item in yields {
|
let references = {
|
||||||
// If any of the bound names are used outside of the loop, don't rewrite.
|
let mut visitor = ReferenceVisitor::default();
|
||||||
if references.iter().any(|(statement, names)| {
|
visitor.visit_body(body);
|
||||||
*statement != StatementKey::from(item.stmt)
|
visitor.references
|
||||||
&& *statement != StatementKey::from(item.body)
|
};
|
||||||
&& item.names.iter().any(|name| names.contains(name))
|
|
||||||
}) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(YieldInForLoop, item.stmt.range());
|
for item in yields {
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
// If any of the bound names are used outside of the loop, don't rewrite.
|
||||||
let contents = checker.locator().slice(item.iter.range());
|
if references.iter().any(|(statement, names)| {
|
||||||
let contents = format!("yield from {contents}");
|
*statement != StatementKey::from(item.stmt)
|
||||||
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
&& *statement != StatementKey::from(item.body)
|
||||||
contents,
|
&& item.names.iter().any(|name| names.contains(name))
|
||||||
item.stmt.range(),
|
}) {
|
||||||
)));
|
continue;
|
||||||
}
|
|
||||||
checker.diagnostics.push(diagnostic);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut diagnostic = Diagnostic::new(YieldInForLoop, item.stmt.range());
|
||||||
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
|
let contents = checker.locator().slice(item.iter.range());
|
||||||
|
let contents = format!("yield from {contents}");
|
||||||
|
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
||||||
|
contents,
|
||||||
|
item.stmt.range(),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ use std::{fmt, iter, usize};
|
||||||
|
|
||||||
use log::error;
|
use log::error;
|
||||||
use ruff_python_ast::{
|
use ruff_python_ast::{
|
||||||
Expr, Identifier, MatchCase, Pattern, PatternMatchAs, Ranged, Stmt, StmtAsyncFor,
|
Expr, Identifier, MatchCase, Pattern, PatternMatchAs, Ranged, Stmt, StmtFor, StmtMatch,
|
||||||
StmtAsyncWith, StmtFor, StmtMatch, StmtReturn, StmtTry, StmtTryStar, StmtWhile, StmtWith,
|
StmtReturn, StmtTry, StmtTryStar, StmtWhile, StmtWith,
|
||||||
};
|
};
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
|
|
||||||
|
@ -467,7 +467,6 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
|
||||||
let next = match stmt {
|
let next = match stmt {
|
||||||
// Statements that continue to the next statement after execution.
|
// Statements that continue to the next statement after execution.
|
||||||
Stmt::FunctionDef(_)
|
Stmt::FunctionDef(_)
|
||||||
| Stmt::AsyncFunctionDef(_)
|
|
||||||
| Stmt::Import(_)
|
| Stmt::Import(_)
|
||||||
| Stmt::ImportFrom(_)
|
| Stmt::ImportFrom(_)
|
||||||
| Stmt::ClassDef(_)
|
| Stmt::ClassDef(_)
|
||||||
|
@ -535,12 +534,6 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
|
||||||
body,
|
body,
|
||||||
orelse,
|
orelse,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFor(StmtAsyncFor {
|
|
||||||
iter: condition,
|
|
||||||
body,
|
|
||||||
orelse,
|
|
||||||
..
|
|
||||||
}) => loop_block(self, Condition::Iterator(condition), body, orelse, after),
|
}) => loop_block(self, Condition::Iterator(condition), body, orelse, after),
|
||||||
Stmt::Try(StmtTry {
|
Stmt::Try(StmtTry {
|
||||||
body,
|
body,
|
||||||
|
@ -566,8 +559,7 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
|
||||||
let _ = (body, handlers, orelse, finalbody); // Silence unused code warnings.
|
let _ = (body, handlers, orelse, finalbody); // Silence unused code warnings.
|
||||||
self.unconditional_next_block(after)
|
self.unconditional_next_block(after)
|
||||||
}
|
}
|
||||||
Stmt::With(StmtWith { items, body, .. })
|
Stmt::With(StmtWith { items, body, .. }) => {
|
||||||
| Stmt::AsyncWith(StmtAsyncWith { items, body, .. }) => {
|
|
||||||
// TODO: handle `with` statements, see
|
// TODO: handle `with` statements, see
|
||||||
// <https://docs.python.org/3/reference/compound_stmts.html#the-with-statement>.
|
// <https://docs.python.org/3/reference/compound_stmts.html#the-with-statement>.
|
||||||
// I recommend to `try` statements first as `with` can desugar
|
// I recommend to `try` statements first as `with` can desugar
|
||||||
|
@ -889,7 +881,6 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
|
||||||
Stmt::Return(_) | Stmt::Raise(_) => false,
|
Stmt::Return(_) | Stmt::Raise(_) => false,
|
||||||
Stmt::If(stmt) => needs_next_block(&stmt.body) || stmt.elif_else_clauses.last().map_or(true, |clause| needs_next_block(&clause.body)),
|
Stmt::If(stmt) => needs_next_block(&stmt.body) || stmt.elif_else_clauses.last().map_or(true, |clause| needs_next_block(&clause.body)),
|
||||||
Stmt::FunctionDef(_)
|
Stmt::FunctionDef(_)
|
||||||
| Stmt::AsyncFunctionDef(_)
|
|
||||||
| Stmt::Import(_)
|
| Stmt::Import(_)
|
||||||
| Stmt::ImportFrom(_)
|
| Stmt::ImportFrom(_)
|
||||||
| Stmt::ClassDef(_)
|
| Stmt::ClassDef(_)
|
||||||
|
@ -905,10 +896,8 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
|
||||||
| Stmt::Break(_)
|
| Stmt::Break(_)
|
||||||
| Stmt::Continue(_)
|
| Stmt::Continue(_)
|
||||||
| Stmt::For(_)
|
| Stmt::For(_)
|
||||||
| Stmt::AsyncFor(_)
|
|
||||||
| Stmt::While(_)
|
| Stmt::While(_)
|
||||||
| Stmt::With(_)
|
| Stmt::With(_)
|
||||||
| Stmt::AsyncWith(_)
|
|
||||||
| Stmt::Match(_)
|
| Stmt::Match(_)
|
||||||
| Stmt::Try(_)
|
| Stmt::Try(_)
|
||||||
| Stmt::TryStar(_)
|
| Stmt::TryStar(_)
|
||||||
|
@ -923,7 +912,6 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
|
||||||
fn is_control_flow_stmt(stmt: &Stmt) -> bool {
|
fn is_control_flow_stmt(stmt: &Stmt) -> bool {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(_)
|
Stmt::FunctionDef(_)
|
||||||
| Stmt::AsyncFunctionDef(_)
|
|
||||||
| Stmt::Import(_)
|
| Stmt::Import(_)
|
||||||
| Stmt::ImportFrom(_)
|
| Stmt::ImportFrom(_)
|
||||||
| Stmt::ClassDef(_)
|
| Stmt::ClassDef(_)
|
||||||
|
@ -937,11 +925,9 @@ fn is_control_flow_stmt(stmt: &Stmt) -> bool {
|
||||||
| Stmt::Pass(_) => false,
|
| Stmt::Pass(_) => false,
|
||||||
Stmt::Return(_)
|
Stmt::Return(_)
|
||||||
| Stmt::For(_)
|
| Stmt::For(_)
|
||||||
| Stmt::AsyncFor(_)
|
|
||||||
| Stmt::While(_)
|
| Stmt::While(_)
|
||||||
| Stmt::If(_)
|
| Stmt::If(_)
|
||||||
| Stmt::With(_)
|
| Stmt::With(_)
|
||||||
| Stmt::AsyncWith(_)
|
|
||||||
| Stmt::Match(_)
|
| Stmt::Match(_)
|
||||||
| Stmt::Raise(_)
|
| Stmt::Raise(_)
|
||||||
| Stmt::Try(_)
|
| Stmt::Try(_)
|
||||||
|
|
|
@ -55,7 +55,7 @@ where
|
||||||
{
|
{
|
||||||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||||
// Don't recurse.
|
// Don't recurse.
|
||||||
}
|
}
|
||||||
Stmt::Return(_) => self.returns.push(stmt),
|
Stmt::Return(_) => self.returns.push(stmt),
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
use crate::{nodes, Decorator, Stmt};
|
use crate::{nodes, Decorator, Stmt};
|
||||||
|
|
||||||
pub fn name(stmt: &Stmt) -> &str {
|
pub fn name(stmt: &Stmt) -> &str {
|
||||||
match stmt {
|
let Stmt::FunctionDef(nodes::StmtFunctionDef { name, .. }) = stmt else {
|
||||||
Stmt::FunctionDef(nodes::StmtFunctionDef { name, .. })
|
panic!("Expected Stmt::FunctionDef")
|
||||||
| Stmt::AsyncFunctionDef(nodes::StmtAsyncFunctionDef { name, .. }) => name.as_str(),
|
};
|
||||||
_ => panic!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef"),
|
name.as_str()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decorator_list(stmt: &Stmt) -> &[Decorator] {
|
pub fn decorator_list(stmt: &Stmt) -> &[Decorator] {
|
||||||
match stmt {
|
let Stmt::FunctionDef(nodes::StmtFunctionDef { decorator_list, .. }) = stmt else {
|
||||||
Stmt::FunctionDef(nodes::StmtFunctionDef { decorator_list, .. })
|
panic!("Expected Stmt::FunctionDef")
|
||||||
| Stmt::AsyncFunctionDef(nodes::StmtAsyncFunctionDef { decorator_list, .. }) => {
|
};
|
||||||
decorator_list
|
decorator_list
|
||||||
}
|
|
||||||
_ => panic!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -950,16 +950,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct StmtFunctionDef<'a> {
|
pub struct StmtFunctionDef<'a> {
|
||||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
is_async: bool,
|
||||||
name: &'a str,
|
|
||||||
type_params: Option<ComparableTypeParams<'a>>,
|
|
||||||
parameters: ComparableParameters<'a>,
|
|
||||||
returns: Option<ComparableExpr<'a>>,
|
|
||||||
body: Vec<ComparableStmt<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub struct StmtAsyncFunctionDef<'a> {
|
|
||||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
type_params: Option<ComparableTypeParams<'a>>,
|
type_params: Option<ComparableTypeParams<'a>>,
|
||||||
|
@ -1084,14 +1075,7 @@ pub struct StmtAnnAssign<'a> {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct StmtFor<'a> {
|
pub struct StmtFor<'a> {
|
||||||
target: ComparableExpr<'a>,
|
is_async: bool,
|
||||||
iter: ComparableExpr<'a>,
|
|
||||||
body: Vec<ComparableStmt<'a>>,
|
|
||||||
orelse: Vec<ComparableStmt<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub struct StmtAsyncFor<'a> {
|
|
||||||
target: ComparableExpr<'a>,
|
target: ComparableExpr<'a>,
|
||||||
iter: ComparableExpr<'a>,
|
iter: ComparableExpr<'a>,
|
||||||
body: Vec<ComparableStmt<'a>>,
|
body: Vec<ComparableStmt<'a>>,
|
||||||
|
@ -1114,12 +1098,7 @@ pub struct StmtIf<'a> {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct StmtWith<'a> {
|
pub struct StmtWith<'a> {
|
||||||
items: Vec<ComparableWithItem<'a>>,
|
is_async: bool,
|
||||||
body: Vec<ComparableStmt<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub struct StmtAsyncWith<'a> {
|
|
||||||
items: Vec<ComparableWithItem<'a>>,
|
items: Vec<ComparableWithItem<'a>>,
|
||||||
body: Vec<ComparableStmt<'a>>,
|
body: Vec<ComparableStmt<'a>>,
|
||||||
}
|
}
|
||||||
|
@ -1194,7 +1173,6 @@ pub struct StmtLineMagic<'a> {
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum ComparableStmt<'a> {
|
pub enum ComparableStmt<'a> {
|
||||||
FunctionDef(StmtFunctionDef<'a>),
|
FunctionDef(StmtFunctionDef<'a>),
|
||||||
AsyncFunctionDef(StmtAsyncFunctionDef<'a>),
|
|
||||||
ClassDef(StmtClassDef<'a>),
|
ClassDef(StmtClassDef<'a>),
|
||||||
Return(StmtReturn<'a>),
|
Return(StmtReturn<'a>),
|
||||||
Delete(StmtDelete<'a>),
|
Delete(StmtDelete<'a>),
|
||||||
|
@ -1202,11 +1180,9 @@ pub enum ComparableStmt<'a> {
|
||||||
AugAssign(StmtAugAssign<'a>),
|
AugAssign(StmtAugAssign<'a>),
|
||||||
AnnAssign(StmtAnnAssign<'a>),
|
AnnAssign(StmtAnnAssign<'a>),
|
||||||
For(StmtFor<'a>),
|
For(StmtFor<'a>),
|
||||||
AsyncFor(StmtAsyncFor<'a>),
|
|
||||||
While(StmtWhile<'a>),
|
While(StmtWhile<'a>),
|
||||||
If(StmtIf<'a>),
|
If(StmtIf<'a>),
|
||||||
With(StmtWith<'a>),
|
With(StmtWith<'a>),
|
||||||
AsyncWith(StmtAsyncWith<'a>),
|
|
||||||
Match(StmtMatch<'a>),
|
Match(StmtMatch<'a>),
|
||||||
Raise(StmtRaise<'a>),
|
Raise(StmtRaise<'a>),
|
||||||
Try(StmtTry<'a>),
|
Try(StmtTry<'a>),
|
||||||
|
@ -1228,6 +1204,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
||||||
fn from(stmt: &'a ast::Stmt) -> Self {
|
fn from(stmt: &'a ast::Stmt) -> Self {
|
||||||
match stmt {
|
match stmt {
|
||||||
ast::Stmt::FunctionDef(ast::StmtFunctionDef {
|
ast::Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
|
is_async,
|
||||||
name,
|
name,
|
||||||
parameters,
|
parameters,
|
||||||
body,
|
body,
|
||||||
|
@ -1236,22 +1213,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
||||||
type_params,
|
type_params,
|
||||||
range: _,
|
range: _,
|
||||||
}) => Self::FunctionDef(StmtFunctionDef {
|
}) => Self::FunctionDef(StmtFunctionDef {
|
||||||
name: name.as_str(),
|
is_async: *is_async,
|
||||||
parameters: parameters.into(),
|
|
||||||
body: body.iter().map(Into::into).collect(),
|
|
||||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
|
||||||
returns: returns.as_ref().map(Into::into),
|
|
||||||
type_params: type_params.as_ref().map(Into::into),
|
|
||||||
}),
|
|
||||||
ast::Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
name,
|
|
||||||
parameters,
|
|
||||||
body,
|
|
||||||
decorator_list,
|
|
||||||
returns,
|
|
||||||
type_params,
|
|
||||||
range: _,
|
|
||||||
}) => Self::AsyncFunctionDef(StmtAsyncFunctionDef {
|
|
||||||
name: name.as_str(),
|
name: name.as_str(),
|
||||||
parameters: parameters.into(),
|
parameters: parameters.into(),
|
||||||
body: body.iter().map(Into::into).collect(),
|
body: body.iter().map(Into::into).collect(),
|
||||||
|
@ -1320,24 +1282,14 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
||||||
simple: *simple,
|
simple: *simple,
|
||||||
}),
|
}),
|
||||||
ast::Stmt::For(ast::StmtFor {
|
ast::Stmt::For(ast::StmtFor {
|
||||||
|
is_async,
|
||||||
target,
|
target,
|
||||||
iter,
|
iter,
|
||||||
body,
|
body,
|
||||||
orelse,
|
orelse,
|
||||||
range: _,
|
range: _,
|
||||||
}) => Self::For(StmtFor {
|
}) => Self::For(StmtFor {
|
||||||
target: target.into(),
|
is_async: *is_async,
|
||||||
iter: iter.into(),
|
|
||||||
body: body.iter().map(Into::into).collect(),
|
|
||||||
orelse: orelse.iter().map(Into::into).collect(),
|
|
||||||
}),
|
|
||||||
ast::Stmt::AsyncFor(ast::StmtAsyncFor {
|
|
||||||
target,
|
|
||||||
iter,
|
|
||||||
body,
|
|
||||||
orelse,
|
|
||||||
range: _,
|
|
||||||
}) => Self::AsyncFor(StmtAsyncFor {
|
|
||||||
target: target.into(),
|
target: target.into(),
|
||||||
iter: iter.into(),
|
iter: iter.into(),
|
||||||
body: body.iter().map(Into::into).collect(),
|
body: body.iter().map(Into::into).collect(),
|
||||||
|
@ -1364,18 +1316,12 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
||||||
elif_else_clauses: elif_else_clauses.iter().map(Into::into).collect(),
|
elif_else_clauses: elif_else_clauses.iter().map(Into::into).collect(),
|
||||||
}),
|
}),
|
||||||
ast::Stmt::With(ast::StmtWith {
|
ast::Stmt::With(ast::StmtWith {
|
||||||
|
is_async,
|
||||||
items,
|
items,
|
||||||
body,
|
body,
|
||||||
range: _,
|
range: _,
|
||||||
}) => Self::With(StmtWith {
|
}) => Self::With(StmtWith {
|
||||||
items: items.iter().map(Into::into).collect(),
|
is_async: *is_async,
|
||||||
body: body.iter().map(Into::into).collect(),
|
|
||||||
}),
|
|
||||||
ast::Stmt::AsyncWith(ast::StmtAsyncWith {
|
|
||||||
items,
|
|
||||||
body,
|
|
||||||
range: _,
|
|
||||||
}) => Self::AsyncWith(StmtAsyncWith {
|
|
||||||
items: items.iter().map(Into::into).collect(),
|
items: items.iter().map(Into::into).collect(),
|
||||||
body: body.iter().map(Into::into).collect(),
|
body: body.iter().map(Into::into).collect(),
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -1,134 +0,0 @@
|
||||||
use crate::node::AnyNodeRef;
|
|
||||||
use crate::{
|
|
||||||
Decorator, Expr, Identifier, Parameters, Ranged, StmtAsyncFunctionDef, StmtFunctionDef, Suite,
|
|
||||||
TypeParams,
|
|
||||||
};
|
|
||||||
use ruff_text_size::TextRange;
|
|
||||||
|
|
||||||
/// Enum that represents any python function definition.
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
||||||
pub enum AnyFunctionDefinition<'a> {
|
|
||||||
FunctionDefinition(&'a StmtFunctionDef),
|
|
||||||
AsyncFunctionDefinition(&'a StmtAsyncFunctionDef),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> AnyFunctionDefinition<'a> {
|
|
||||||
pub const fn cast_ref(reference: AnyNodeRef<'a>) -> Option<Self> {
|
|
||||||
match reference {
|
|
||||||
AnyNodeRef::StmtAsyncFunctionDef(definition) => {
|
|
||||||
Some(Self::AsyncFunctionDefinition(definition))
|
|
||||||
}
|
|
||||||
AnyNodeRef::StmtFunctionDef(definition) => Some(Self::FunctionDefinition(definition)),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `Some` if this is a [`StmtFunctionDef`] and `None` otherwise.
|
|
||||||
pub const fn as_function_definition(self) -> Option<&'a StmtFunctionDef> {
|
|
||||||
if let Self::FunctionDefinition(definition) = self {
|
|
||||||
Some(definition)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `Some` if this is a [`StmtAsyncFunctionDef`] and `None` otherwise.
|
|
||||||
pub const fn as_async_function_definition(self) -> Option<&'a StmtAsyncFunctionDef> {
|
|
||||||
if let Self::AsyncFunctionDefinition(definition) = self {
|
|
||||||
Some(definition)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the function's name
|
|
||||||
pub const fn name(self) -> &'a Identifier {
|
|
||||||
match self {
|
|
||||||
Self::FunctionDefinition(definition) => &definition.name,
|
|
||||||
Self::AsyncFunctionDefinition(definition) => &definition.name,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the function arguments (parameters).
|
|
||||||
pub fn arguments(self) -> &'a Parameters {
|
|
||||||
match self {
|
|
||||||
Self::FunctionDefinition(definition) => definition.parameters.as_ref(),
|
|
||||||
Self::AsyncFunctionDefinition(definition) => definition.parameters.as_ref(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the function's body
|
|
||||||
pub const fn body(self) -> &'a Suite {
|
|
||||||
match self {
|
|
||||||
Self::FunctionDefinition(definition) => &definition.body,
|
|
||||||
Self::AsyncFunctionDefinition(definition) => &definition.body,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the decorators attributing the function.
|
|
||||||
pub fn decorators(self) -> &'a [Decorator] {
|
|
||||||
match self {
|
|
||||||
Self::FunctionDefinition(definition) => &definition.decorator_list,
|
|
||||||
Self::AsyncFunctionDefinition(definition) => &definition.decorator_list,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn returns(self) -> Option<&'a Expr> {
|
|
||||||
match self {
|
|
||||||
Self::FunctionDefinition(definition) => definition.returns.as_deref(),
|
|
||||||
Self::AsyncFunctionDefinition(definition) => definition.returns.as_deref(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn type_params(self) -> Option<&'a TypeParams> {
|
|
||||||
match self {
|
|
||||||
Self::FunctionDefinition(definition) => definition.type_params.as_ref(),
|
|
||||||
Self::AsyncFunctionDefinition(definition) => definition.type_params.as_ref(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if this is [`Self::AsyncFunctionDefinition`]
|
|
||||||
pub const fn is_async(self) -> bool {
|
|
||||||
matches!(self, Self::AsyncFunctionDefinition(_))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ranged for AnyFunctionDefinition<'_> {
|
|
||||||
fn range(&self) -> TextRange {
|
|
||||||
match self {
|
|
||||||
AnyFunctionDefinition::FunctionDefinition(definition) => definition.range(),
|
|
||||||
AnyFunctionDefinition::AsyncFunctionDefinition(definition) => definition.range(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a StmtFunctionDef> for AnyFunctionDefinition<'a> {
|
|
||||||
fn from(value: &'a StmtFunctionDef) -> Self {
|
|
||||||
Self::FunctionDefinition(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a StmtAsyncFunctionDef> for AnyFunctionDefinition<'a> {
|
|
||||||
fn from(value: &'a StmtAsyncFunctionDef) -> Self {
|
|
||||||
Self::AsyncFunctionDefinition(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<AnyFunctionDefinition<'a>> for AnyNodeRef<'a> {
|
|
||||||
fn from(value: AnyFunctionDefinition<'a>) -> Self {
|
|
||||||
match value {
|
|
||||||
AnyFunctionDefinition::FunctionDefinition(function_def) => {
|
|
||||||
AnyNodeRef::StmtFunctionDef(function_def)
|
|
||||||
}
|
|
||||||
AnyFunctionDefinition::AsyncFunctionDefinition(async_def) => {
|
|
||||||
AnyNodeRef::StmtAsyncFunctionDef(async_def)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a AnyFunctionDefinition<'a>> for AnyNodeRef<'a> {
|
|
||||||
fn from(value: &'a AnyFunctionDefinition<'a>) -> Self {
|
|
||||||
(*value).into()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -18,14 +18,11 @@ pub const fn is_compound_statement(stmt: &Stmt) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
stmt,
|
stmt,
|
||||||
Stmt::FunctionDef(_)
|
Stmt::FunctionDef(_)
|
||||||
| Stmt::AsyncFunctionDef(_)
|
|
||||||
| Stmt::ClassDef(_)
|
| Stmt::ClassDef(_)
|
||||||
| Stmt::While(_)
|
| Stmt::While(_)
|
||||||
| Stmt::For(_)
|
| Stmt::For(_)
|
||||||
| Stmt::AsyncFor(_)
|
|
||||||
| Stmt::Match(_)
|
| Stmt::Match(_)
|
||||||
| Stmt::With(_)
|
| Stmt::With(_)
|
||||||
| Stmt::AsyncWith(_)
|
|
||||||
| Stmt::If(_)
|
| Stmt::If(_)
|
||||||
| Stmt::Try(_)
|
| Stmt::Try(_)
|
||||||
| Stmt::TryStar(_)
|
| Stmt::TryStar(_)
|
||||||
|
@ -321,14 +318,6 @@ where
|
||||||
decorator_list,
|
decorator_list,
|
||||||
returns,
|
returns,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
parameters,
|
|
||||||
type_params,
|
|
||||||
body,
|
|
||||||
decorator_list,
|
|
||||||
returns,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
parameters
|
parameters
|
||||||
.posonlyargs
|
.posonlyargs
|
||||||
|
@ -439,13 +428,6 @@ where
|
||||||
body,
|
body,
|
||||||
orelse,
|
orelse,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
|
||||||
target,
|
|
||||||
iter,
|
|
||||||
body,
|
|
||||||
orelse,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
any_over_expr(target, func)
|
any_over_expr(target, func)
|
||||||
|| any_over_expr(iter, func)
|
|| any_over_expr(iter, func)
|
||||||
|
@ -474,8 +456,7 @@ where
|
||||||
|| any_over_body(&clause.body, func)
|
|| any_over_body(&clause.body, func)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Stmt::With(ast::StmtWith { items, body, .. })
|
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
|
||||||
items.iter().any(|with_item| {
|
items.iter().any(|with_item| {
|
||||||
any_over_expr(&with_item.context_expr, func)
|
any_over_expr(&with_item.context_expr, func)
|
||||||
|| with_item
|
|| with_item
|
||||||
|
@ -912,7 +893,7 @@ where
|
||||||
{
|
{
|
||||||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||||
// Don't recurse.
|
// Don't recurse.
|
||||||
}
|
}
|
||||||
Stmt::Return(stmt) => self.returns.push(stmt),
|
Stmt::Return(stmt) => self.returns.push(stmt),
|
||||||
|
@ -941,11 +922,7 @@ where
|
||||||
self.raises
|
self.raises
|
||||||
.push((stmt.range(), exc.as_deref(), cause.as_deref()));
|
.push((stmt.range(), exc.as_deref(), cause.as_deref()));
|
||||||
}
|
}
|
||||||
Stmt::ClassDef(_)
|
Stmt::ClassDef(_) | Stmt::FunctionDef(_) | Stmt::Try(_) | Stmt::TryStar(_) => {}
|
||||||
| Stmt::FunctionDef(_)
|
|
||||||
| Stmt::AsyncFunctionDef(_)
|
|
||||||
| Stmt::Try(_)
|
|
||||||
| Stmt::TryStar(_) => {}
|
|
||||||
Stmt::If(ast::StmtIf {
|
Stmt::If(ast::StmtIf {
|
||||||
body,
|
body,
|
||||||
elif_else_clauses,
|
elif_else_clauses,
|
||||||
|
@ -958,9 +935,7 @@ where
|
||||||
}
|
}
|
||||||
Stmt::While(ast::StmtWhile { body, .. })
|
Stmt::While(ast::StmtWhile { body, .. })
|
||||||
| Stmt::With(ast::StmtWith { body, .. })
|
| Stmt::With(ast::StmtWith { body, .. })
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. })
|
| Stmt::For(ast::StmtFor { body, .. }) => {
|
||||||
| Stmt::For(ast::StmtFor { body, .. })
|
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, .. }) => {
|
|
||||||
walk_body(self, body);
|
walk_body(self, body);
|
||||||
}
|
}
|
||||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||||
|
|
|
@ -31,8 +31,7 @@ impl Identifier for Stmt {
|
||||||
fn identifier(&self) -> TextRange {
|
fn identifier(&self) -> TextRange {
|
||||||
match self {
|
match self {
|
||||||
Stmt::ClassDef(ast::StmtClassDef { name, .. })
|
Stmt::ClassDef(ast::StmtClassDef { name, .. })
|
||||||
| Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
| Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => name.range(),
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => name.range(),
|
|
||||||
_ => self.range(),
|
_ => self.range(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,10 +84,9 @@ pub fn except(handler: &ExceptHandler, source: &str) -> TextRange {
|
||||||
.expect("Failed to find `except` token in `ExceptHandler`")
|
.expect("Failed to find `except` token in `ExceptHandler`")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the [`TextRange`] of the `else` token in a `For`, `AsyncFor`, or `While` statement.
|
/// Return the [`TextRange`] of the `else` token in a `For` or `While` statement.
|
||||||
pub fn else_(stmt: &Stmt, source: &str) -> Option<TextRange> {
|
pub fn else_(stmt: &Stmt, source: &str) -> Option<TextRange> {
|
||||||
let (Stmt::For(ast::StmtFor { body, orelse, .. })
|
let (Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
|
||||||
| Stmt::While(ast::StmtWhile { body, orelse, .. })) = stmt
|
| Stmt::While(ast::StmtWhile { body, orelse, .. })) = stmt
|
||||||
else {
|
else {
|
||||||
return None;
|
return None;
|
||||||
|
|
|
@ -6,7 +6,6 @@ pub mod call_path;
|
||||||
pub mod cast;
|
pub mod cast;
|
||||||
pub mod comparable;
|
pub mod comparable;
|
||||||
pub mod docstrings;
|
pub mod docstrings;
|
||||||
pub mod function;
|
|
||||||
pub mod hashable;
|
pub mod hashable;
|
||||||
pub mod helpers;
|
pub mod helpers;
|
||||||
pub mod identifier;
|
pub mod identifier;
|
||||||
|
|
|
@ -24,7 +24,6 @@ pub enum AnyNode {
|
||||||
ModModule(ast::ModModule),
|
ModModule(ast::ModModule),
|
||||||
ModExpression(ast::ModExpression),
|
ModExpression(ast::ModExpression),
|
||||||
StmtFunctionDef(ast::StmtFunctionDef),
|
StmtFunctionDef(ast::StmtFunctionDef),
|
||||||
StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef),
|
|
||||||
StmtClassDef(ast::StmtClassDef),
|
StmtClassDef(ast::StmtClassDef),
|
||||||
StmtReturn(ast::StmtReturn),
|
StmtReturn(ast::StmtReturn),
|
||||||
StmtDelete(ast::StmtDelete),
|
StmtDelete(ast::StmtDelete),
|
||||||
|
@ -33,11 +32,9 @@ pub enum AnyNode {
|
||||||
StmtAugAssign(ast::StmtAugAssign),
|
StmtAugAssign(ast::StmtAugAssign),
|
||||||
StmtAnnAssign(ast::StmtAnnAssign),
|
StmtAnnAssign(ast::StmtAnnAssign),
|
||||||
StmtFor(ast::StmtFor),
|
StmtFor(ast::StmtFor),
|
||||||
StmtAsyncFor(ast::StmtAsyncFor),
|
|
||||||
StmtWhile(ast::StmtWhile),
|
StmtWhile(ast::StmtWhile),
|
||||||
StmtIf(ast::StmtIf),
|
StmtIf(ast::StmtIf),
|
||||||
StmtWith(ast::StmtWith),
|
StmtWith(ast::StmtWith),
|
||||||
StmtAsyncWith(ast::StmtAsyncWith),
|
|
||||||
StmtMatch(ast::StmtMatch),
|
StmtMatch(ast::StmtMatch),
|
||||||
StmtRaise(ast::StmtRaise),
|
StmtRaise(ast::StmtRaise),
|
||||||
StmtTry(ast::StmtTry),
|
StmtTry(ast::StmtTry),
|
||||||
|
@ -110,7 +107,6 @@ impl AnyNode {
|
||||||
pub fn statement(self) -> Option<Stmt> {
|
pub fn statement(self) -> Option<Stmt> {
|
||||||
match self {
|
match self {
|
||||||
AnyNode::StmtFunctionDef(node) => Some(Stmt::FunctionDef(node)),
|
AnyNode::StmtFunctionDef(node) => Some(Stmt::FunctionDef(node)),
|
||||||
AnyNode::StmtAsyncFunctionDef(node) => Some(Stmt::AsyncFunctionDef(node)),
|
|
||||||
AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)),
|
AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)),
|
||||||
AnyNode::StmtReturn(node) => Some(Stmt::Return(node)),
|
AnyNode::StmtReturn(node) => Some(Stmt::Return(node)),
|
||||||
AnyNode::StmtDelete(node) => Some(Stmt::Delete(node)),
|
AnyNode::StmtDelete(node) => Some(Stmt::Delete(node)),
|
||||||
|
@ -119,11 +115,9 @@ impl AnyNode {
|
||||||
AnyNode::StmtAugAssign(node) => Some(Stmt::AugAssign(node)),
|
AnyNode::StmtAugAssign(node) => Some(Stmt::AugAssign(node)),
|
||||||
AnyNode::StmtAnnAssign(node) => Some(Stmt::AnnAssign(node)),
|
AnyNode::StmtAnnAssign(node) => Some(Stmt::AnnAssign(node)),
|
||||||
AnyNode::StmtFor(node) => Some(Stmt::For(node)),
|
AnyNode::StmtFor(node) => Some(Stmt::For(node)),
|
||||||
AnyNode::StmtAsyncFor(node) => Some(Stmt::AsyncFor(node)),
|
|
||||||
AnyNode::StmtWhile(node) => Some(Stmt::While(node)),
|
AnyNode::StmtWhile(node) => Some(Stmt::While(node)),
|
||||||
AnyNode::StmtIf(node) => Some(Stmt::If(node)),
|
AnyNode::StmtIf(node) => Some(Stmt::If(node)),
|
||||||
AnyNode::StmtWith(node) => Some(Stmt::With(node)),
|
AnyNode::StmtWith(node) => Some(Stmt::With(node)),
|
||||||
AnyNode::StmtAsyncWith(node) => Some(Stmt::AsyncWith(node)),
|
|
||||||
AnyNode::StmtMatch(node) => Some(Stmt::Match(node)),
|
AnyNode::StmtMatch(node) => Some(Stmt::Match(node)),
|
||||||
AnyNode::StmtRaise(node) => Some(Stmt::Raise(node)),
|
AnyNode::StmtRaise(node) => Some(Stmt::Raise(node)),
|
||||||
AnyNode::StmtTry(node) => Some(Stmt::Try(node)),
|
AnyNode::StmtTry(node) => Some(Stmt::Try(node)),
|
||||||
|
@ -230,7 +224,6 @@ impl AnyNode {
|
||||||
AnyNode::ModModule(_)
|
AnyNode::ModModule(_)
|
||||||
| AnyNode::ModExpression(_)
|
| AnyNode::ModExpression(_)
|
||||||
| AnyNode::StmtFunctionDef(_)
|
| AnyNode::StmtFunctionDef(_)
|
||||||
| AnyNode::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNode::StmtClassDef(_)
|
| AnyNode::StmtClassDef(_)
|
||||||
| AnyNode::StmtReturn(_)
|
| AnyNode::StmtReturn(_)
|
||||||
| AnyNode::StmtDelete(_)
|
| AnyNode::StmtDelete(_)
|
||||||
|
@ -239,11 +232,9 @@ impl AnyNode {
|
||||||
| AnyNode::StmtAugAssign(_)
|
| AnyNode::StmtAugAssign(_)
|
||||||
| AnyNode::StmtAnnAssign(_)
|
| AnyNode::StmtAnnAssign(_)
|
||||||
| AnyNode::StmtFor(_)
|
| AnyNode::StmtFor(_)
|
||||||
| AnyNode::StmtAsyncFor(_)
|
|
||||||
| AnyNode::StmtWhile(_)
|
| AnyNode::StmtWhile(_)
|
||||||
| AnyNode::StmtIf(_)
|
| AnyNode::StmtIf(_)
|
||||||
| AnyNode::StmtWith(_)
|
| AnyNode::StmtWith(_)
|
||||||
| AnyNode::StmtAsyncWith(_)
|
|
||||||
| AnyNode::StmtMatch(_)
|
| AnyNode::StmtMatch(_)
|
||||||
| AnyNode::StmtRaise(_)
|
| AnyNode::StmtRaise(_)
|
||||||
| AnyNode::StmtTry(_)
|
| AnyNode::StmtTry(_)
|
||||||
|
@ -291,7 +282,6 @@ impl AnyNode {
|
||||||
AnyNode::ModExpression(node) => Some(Mod::Expression(node)),
|
AnyNode::ModExpression(node) => Some(Mod::Expression(node)),
|
||||||
|
|
||||||
AnyNode::StmtFunctionDef(_)
|
AnyNode::StmtFunctionDef(_)
|
||||||
| AnyNode::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNode::StmtClassDef(_)
|
| AnyNode::StmtClassDef(_)
|
||||||
| AnyNode::StmtReturn(_)
|
| AnyNode::StmtReturn(_)
|
||||||
| AnyNode::StmtDelete(_)
|
| AnyNode::StmtDelete(_)
|
||||||
|
@ -300,11 +290,9 @@ impl AnyNode {
|
||||||
| AnyNode::StmtAugAssign(_)
|
| AnyNode::StmtAugAssign(_)
|
||||||
| AnyNode::StmtAnnAssign(_)
|
| AnyNode::StmtAnnAssign(_)
|
||||||
| AnyNode::StmtFor(_)
|
| AnyNode::StmtFor(_)
|
||||||
| AnyNode::StmtAsyncFor(_)
|
|
||||||
| AnyNode::StmtWhile(_)
|
| AnyNode::StmtWhile(_)
|
||||||
| AnyNode::StmtIf(_)
|
| AnyNode::StmtIf(_)
|
||||||
| AnyNode::StmtWith(_)
|
| AnyNode::StmtWith(_)
|
||||||
| AnyNode::StmtAsyncWith(_)
|
|
||||||
| AnyNode::StmtMatch(_)
|
| AnyNode::StmtMatch(_)
|
||||||
| AnyNode::StmtRaise(_)
|
| AnyNode::StmtRaise(_)
|
||||||
| AnyNode::StmtTry(_)
|
| AnyNode::StmtTry(_)
|
||||||
|
@ -388,7 +376,6 @@ impl AnyNode {
|
||||||
AnyNode::ModModule(_)
|
AnyNode::ModModule(_)
|
||||||
| AnyNode::ModExpression(_)
|
| AnyNode::ModExpression(_)
|
||||||
| AnyNode::StmtFunctionDef(_)
|
| AnyNode::StmtFunctionDef(_)
|
||||||
| AnyNode::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNode::StmtClassDef(_)
|
| AnyNode::StmtClassDef(_)
|
||||||
| AnyNode::StmtReturn(_)
|
| AnyNode::StmtReturn(_)
|
||||||
| AnyNode::StmtDelete(_)
|
| AnyNode::StmtDelete(_)
|
||||||
|
@ -397,11 +384,9 @@ impl AnyNode {
|
||||||
| AnyNode::StmtAugAssign(_)
|
| AnyNode::StmtAugAssign(_)
|
||||||
| AnyNode::StmtAnnAssign(_)
|
| AnyNode::StmtAnnAssign(_)
|
||||||
| AnyNode::StmtFor(_)
|
| AnyNode::StmtFor(_)
|
||||||
| AnyNode::StmtAsyncFor(_)
|
|
||||||
| AnyNode::StmtWhile(_)
|
| AnyNode::StmtWhile(_)
|
||||||
| AnyNode::StmtIf(_)
|
| AnyNode::StmtIf(_)
|
||||||
| AnyNode::StmtWith(_)
|
| AnyNode::StmtWith(_)
|
||||||
| AnyNode::StmtAsyncWith(_)
|
|
||||||
| AnyNode::StmtMatch(_)
|
| AnyNode::StmtMatch(_)
|
||||||
| AnyNode::StmtRaise(_)
|
| AnyNode::StmtRaise(_)
|
||||||
| AnyNode::StmtTry(_)
|
| AnyNode::StmtTry(_)
|
||||||
|
@ -470,7 +455,6 @@ impl AnyNode {
|
||||||
AnyNode::ModModule(_)
|
AnyNode::ModModule(_)
|
||||||
| AnyNode::ModExpression(_)
|
| AnyNode::ModExpression(_)
|
||||||
| AnyNode::StmtFunctionDef(_)
|
| AnyNode::StmtFunctionDef(_)
|
||||||
| AnyNode::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNode::StmtClassDef(_)
|
| AnyNode::StmtClassDef(_)
|
||||||
| AnyNode::StmtReturn(_)
|
| AnyNode::StmtReturn(_)
|
||||||
| AnyNode::StmtDelete(_)
|
| AnyNode::StmtDelete(_)
|
||||||
|
@ -479,11 +463,9 @@ impl AnyNode {
|
||||||
| AnyNode::StmtAugAssign(_)
|
| AnyNode::StmtAugAssign(_)
|
||||||
| AnyNode::StmtAnnAssign(_)
|
| AnyNode::StmtAnnAssign(_)
|
||||||
| AnyNode::StmtFor(_)
|
| AnyNode::StmtFor(_)
|
||||||
| AnyNode::StmtAsyncFor(_)
|
|
||||||
| AnyNode::StmtWhile(_)
|
| AnyNode::StmtWhile(_)
|
||||||
| AnyNode::StmtIf(_)
|
| AnyNode::StmtIf(_)
|
||||||
| AnyNode::StmtWith(_)
|
| AnyNode::StmtWith(_)
|
||||||
| AnyNode::StmtAsyncWith(_)
|
|
||||||
| AnyNode::StmtMatch(_)
|
| AnyNode::StmtMatch(_)
|
||||||
| AnyNode::StmtRaise(_)
|
| AnyNode::StmtRaise(_)
|
||||||
| AnyNode::StmtTry(_)
|
| AnyNode::StmtTry(_)
|
||||||
|
@ -577,7 +559,6 @@ impl AnyNode {
|
||||||
Self::ModModule(node) => AnyNodeRef::ModModule(node),
|
Self::ModModule(node) => AnyNodeRef::ModModule(node),
|
||||||
Self::ModExpression(node) => AnyNodeRef::ModExpression(node),
|
Self::ModExpression(node) => AnyNodeRef::ModExpression(node),
|
||||||
Self::StmtFunctionDef(node) => AnyNodeRef::StmtFunctionDef(node),
|
Self::StmtFunctionDef(node) => AnyNodeRef::StmtFunctionDef(node),
|
||||||
Self::StmtAsyncFunctionDef(node) => AnyNodeRef::StmtAsyncFunctionDef(node),
|
|
||||||
Self::StmtClassDef(node) => AnyNodeRef::StmtClassDef(node),
|
Self::StmtClassDef(node) => AnyNodeRef::StmtClassDef(node),
|
||||||
Self::StmtReturn(node) => AnyNodeRef::StmtReturn(node),
|
Self::StmtReturn(node) => AnyNodeRef::StmtReturn(node),
|
||||||
Self::StmtDelete(node) => AnyNodeRef::StmtDelete(node),
|
Self::StmtDelete(node) => AnyNodeRef::StmtDelete(node),
|
||||||
|
@ -586,11 +567,9 @@ impl AnyNode {
|
||||||
Self::StmtAugAssign(node) => AnyNodeRef::StmtAugAssign(node),
|
Self::StmtAugAssign(node) => AnyNodeRef::StmtAugAssign(node),
|
||||||
Self::StmtAnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
|
Self::StmtAnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
|
||||||
Self::StmtFor(node) => AnyNodeRef::StmtFor(node),
|
Self::StmtFor(node) => AnyNodeRef::StmtFor(node),
|
||||||
Self::StmtAsyncFor(node) => AnyNodeRef::StmtAsyncFor(node),
|
|
||||||
Self::StmtWhile(node) => AnyNodeRef::StmtWhile(node),
|
Self::StmtWhile(node) => AnyNodeRef::StmtWhile(node),
|
||||||
Self::StmtIf(node) => AnyNodeRef::StmtIf(node),
|
Self::StmtIf(node) => AnyNodeRef::StmtIf(node),
|
||||||
Self::StmtWith(node) => AnyNodeRef::StmtWith(node),
|
Self::StmtWith(node) => AnyNodeRef::StmtWith(node),
|
||||||
Self::StmtAsyncWith(node) => AnyNodeRef::StmtAsyncWith(node),
|
|
||||||
Self::StmtMatch(node) => AnyNodeRef::StmtMatch(node),
|
Self::StmtMatch(node) => AnyNodeRef::StmtMatch(node),
|
||||||
Self::StmtRaise(node) => AnyNodeRef::StmtRaise(node),
|
Self::StmtRaise(node) => AnyNodeRef::StmtRaise(node),
|
||||||
Self::StmtTry(node) => AnyNodeRef::StmtTry(node),
|
Self::StmtTry(node) => AnyNodeRef::StmtTry(node),
|
||||||
|
@ -750,34 +729,6 @@ impl AstNode for ast::StmtFunctionDef {
|
||||||
AnyNode::from(self)
|
AnyNode::from(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AstNode for ast::StmtAsyncFunctionDef {
|
|
||||||
fn cast(kind: AnyNode) -> Option<Self>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
if let AnyNode::StmtAsyncFunctionDef(node) = kind {
|
|
||||||
Some(node)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
|
||||||
if let AnyNodeRef::StmtAsyncFunctionDef(node) = kind {
|
|
||||||
Some(node)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
|
||||||
AnyNodeRef::from(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_any_node(self) -> AnyNode {
|
|
||||||
AnyNode::from(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl AstNode for ast::StmtClassDef {
|
impl AstNode for ast::StmtClassDef {
|
||||||
fn cast(kind: AnyNode) -> Option<Self>
|
fn cast(kind: AnyNode) -> Option<Self>
|
||||||
where
|
where
|
||||||
|
@ -1002,34 +953,6 @@ impl AstNode for ast::StmtFor {
|
||||||
AnyNode::from(self)
|
AnyNode::from(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AstNode for ast::StmtAsyncFor {
|
|
||||||
fn cast(kind: AnyNode) -> Option<Self>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
if let AnyNode::StmtAsyncFor(node) = kind {
|
|
||||||
Some(node)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
|
||||||
if let AnyNodeRef::StmtAsyncFor(node) = kind {
|
|
||||||
Some(node)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
|
||||||
AnyNodeRef::from(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_any_node(self) -> AnyNode {
|
|
||||||
AnyNode::from(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl AstNode for ast::StmtWhile {
|
impl AstNode for ast::StmtWhile {
|
||||||
fn cast(kind: AnyNode) -> Option<Self>
|
fn cast(kind: AnyNode) -> Option<Self>
|
||||||
where
|
where
|
||||||
|
@ -1142,34 +1065,6 @@ impl AstNode for ast::StmtWith {
|
||||||
AnyNode::from(self)
|
AnyNode::from(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AstNode for ast::StmtAsyncWith {
|
|
||||||
fn cast(kind: AnyNode) -> Option<Self>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
if let AnyNode::StmtAsyncWith(node) = kind {
|
|
||||||
Some(node)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
|
||||||
if let AnyNodeRef::StmtAsyncWith(node) = kind {
|
|
||||||
Some(node)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
|
||||||
AnyNodeRef::from(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_any_node(self) -> AnyNode {
|
|
||||||
AnyNode::from(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl AstNode for ast::StmtMatch {
|
impl AstNode for ast::StmtMatch {
|
||||||
fn cast(kind: AnyNode) -> Option<Self>
|
fn cast(kind: AnyNode) -> Option<Self>
|
||||||
where
|
where
|
||||||
|
@ -2996,7 +2891,6 @@ impl From<Stmt> for AnyNode {
|
||||||
fn from(stmt: Stmt) -> Self {
|
fn from(stmt: Stmt) -> Self {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(node) => AnyNode::StmtFunctionDef(node),
|
Stmt::FunctionDef(node) => AnyNode::StmtFunctionDef(node),
|
||||||
Stmt::AsyncFunctionDef(node) => AnyNode::StmtAsyncFunctionDef(node),
|
|
||||||
Stmt::ClassDef(node) => AnyNode::StmtClassDef(node),
|
Stmt::ClassDef(node) => AnyNode::StmtClassDef(node),
|
||||||
Stmt::Return(node) => AnyNode::StmtReturn(node),
|
Stmt::Return(node) => AnyNode::StmtReturn(node),
|
||||||
Stmt::Delete(node) => AnyNode::StmtDelete(node),
|
Stmt::Delete(node) => AnyNode::StmtDelete(node),
|
||||||
|
@ -3005,11 +2899,9 @@ impl From<Stmt> for AnyNode {
|
||||||
Stmt::AugAssign(node) => AnyNode::StmtAugAssign(node),
|
Stmt::AugAssign(node) => AnyNode::StmtAugAssign(node),
|
||||||
Stmt::AnnAssign(node) => AnyNode::StmtAnnAssign(node),
|
Stmt::AnnAssign(node) => AnyNode::StmtAnnAssign(node),
|
||||||
Stmt::For(node) => AnyNode::StmtFor(node),
|
Stmt::For(node) => AnyNode::StmtFor(node),
|
||||||
Stmt::AsyncFor(node) => AnyNode::StmtAsyncFor(node),
|
|
||||||
Stmt::While(node) => AnyNode::StmtWhile(node),
|
Stmt::While(node) => AnyNode::StmtWhile(node),
|
||||||
Stmt::If(node) => AnyNode::StmtIf(node),
|
Stmt::If(node) => AnyNode::StmtIf(node),
|
||||||
Stmt::With(node) => AnyNode::StmtWith(node),
|
Stmt::With(node) => AnyNode::StmtWith(node),
|
||||||
Stmt::AsyncWith(node) => AnyNode::StmtAsyncWith(node),
|
|
||||||
Stmt::Match(node) => AnyNode::StmtMatch(node),
|
Stmt::Match(node) => AnyNode::StmtMatch(node),
|
||||||
Stmt::Raise(node) => AnyNode::StmtRaise(node),
|
Stmt::Raise(node) => AnyNode::StmtRaise(node),
|
||||||
Stmt::Try(node) => AnyNode::StmtTry(node),
|
Stmt::Try(node) => AnyNode::StmtTry(node),
|
||||||
|
@ -3113,12 +3005,6 @@ impl From<ast::StmtFunctionDef> for AnyNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ast::StmtAsyncFunctionDef> for AnyNode {
|
|
||||||
fn from(node: ast::StmtAsyncFunctionDef) -> Self {
|
|
||||||
AnyNode::StmtAsyncFunctionDef(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ast::StmtClassDef> for AnyNode {
|
impl From<ast::StmtClassDef> for AnyNode {
|
||||||
fn from(node: ast::StmtClassDef) -> Self {
|
fn from(node: ast::StmtClassDef) -> Self {
|
||||||
AnyNode::StmtClassDef(node)
|
AnyNode::StmtClassDef(node)
|
||||||
|
@ -3167,12 +3053,6 @@ impl From<ast::StmtFor> for AnyNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ast::StmtAsyncFor> for AnyNode {
|
|
||||||
fn from(node: ast::StmtAsyncFor) -> Self {
|
|
||||||
AnyNode::StmtAsyncFor(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ast::StmtWhile> for AnyNode {
|
impl From<ast::StmtWhile> for AnyNode {
|
||||||
fn from(node: ast::StmtWhile) -> Self {
|
fn from(node: ast::StmtWhile) -> Self {
|
||||||
AnyNode::StmtWhile(node)
|
AnyNode::StmtWhile(node)
|
||||||
|
@ -3197,12 +3077,6 @@ impl From<ast::StmtWith> for AnyNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ast::StmtAsyncWith> for AnyNode {
|
|
||||||
fn from(node: ast::StmtAsyncWith) -> Self {
|
|
||||||
AnyNode::StmtAsyncWith(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ast::StmtMatch> for AnyNode {
|
impl From<ast::StmtMatch> for AnyNode {
|
||||||
fn from(node: ast::StmtMatch) -> Self {
|
fn from(node: ast::StmtMatch) -> Self {
|
||||||
AnyNode::StmtMatch(node)
|
AnyNode::StmtMatch(node)
|
||||||
|
@ -3588,7 +3462,6 @@ impl Ranged for AnyNode {
|
||||||
AnyNode::ModModule(node) => node.range(),
|
AnyNode::ModModule(node) => node.range(),
|
||||||
AnyNode::ModExpression(node) => node.range(),
|
AnyNode::ModExpression(node) => node.range(),
|
||||||
AnyNode::StmtFunctionDef(node) => node.range(),
|
AnyNode::StmtFunctionDef(node) => node.range(),
|
||||||
AnyNode::StmtAsyncFunctionDef(node) => node.range(),
|
|
||||||
AnyNode::StmtClassDef(node) => node.range(),
|
AnyNode::StmtClassDef(node) => node.range(),
|
||||||
AnyNode::StmtReturn(node) => node.range(),
|
AnyNode::StmtReturn(node) => node.range(),
|
||||||
AnyNode::StmtDelete(node) => node.range(),
|
AnyNode::StmtDelete(node) => node.range(),
|
||||||
|
@ -3597,11 +3470,9 @@ impl Ranged for AnyNode {
|
||||||
AnyNode::StmtAugAssign(node) => node.range(),
|
AnyNode::StmtAugAssign(node) => node.range(),
|
||||||
AnyNode::StmtAnnAssign(node) => node.range(),
|
AnyNode::StmtAnnAssign(node) => node.range(),
|
||||||
AnyNode::StmtFor(node) => node.range(),
|
AnyNode::StmtFor(node) => node.range(),
|
||||||
AnyNode::StmtAsyncFor(node) => node.range(),
|
|
||||||
AnyNode::StmtWhile(node) => node.range(),
|
AnyNode::StmtWhile(node) => node.range(),
|
||||||
AnyNode::StmtIf(node) => node.range(),
|
AnyNode::StmtIf(node) => node.range(),
|
||||||
AnyNode::StmtWith(node) => node.range(),
|
AnyNode::StmtWith(node) => node.range(),
|
||||||
AnyNode::StmtAsyncWith(node) => node.range(),
|
|
||||||
AnyNode::StmtMatch(node) => node.range(),
|
AnyNode::StmtMatch(node) => node.range(),
|
||||||
AnyNode::StmtRaise(node) => node.range(),
|
AnyNode::StmtRaise(node) => node.range(),
|
||||||
AnyNode::StmtTry(node) => node.range(),
|
AnyNode::StmtTry(node) => node.range(),
|
||||||
|
@ -3677,7 +3548,6 @@ pub enum AnyNodeRef<'a> {
|
||||||
ModModule(&'a ast::ModModule),
|
ModModule(&'a ast::ModModule),
|
||||||
ModExpression(&'a ast::ModExpression),
|
ModExpression(&'a ast::ModExpression),
|
||||||
StmtFunctionDef(&'a ast::StmtFunctionDef),
|
StmtFunctionDef(&'a ast::StmtFunctionDef),
|
||||||
StmtAsyncFunctionDef(&'a ast::StmtAsyncFunctionDef),
|
|
||||||
StmtClassDef(&'a ast::StmtClassDef),
|
StmtClassDef(&'a ast::StmtClassDef),
|
||||||
StmtReturn(&'a ast::StmtReturn),
|
StmtReturn(&'a ast::StmtReturn),
|
||||||
StmtDelete(&'a ast::StmtDelete),
|
StmtDelete(&'a ast::StmtDelete),
|
||||||
|
@ -3686,11 +3556,9 @@ pub enum AnyNodeRef<'a> {
|
||||||
StmtAugAssign(&'a ast::StmtAugAssign),
|
StmtAugAssign(&'a ast::StmtAugAssign),
|
||||||
StmtAnnAssign(&'a ast::StmtAnnAssign),
|
StmtAnnAssign(&'a ast::StmtAnnAssign),
|
||||||
StmtFor(&'a ast::StmtFor),
|
StmtFor(&'a ast::StmtFor),
|
||||||
StmtAsyncFor(&'a ast::StmtAsyncFor),
|
|
||||||
StmtWhile(&'a ast::StmtWhile),
|
StmtWhile(&'a ast::StmtWhile),
|
||||||
StmtIf(&'a ast::StmtIf),
|
StmtIf(&'a ast::StmtIf),
|
||||||
StmtWith(&'a ast::StmtWith),
|
StmtWith(&'a ast::StmtWith),
|
||||||
StmtAsyncWith(&'a ast::StmtAsyncWith),
|
|
||||||
StmtMatch(&'a ast::StmtMatch),
|
StmtMatch(&'a ast::StmtMatch),
|
||||||
StmtRaise(&'a ast::StmtRaise),
|
StmtRaise(&'a ast::StmtRaise),
|
||||||
StmtTry(&'a ast::StmtTry),
|
StmtTry(&'a ast::StmtTry),
|
||||||
|
@ -3765,7 +3633,6 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::ModModule(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::ModModule(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::ModExpression(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::ModExpression(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtFunctionDef(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtFunctionDef(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtAsyncFunctionDef(node) => NonNull::from(*node).cast(),
|
|
||||||
AnyNodeRef::StmtClassDef(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtClassDef(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtReturn(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtReturn(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtDelete(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtDelete(node) => NonNull::from(*node).cast(),
|
||||||
|
@ -3774,11 +3641,9 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::StmtAugAssign(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtAugAssign(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtAnnAssign(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtAnnAssign(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtFor(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtFor(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtAsyncFor(node) => NonNull::from(*node).cast(),
|
|
||||||
AnyNodeRef::StmtWhile(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtWhile(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtIf(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtIf(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtWith(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtWith(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtAsyncWith(node) => NonNull::from(*node).cast(),
|
|
||||||
AnyNodeRef::StmtMatch(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtMatch(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtRaise(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtRaise(node) => NonNull::from(*node).cast(),
|
||||||
AnyNodeRef::StmtTry(node) => NonNull::from(*node).cast(),
|
AnyNodeRef::StmtTry(node) => NonNull::from(*node).cast(),
|
||||||
|
@ -3859,7 +3724,6 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::ModModule(_) => NodeKind::ModModule,
|
AnyNodeRef::ModModule(_) => NodeKind::ModModule,
|
||||||
AnyNodeRef::ModExpression(_) => NodeKind::ModExpression,
|
AnyNodeRef::ModExpression(_) => NodeKind::ModExpression,
|
||||||
AnyNodeRef::StmtFunctionDef(_) => NodeKind::StmtFunctionDef,
|
AnyNodeRef::StmtFunctionDef(_) => NodeKind::StmtFunctionDef,
|
||||||
AnyNodeRef::StmtAsyncFunctionDef(_) => NodeKind::StmtAsyncFunctionDef,
|
|
||||||
AnyNodeRef::StmtClassDef(_) => NodeKind::StmtClassDef,
|
AnyNodeRef::StmtClassDef(_) => NodeKind::StmtClassDef,
|
||||||
AnyNodeRef::StmtReturn(_) => NodeKind::StmtReturn,
|
AnyNodeRef::StmtReturn(_) => NodeKind::StmtReturn,
|
||||||
AnyNodeRef::StmtDelete(_) => NodeKind::StmtDelete,
|
AnyNodeRef::StmtDelete(_) => NodeKind::StmtDelete,
|
||||||
|
@ -3868,11 +3732,9 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::StmtAugAssign(_) => NodeKind::StmtAugAssign,
|
AnyNodeRef::StmtAugAssign(_) => NodeKind::StmtAugAssign,
|
||||||
AnyNodeRef::StmtAnnAssign(_) => NodeKind::StmtAnnAssign,
|
AnyNodeRef::StmtAnnAssign(_) => NodeKind::StmtAnnAssign,
|
||||||
AnyNodeRef::StmtFor(_) => NodeKind::StmtFor,
|
AnyNodeRef::StmtFor(_) => NodeKind::StmtFor,
|
||||||
AnyNodeRef::StmtAsyncFor(_) => NodeKind::StmtAsyncFor,
|
|
||||||
AnyNodeRef::StmtWhile(_) => NodeKind::StmtWhile,
|
AnyNodeRef::StmtWhile(_) => NodeKind::StmtWhile,
|
||||||
AnyNodeRef::StmtIf(_) => NodeKind::StmtIf,
|
AnyNodeRef::StmtIf(_) => NodeKind::StmtIf,
|
||||||
AnyNodeRef::StmtWith(_) => NodeKind::StmtWith,
|
AnyNodeRef::StmtWith(_) => NodeKind::StmtWith,
|
||||||
AnyNodeRef::StmtAsyncWith(_) => NodeKind::StmtAsyncWith,
|
|
||||||
AnyNodeRef::StmtMatch(_) => NodeKind::StmtMatch,
|
AnyNodeRef::StmtMatch(_) => NodeKind::StmtMatch,
|
||||||
AnyNodeRef::StmtRaise(_) => NodeKind::StmtRaise,
|
AnyNodeRef::StmtRaise(_) => NodeKind::StmtRaise,
|
||||||
AnyNodeRef::StmtTry(_) => NodeKind::StmtTry,
|
AnyNodeRef::StmtTry(_) => NodeKind::StmtTry,
|
||||||
|
@ -3945,7 +3807,6 @@ impl AnyNodeRef<'_> {
|
||||||
pub const fn is_statement(self) -> bool {
|
pub const fn is_statement(self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
AnyNodeRef::StmtFunctionDef(_)
|
AnyNodeRef::StmtFunctionDef(_)
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNodeRef::StmtClassDef(_)
|
| AnyNodeRef::StmtClassDef(_)
|
||||||
| AnyNodeRef::StmtReturn(_)
|
| AnyNodeRef::StmtReturn(_)
|
||||||
| AnyNodeRef::StmtDelete(_)
|
| AnyNodeRef::StmtDelete(_)
|
||||||
|
@ -3954,11 +3815,9 @@ impl AnyNodeRef<'_> {
|
||||||
| AnyNodeRef::StmtAugAssign(_)
|
| AnyNodeRef::StmtAugAssign(_)
|
||||||
| AnyNodeRef::StmtAnnAssign(_)
|
| AnyNodeRef::StmtAnnAssign(_)
|
||||||
| AnyNodeRef::StmtFor(_)
|
| AnyNodeRef::StmtFor(_)
|
||||||
| AnyNodeRef::StmtAsyncFor(_)
|
|
||||||
| AnyNodeRef::StmtWhile(_)
|
| AnyNodeRef::StmtWhile(_)
|
||||||
| AnyNodeRef::StmtIf(_)
|
| AnyNodeRef::StmtIf(_)
|
||||||
| AnyNodeRef::StmtWith(_)
|
| AnyNodeRef::StmtWith(_)
|
||||||
| AnyNodeRef::StmtAsyncWith(_)
|
|
||||||
| AnyNodeRef::StmtMatch(_)
|
| AnyNodeRef::StmtMatch(_)
|
||||||
| AnyNodeRef::StmtRaise(_)
|
| AnyNodeRef::StmtRaise(_)
|
||||||
| AnyNodeRef::StmtTry(_)
|
| AnyNodeRef::StmtTry(_)
|
||||||
|
@ -4065,7 +3924,6 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::ModModule(_)
|
AnyNodeRef::ModModule(_)
|
||||||
| AnyNodeRef::ModExpression(_)
|
| AnyNodeRef::ModExpression(_)
|
||||||
| AnyNodeRef::StmtFunctionDef(_)
|
| AnyNodeRef::StmtFunctionDef(_)
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNodeRef::StmtClassDef(_)
|
| AnyNodeRef::StmtClassDef(_)
|
||||||
| AnyNodeRef::StmtReturn(_)
|
| AnyNodeRef::StmtReturn(_)
|
||||||
| AnyNodeRef::StmtDelete(_)
|
| AnyNodeRef::StmtDelete(_)
|
||||||
|
@ -4074,11 +3932,9 @@ impl AnyNodeRef<'_> {
|
||||||
| AnyNodeRef::StmtAugAssign(_)
|
| AnyNodeRef::StmtAugAssign(_)
|
||||||
| AnyNodeRef::StmtAnnAssign(_)
|
| AnyNodeRef::StmtAnnAssign(_)
|
||||||
| AnyNodeRef::StmtFor(_)
|
| AnyNodeRef::StmtFor(_)
|
||||||
| AnyNodeRef::StmtAsyncFor(_)
|
|
||||||
| AnyNodeRef::StmtWhile(_)
|
| AnyNodeRef::StmtWhile(_)
|
||||||
| AnyNodeRef::StmtIf(_)
|
| AnyNodeRef::StmtIf(_)
|
||||||
| AnyNodeRef::StmtWith(_)
|
| AnyNodeRef::StmtWith(_)
|
||||||
| AnyNodeRef::StmtAsyncWith(_)
|
|
||||||
| AnyNodeRef::StmtMatch(_)
|
| AnyNodeRef::StmtMatch(_)
|
||||||
| AnyNodeRef::StmtRaise(_)
|
| AnyNodeRef::StmtRaise(_)
|
||||||
| AnyNodeRef::StmtTry(_)
|
| AnyNodeRef::StmtTry(_)
|
||||||
|
@ -4125,7 +3981,6 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) => true,
|
AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) => true,
|
||||||
|
|
||||||
AnyNodeRef::StmtFunctionDef(_)
|
AnyNodeRef::StmtFunctionDef(_)
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNodeRef::StmtClassDef(_)
|
| AnyNodeRef::StmtClassDef(_)
|
||||||
| AnyNodeRef::StmtReturn(_)
|
| AnyNodeRef::StmtReturn(_)
|
||||||
| AnyNodeRef::StmtDelete(_)
|
| AnyNodeRef::StmtDelete(_)
|
||||||
|
@ -4134,11 +3989,9 @@ impl AnyNodeRef<'_> {
|
||||||
| AnyNodeRef::StmtAugAssign(_)
|
| AnyNodeRef::StmtAugAssign(_)
|
||||||
| AnyNodeRef::StmtAnnAssign(_)
|
| AnyNodeRef::StmtAnnAssign(_)
|
||||||
| AnyNodeRef::StmtFor(_)
|
| AnyNodeRef::StmtFor(_)
|
||||||
| AnyNodeRef::StmtAsyncFor(_)
|
|
||||||
| AnyNodeRef::StmtWhile(_)
|
| AnyNodeRef::StmtWhile(_)
|
||||||
| AnyNodeRef::StmtIf(_)
|
| AnyNodeRef::StmtIf(_)
|
||||||
| AnyNodeRef::StmtWith(_)
|
| AnyNodeRef::StmtWith(_)
|
||||||
| AnyNodeRef::StmtAsyncWith(_)
|
|
||||||
| AnyNodeRef::StmtMatch(_)
|
| AnyNodeRef::StmtMatch(_)
|
||||||
| AnyNodeRef::StmtRaise(_)
|
| AnyNodeRef::StmtRaise(_)
|
||||||
| AnyNodeRef::StmtTry(_)
|
| AnyNodeRef::StmtTry(_)
|
||||||
|
@ -4222,7 +4075,6 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::ModModule(_)
|
AnyNodeRef::ModModule(_)
|
||||||
| AnyNodeRef::ModExpression(_)
|
| AnyNodeRef::ModExpression(_)
|
||||||
| AnyNodeRef::StmtFunctionDef(_)
|
| AnyNodeRef::StmtFunctionDef(_)
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNodeRef::StmtClassDef(_)
|
| AnyNodeRef::StmtClassDef(_)
|
||||||
| AnyNodeRef::StmtReturn(_)
|
| AnyNodeRef::StmtReturn(_)
|
||||||
| AnyNodeRef::StmtDelete(_)
|
| AnyNodeRef::StmtDelete(_)
|
||||||
|
@ -4231,11 +4083,9 @@ impl AnyNodeRef<'_> {
|
||||||
| AnyNodeRef::StmtAugAssign(_)
|
| AnyNodeRef::StmtAugAssign(_)
|
||||||
| AnyNodeRef::StmtAnnAssign(_)
|
| AnyNodeRef::StmtAnnAssign(_)
|
||||||
| AnyNodeRef::StmtFor(_)
|
| AnyNodeRef::StmtFor(_)
|
||||||
| AnyNodeRef::StmtAsyncFor(_)
|
|
||||||
| AnyNodeRef::StmtWhile(_)
|
| AnyNodeRef::StmtWhile(_)
|
||||||
| AnyNodeRef::StmtIf(_)
|
| AnyNodeRef::StmtIf(_)
|
||||||
| AnyNodeRef::StmtWith(_)
|
| AnyNodeRef::StmtWith(_)
|
||||||
| AnyNodeRef::StmtAsyncWith(_)
|
|
||||||
| AnyNodeRef::StmtMatch(_)
|
| AnyNodeRef::StmtMatch(_)
|
||||||
| AnyNodeRef::StmtRaise(_)
|
| AnyNodeRef::StmtRaise(_)
|
||||||
| AnyNodeRef::StmtTry(_)
|
| AnyNodeRef::StmtTry(_)
|
||||||
|
@ -4304,7 +4154,6 @@ impl AnyNodeRef<'_> {
|
||||||
AnyNodeRef::ModModule(_)
|
AnyNodeRef::ModModule(_)
|
||||||
| AnyNodeRef::ModExpression(_)
|
| AnyNodeRef::ModExpression(_)
|
||||||
| AnyNodeRef::StmtFunctionDef(_)
|
| AnyNodeRef::StmtFunctionDef(_)
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNodeRef::StmtClassDef(_)
|
| AnyNodeRef::StmtClassDef(_)
|
||||||
| AnyNodeRef::StmtReturn(_)
|
| AnyNodeRef::StmtReturn(_)
|
||||||
| AnyNodeRef::StmtDelete(_)
|
| AnyNodeRef::StmtDelete(_)
|
||||||
|
@ -4313,11 +4162,9 @@ impl AnyNodeRef<'_> {
|
||||||
| AnyNodeRef::StmtAugAssign(_)
|
| AnyNodeRef::StmtAugAssign(_)
|
||||||
| AnyNodeRef::StmtAnnAssign(_)
|
| AnyNodeRef::StmtAnnAssign(_)
|
||||||
| AnyNodeRef::StmtFor(_)
|
| AnyNodeRef::StmtFor(_)
|
||||||
| AnyNodeRef::StmtAsyncFor(_)
|
|
||||||
| AnyNodeRef::StmtWhile(_)
|
| AnyNodeRef::StmtWhile(_)
|
||||||
| AnyNodeRef::StmtIf(_)
|
| AnyNodeRef::StmtIf(_)
|
||||||
| AnyNodeRef::StmtWith(_)
|
| AnyNodeRef::StmtWith(_)
|
||||||
| AnyNodeRef::StmtAsyncWith(_)
|
|
||||||
| AnyNodeRef::StmtMatch(_)
|
| AnyNodeRef::StmtMatch(_)
|
||||||
| AnyNodeRef::StmtRaise(_)
|
| AnyNodeRef::StmtRaise(_)
|
||||||
| AnyNodeRef::StmtTry(_)
|
| AnyNodeRef::StmtTry(_)
|
||||||
|
@ -4391,13 +4238,10 @@ impl AnyNodeRef<'_> {
|
||||||
self,
|
self,
|
||||||
AnyNodeRef::StmtIf(_)
|
AnyNodeRef::StmtIf(_)
|
||||||
| AnyNodeRef::StmtFor(_)
|
| AnyNodeRef::StmtFor(_)
|
||||||
| AnyNodeRef::StmtAsyncFor(_)
|
|
||||||
| AnyNodeRef::StmtWhile(_)
|
| AnyNodeRef::StmtWhile(_)
|
||||||
| AnyNodeRef::StmtWith(_)
|
| AnyNodeRef::StmtWith(_)
|
||||||
| AnyNodeRef::StmtAsyncWith(_)
|
|
||||||
| AnyNodeRef::StmtMatch(_)
|
| AnyNodeRef::StmtMatch(_)
|
||||||
| AnyNodeRef::StmtFunctionDef(_)
|
| AnyNodeRef::StmtFunctionDef(_)
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
|
||||||
| AnyNodeRef::StmtClassDef(_)
|
| AnyNodeRef::StmtClassDef(_)
|
||||||
| AnyNodeRef::StmtTry(_)
|
| AnyNodeRef::StmtTry(_)
|
||||||
| AnyNodeRef::StmtTryStar(_)
|
| AnyNodeRef::StmtTryStar(_)
|
||||||
|
@ -4435,12 +4279,6 @@ impl<'a> From<&'a ast::StmtFunctionDef> for AnyNodeRef<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a ast::StmtAsyncFunctionDef> for AnyNodeRef<'a> {
|
|
||||||
fn from(node: &'a ast::StmtAsyncFunctionDef) -> Self {
|
|
||||||
AnyNodeRef::StmtAsyncFunctionDef(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a ast::StmtClassDef> for AnyNodeRef<'a> {
|
impl<'a> From<&'a ast::StmtClassDef> for AnyNodeRef<'a> {
|
||||||
fn from(node: &'a ast::StmtClassDef) -> Self {
|
fn from(node: &'a ast::StmtClassDef) -> Self {
|
||||||
AnyNodeRef::StmtClassDef(node)
|
AnyNodeRef::StmtClassDef(node)
|
||||||
|
@ -4489,12 +4327,6 @@ impl<'a> From<&'a ast::StmtFor> for AnyNodeRef<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a ast::StmtAsyncFor> for AnyNodeRef<'a> {
|
|
||||||
fn from(node: &'a ast::StmtAsyncFor) -> Self {
|
|
||||||
AnyNodeRef::StmtAsyncFor(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a ast::StmtWhile> for AnyNodeRef<'a> {
|
impl<'a> From<&'a ast::StmtWhile> for AnyNodeRef<'a> {
|
||||||
fn from(node: &'a ast::StmtWhile) -> Self {
|
fn from(node: &'a ast::StmtWhile) -> Self {
|
||||||
AnyNodeRef::StmtWhile(node)
|
AnyNodeRef::StmtWhile(node)
|
||||||
|
@ -4519,12 +4351,6 @@ impl<'a> From<&'a ast::StmtWith> for AnyNodeRef<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a ast::StmtAsyncWith> for AnyNodeRef<'a> {
|
|
||||||
fn from(node: &'a ast::StmtAsyncWith) -> Self {
|
|
||||||
AnyNodeRef::StmtAsyncWith(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a ast::StmtMatch> for AnyNodeRef<'a> {
|
impl<'a> From<&'a ast::StmtMatch> for AnyNodeRef<'a> {
|
||||||
fn from(node: &'a ast::StmtMatch) -> Self {
|
fn from(node: &'a ast::StmtMatch) -> Self {
|
||||||
AnyNodeRef::StmtMatch(node)
|
AnyNodeRef::StmtMatch(node)
|
||||||
|
@ -4864,7 +4690,6 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
|
||||||
fn from(stmt: &'a Stmt) -> Self {
|
fn from(stmt: &'a Stmt) -> Self {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(node) => AnyNodeRef::StmtFunctionDef(node),
|
Stmt::FunctionDef(node) => AnyNodeRef::StmtFunctionDef(node),
|
||||||
Stmt::AsyncFunctionDef(node) => AnyNodeRef::StmtAsyncFunctionDef(node),
|
|
||||||
Stmt::ClassDef(node) => AnyNodeRef::StmtClassDef(node),
|
Stmt::ClassDef(node) => AnyNodeRef::StmtClassDef(node),
|
||||||
Stmt::Return(node) => AnyNodeRef::StmtReturn(node),
|
Stmt::Return(node) => AnyNodeRef::StmtReturn(node),
|
||||||
Stmt::Delete(node) => AnyNodeRef::StmtDelete(node),
|
Stmt::Delete(node) => AnyNodeRef::StmtDelete(node),
|
||||||
|
@ -4873,11 +4698,9 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
|
||||||
Stmt::AugAssign(node) => AnyNodeRef::StmtAugAssign(node),
|
Stmt::AugAssign(node) => AnyNodeRef::StmtAugAssign(node),
|
||||||
Stmt::AnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
|
Stmt::AnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
|
||||||
Stmt::For(node) => AnyNodeRef::StmtFor(node),
|
Stmt::For(node) => AnyNodeRef::StmtFor(node),
|
||||||
Stmt::AsyncFor(node) => AnyNodeRef::StmtAsyncFor(node),
|
|
||||||
Stmt::While(node) => AnyNodeRef::StmtWhile(node),
|
Stmt::While(node) => AnyNodeRef::StmtWhile(node),
|
||||||
Stmt::If(node) => AnyNodeRef::StmtIf(node),
|
Stmt::If(node) => AnyNodeRef::StmtIf(node),
|
||||||
Stmt::With(node) => AnyNodeRef::StmtWith(node),
|
Stmt::With(node) => AnyNodeRef::StmtWith(node),
|
||||||
Stmt::AsyncWith(node) => AnyNodeRef::StmtAsyncWith(node),
|
|
||||||
Stmt::Match(node) => AnyNodeRef::StmtMatch(node),
|
Stmt::Match(node) => AnyNodeRef::StmtMatch(node),
|
||||||
Stmt::Raise(node) => AnyNodeRef::StmtRaise(node),
|
Stmt::Raise(node) => AnyNodeRef::StmtRaise(node),
|
||||||
Stmt::Try(node) => AnyNodeRef::StmtTry(node),
|
Stmt::Try(node) => AnyNodeRef::StmtTry(node),
|
||||||
|
@ -5027,7 +4850,6 @@ impl Ranged for AnyNodeRef<'_> {
|
||||||
AnyNodeRef::ModModule(node) => node.range(),
|
AnyNodeRef::ModModule(node) => node.range(),
|
||||||
AnyNodeRef::ModExpression(node) => node.range(),
|
AnyNodeRef::ModExpression(node) => node.range(),
|
||||||
AnyNodeRef::StmtFunctionDef(node) => node.range(),
|
AnyNodeRef::StmtFunctionDef(node) => node.range(),
|
||||||
AnyNodeRef::StmtAsyncFunctionDef(node) => node.range(),
|
|
||||||
AnyNodeRef::StmtClassDef(node) => node.range(),
|
AnyNodeRef::StmtClassDef(node) => node.range(),
|
||||||
AnyNodeRef::StmtReturn(node) => node.range(),
|
AnyNodeRef::StmtReturn(node) => node.range(),
|
||||||
AnyNodeRef::StmtDelete(node) => node.range(),
|
AnyNodeRef::StmtDelete(node) => node.range(),
|
||||||
|
@ -5036,11 +4858,9 @@ impl Ranged for AnyNodeRef<'_> {
|
||||||
AnyNodeRef::StmtAugAssign(node) => node.range(),
|
AnyNodeRef::StmtAugAssign(node) => node.range(),
|
||||||
AnyNodeRef::StmtAnnAssign(node) => node.range(),
|
AnyNodeRef::StmtAnnAssign(node) => node.range(),
|
||||||
AnyNodeRef::StmtFor(node) => node.range(),
|
AnyNodeRef::StmtFor(node) => node.range(),
|
||||||
AnyNodeRef::StmtAsyncFor(node) => node.range(),
|
|
||||||
AnyNodeRef::StmtWhile(node) => node.range(),
|
AnyNodeRef::StmtWhile(node) => node.range(),
|
||||||
AnyNodeRef::StmtIf(node) => node.range(),
|
AnyNodeRef::StmtIf(node) => node.range(),
|
||||||
AnyNodeRef::StmtWith(node) => node.range(),
|
AnyNodeRef::StmtWith(node) => node.range(),
|
||||||
AnyNodeRef::StmtAsyncWith(node) => node.range(),
|
|
||||||
AnyNodeRef::StmtMatch(node) => node.range(),
|
AnyNodeRef::StmtMatch(node) => node.range(),
|
||||||
AnyNodeRef::StmtRaise(node) => node.range(),
|
AnyNodeRef::StmtRaise(node) => node.range(),
|
||||||
AnyNodeRef::StmtTry(node) => node.range(),
|
AnyNodeRef::StmtTry(node) => node.range(),
|
||||||
|
@ -5118,7 +4938,6 @@ pub enum NodeKind {
|
||||||
ModExpression,
|
ModExpression,
|
||||||
ModFunctionType,
|
ModFunctionType,
|
||||||
StmtFunctionDef,
|
StmtFunctionDef,
|
||||||
StmtAsyncFunctionDef,
|
|
||||||
StmtClassDef,
|
StmtClassDef,
|
||||||
StmtReturn,
|
StmtReturn,
|
||||||
StmtDelete,
|
StmtDelete,
|
||||||
|
@ -5127,11 +4946,9 @@ pub enum NodeKind {
|
||||||
StmtAugAssign,
|
StmtAugAssign,
|
||||||
StmtAnnAssign,
|
StmtAnnAssign,
|
||||||
StmtFor,
|
StmtFor,
|
||||||
StmtAsyncFor,
|
|
||||||
StmtWhile,
|
StmtWhile,
|
||||||
StmtIf,
|
StmtIf,
|
||||||
StmtWith,
|
StmtWith,
|
||||||
StmtAsyncWith,
|
|
||||||
StmtMatch,
|
StmtMatch,
|
||||||
StmtRaise,
|
StmtRaise,
|
||||||
StmtTry,
|
StmtTry,
|
||||||
|
|
|
@ -45,8 +45,6 @@ impl From<ModExpression> for Mod {
|
||||||
pub enum Stmt {
|
pub enum Stmt {
|
||||||
#[is(name = "function_def_stmt")]
|
#[is(name = "function_def_stmt")]
|
||||||
FunctionDef(StmtFunctionDef),
|
FunctionDef(StmtFunctionDef),
|
||||||
#[is(name = "async_function_def_stmt")]
|
|
||||||
AsyncFunctionDef(StmtAsyncFunctionDef),
|
|
||||||
#[is(name = "class_def_stmt")]
|
#[is(name = "class_def_stmt")]
|
||||||
ClassDef(StmtClassDef),
|
ClassDef(StmtClassDef),
|
||||||
#[is(name = "return_stmt")]
|
#[is(name = "return_stmt")]
|
||||||
|
@ -63,16 +61,12 @@ pub enum Stmt {
|
||||||
TypeAlias(StmtTypeAlias),
|
TypeAlias(StmtTypeAlias),
|
||||||
#[is(name = "for_stmt")]
|
#[is(name = "for_stmt")]
|
||||||
For(StmtFor),
|
For(StmtFor),
|
||||||
#[is(name = "async_for_stmt")]
|
|
||||||
AsyncFor(StmtAsyncFor),
|
|
||||||
#[is(name = "while_stmt")]
|
#[is(name = "while_stmt")]
|
||||||
While(StmtWhile),
|
While(StmtWhile),
|
||||||
#[is(name = "if_stmt")]
|
#[is(name = "if_stmt")]
|
||||||
If(StmtIf),
|
If(StmtIf),
|
||||||
#[is(name = "with_stmt")]
|
#[is(name = "with_stmt")]
|
||||||
With(StmtWith),
|
With(StmtWith),
|
||||||
#[is(name = "async_with_stmt")]
|
|
||||||
AsyncWith(StmtAsyncWith),
|
|
||||||
#[is(name = "match_stmt")]
|
#[is(name = "match_stmt")]
|
||||||
Match(StmtMatch),
|
Match(StmtMatch),
|
||||||
#[is(name = "raise_stmt")]
|
#[is(name = "raise_stmt")]
|
||||||
|
@ -118,10 +112,15 @@ impl From<StmtLineMagic> for Stmt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See also [FunctionDef](https://docs.python.org/3/library/ast.html#ast.FunctionDef)
|
/// See also [FunctionDef](https://docs.python.org/3/library/ast.html#ast.FunctionDef) and
|
||||||
|
/// [AsyncFunctionDef](https://docs.python.org/3/library/ast.html#ast.AsyncFunctionDef).
|
||||||
|
///
|
||||||
|
/// This type differs from the original Python AST, as it collapses the
|
||||||
|
/// synchronous and asynchronous variants into a single type.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct StmtFunctionDef {
|
pub struct StmtFunctionDef {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
|
pub is_async: bool,
|
||||||
pub decorator_list: Vec<Decorator>,
|
pub decorator_list: Vec<Decorator>,
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
pub type_params: Option<TypeParams>,
|
pub type_params: Option<TypeParams>,
|
||||||
|
@ -136,24 +135,6 @@ impl From<StmtFunctionDef> for Stmt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See also [AsyncFunctionDef](https://docs.python.org/3/library/ast.html#ast.AsyncFunctionDef)
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
|
||||||
pub struct StmtAsyncFunctionDef {
|
|
||||||
pub range: TextRange,
|
|
||||||
pub decorator_list: Vec<Decorator>,
|
|
||||||
pub name: Identifier,
|
|
||||||
pub type_params: Option<TypeParams>,
|
|
||||||
pub parameters: Box<Parameters>,
|
|
||||||
pub returns: Option<Box<Expr>>,
|
|
||||||
pub body: Vec<Stmt>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<StmtAsyncFunctionDef> for Stmt {
|
|
||||||
fn from(payload: StmtAsyncFunctionDef) -> Self {
|
|
||||||
Stmt::AsyncFunctionDef(payload)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// See also [ClassDef](https://docs.python.org/3/library/ast.html#ast.ClassDef)
|
/// See also [ClassDef](https://docs.python.org/3/library/ast.html#ast.ClassDef)
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct StmtClassDef {
|
pub struct StmtClassDef {
|
||||||
|
@ -275,10 +256,15 @@ impl From<StmtAnnAssign> for Stmt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See also [For](https://docs.python.org/3/library/ast.html#ast.For)
|
/// See also [For](https://docs.python.org/3/library/ast.html#ast.For) and
|
||||||
|
/// [AsyncFor](https://docs.python.org/3/library/ast.html#ast.AsyncFor).
|
||||||
|
///
|
||||||
|
/// This type differs from the original Python AST, as it collapses the
|
||||||
|
/// synchronous and asynchronous variants into a single type.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct StmtFor {
|
pub struct StmtFor {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
|
pub is_async: bool,
|
||||||
pub target: Box<Expr>,
|
pub target: Box<Expr>,
|
||||||
pub iter: Box<Expr>,
|
pub iter: Box<Expr>,
|
||||||
pub body: Vec<Stmt>,
|
pub body: Vec<Stmt>,
|
||||||
|
@ -291,23 +277,8 @@ impl From<StmtFor> for Stmt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See also [AsyncFor](https://docs.python.org/3/library/ast.html#ast.AsyncFor)
|
/// See also [While](https://docs.python.org/3/library/ast.html#ast.While) and
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
/// [AsyncWhile](https://docs.python.org/3/library/ast.html#ast.AsyncWhile).
|
||||||
pub struct StmtAsyncFor {
|
|
||||||
pub range: TextRange,
|
|
||||||
pub target: Box<Expr>,
|
|
||||||
pub iter: Box<Expr>,
|
|
||||||
pub body: Vec<Stmt>,
|
|
||||||
pub orelse: Vec<Stmt>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<StmtAsyncFor> for Stmt {
|
|
||||||
fn from(payload: StmtAsyncFor) -> Self {
|
|
||||||
Stmt::AsyncFor(payload)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// See also [While](https://docs.python.org/3/library/ast.html#ast.While)
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct StmtWhile {
|
pub struct StmtWhile {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
|
@ -344,10 +315,15 @@ pub struct ElifElseClause {
|
||||||
pub body: Vec<Stmt>,
|
pub body: Vec<Stmt>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With)
|
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With) and
|
||||||
|
/// [AsyncWith](https://docs.python.org/3/library/ast.html#ast.AsyncWith).
|
||||||
|
///
|
||||||
|
/// This type differs from the original Python AST, as it collapses the
|
||||||
|
/// synchronous and asynchronous variants into a single type.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct StmtWith {
|
pub struct StmtWith {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
|
pub is_async: bool,
|
||||||
pub items: Vec<WithItem>,
|
pub items: Vec<WithItem>,
|
||||||
pub body: Vec<Stmt>,
|
pub body: Vec<Stmt>,
|
||||||
}
|
}
|
||||||
|
@ -358,20 +334,6 @@ impl From<StmtWith> for Stmt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See also [AsyncWith](https://docs.python.org/3/library/ast.html#ast.AsyncWith)
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
|
||||||
pub struct StmtAsyncWith {
|
|
||||||
pub range: TextRange,
|
|
||||||
pub items: Vec<WithItem>,
|
|
||||||
pub body: Vec<Stmt>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<StmtAsyncWith> for Stmt {
|
|
||||||
fn from(payload: StmtAsyncWith) -> Self {
|
|
||||||
Stmt::AsyncWith(payload)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// See also [Match](https://docs.python.org/3/library/ast.html#ast.Match)
|
/// See also [Match](https://docs.python.org/3/library/ast.html#ast.Match)
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct StmtMatch {
|
pub struct StmtMatch {
|
||||||
|
@ -2599,11 +2561,6 @@ impl Ranged for crate::nodes::StmtFunctionDef {
|
||||||
self.range
|
self.range
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Ranged for crate::nodes::StmtAsyncFunctionDef {
|
|
||||||
fn range(&self) -> TextRange {
|
|
||||||
self.range
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Ranged for crate::nodes::StmtClassDef {
|
impl Ranged for crate::nodes::StmtClassDef {
|
||||||
fn range(&self) -> TextRange {
|
fn range(&self) -> TextRange {
|
||||||
self.range
|
self.range
|
||||||
|
@ -2644,11 +2601,6 @@ impl Ranged for crate::nodes::StmtFor {
|
||||||
self.range
|
self.range
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Ranged for crate::nodes::StmtAsyncFor {
|
|
||||||
fn range(&self) -> TextRange {
|
|
||||||
self.range
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Ranged for crate::nodes::StmtWhile {
|
impl Ranged for crate::nodes::StmtWhile {
|
||||||
fn range(&self) -> TextRange {
|
fn range(&self) -> TextRange {
|
||||||
self.range
|
self.range
|
||||||
|
@ -2669,11 +2621,6 @@ impl Ranged for crate::nodes::StmtWith {
|
||||||
self.range
|
self.range
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Ranged for crate::nodes::StmtAsyncWith {
|
|
||||||
fn range(&self) -> TextRange {
|
|
||||||
self.range
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Ranged for crate::nodes::StmtMatch {
|
impl Ranged for crate::nodes::StmtMatch {
|
||||||
fn range(&self) -> TextRange {
|
fn range(&self) -> TextRange {
|
||||||
self.range
|
self.range
|
||||||
|
@ -2748,7 +2695,6 @@ impl Ranged for crate::Stmt {
|
||||||
fn range(&self) -> TextRange {
|
fn range(&self) -> TextRange {
|
||||||
match self {
|
match self {
|
||||||
Self::FunctionDef(node) => node.range(),
|
Self::FunctionDef(node) => node.range(),
|
||||||
Self::AsyncFunctionDef(node) => node.range(),
|
|
||||||
Self::ClassDef(node) => node.range(),
|
Self::ClassDef(node) => node.range(),
|
||||||
Self::Return(node) => node.range(),
|
Self::Return(node) => node.range(),
|
||||||
Self::Delete(node) => node.range(),
|
Self::Delete(node) => node.range(),
|
||||||
|
@ -2757,11 +2703,9 @@ impl Ranged for crate::Stmt {
|
||||||
Self::AugAssign(node) => node.range(),
|
Self::AugAssign(node) => node.range(),
|
||||||
Self::AnnAssign(node) => node.range(),
|
Self::AnnAssign(node) => node.range(),
|
||||||
Self::For(node) => node.range(),
|
Self::For(node) => node.range(),
|
||||||
Self::AsyncFor(node) => node.range(),
|
|
||||||
Self::While(node) => node.range(),
|
Self::While(node) => node.range(),
|
||||||
Self::If(node) => node.range(),
|
Self::If(node) => node.range(),
|
||||||
Self::With(node) => node.range(),
|
Self::With(node) => node.range(),
|
||||||
Self::AsyncWith(node) => node.range(),
|
|
||||||
Self::Match(node) => node.range(),
|
Self::Match(node) => node.range(),
|
||||||
Self::Raise(node) => node.range(),
|
Self::Raise(node) => node.range(),
|
||||||
Self::Try(node) => node.range(),
|
Self::Try(node) => node.range(),
|
||||||
|
@ -3110,8 +3054,7 @@ mod size_assertions {
|
||||||
use static_assertions::assert_eq_size;
|
use static_assertions::assert_eq_size;
|
||||||
|
|
||||||
assert_eq_size!(Stmt, [u8; 144]);
|
assert_eq_size!(Stmt, [u8; 144]);
|
||||||
assert_eq_size!(StmtFunctionDef, [u8; 136]);
|
assert_eq_size!(StmtFunctionDef, [u8; 144]);
|
||||||
assert_eq_size!(StmtAsyncFunctionDef, [u8; 136]);
|
|
||||||
assert_eq_size!(StmtClassDef, [u8; 104]);
|
assert_eq_size!(StmtClassDef, [u8; 104]);
|
||||||
assert_eq_size!(StmtTry, [u8; 104]);
|
assert_eq_size!(StmtTry, [u8; 104]);
|
||||||
assert_eq_size!(Expr, [u8; 80]);
|
assert_eq_size!(Expr, [u8; 80]);
|
||||||
|
|
|
@ -32,9 +32,6 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
}
|
}
|
||||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => {
|
|
||||||
visitor.visit_body(body);
|
|
||||||
}
|
|
||||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
visitor.visit_body(orelse);
|
visitor.visit_body(orelse);
|
||||||
|
@ -42,10 +39,6 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
||||||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
}
|
}
|
||||||
Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
|
||||||
visitor.visit_body(body);
|
|
||||||
visitor.visit_body(orelse);
|
|
||||||
}
|
|
||||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
visitor.visit_body(orelse);
|
visitor.visit_body(orelse);
|
||||||
|
@ -63,9 +56,6 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
||||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
}
|
}
|
||||||
Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
|
||||||
visitor.visit_body(body);
|
|
||||||
}
|
|
||||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||||
for match_case in cases {
|
for match_case in cases {
|
||||||
visitor.visit_match_case(match_case);
|
visitor.visit_match_case(match_case);
|
||||||
|
|
|
@ -5,7 +5,6 @@ use crate::{self as ast, ExceptHandler, Stmt, Suite};
|
||||||
pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> {
|
pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> {
|
||||||
match parent {
|
match parent {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => Some(body),
|
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => Some(body),
|
||||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => Some(body),
|
|
||||||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => Some(body),
|
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => Some(body),
|
||||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||||
if body.contains(stmt) {
|
if body.contains(stmt) {
|
||||||
|
@ -16,15 +15,6 @@ pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
|
||||||
if body.contains(stmt) {
|
|
||||||
Some(body)
|
|
||||||
} else if orelse.contains(stmt) {
|
|
||||||
Some(orelse)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
if body.contains(stmt) {
|
if body.contains(stmt) {
|
||||||
Some(body)
|
Some(body)
|
||||||
|
@ -49,7 +39,6 @@ pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::With(ast::StmtWith { body, .. }) => Some(body),
|
Stmt::With(ast::StmtWith { body, .. }) => Some(body),
|
||||||
Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => Some(body),
|
|
||||||
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
|
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
|
||||||
.iter()
|
.iter()
|
||||||
.map(|case| &case.body)
|
.map(|case| &case.body)
|
||||||
|
|
|
@ -128,26 +128,6 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
||||||
}
|
}
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
}
|
}
|
||||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
parameters,
|
|
||||||
body,
|
|
||||||
decorator_list,
|
|
||||||
returns,
|
|
||||||
type_params,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
for decorator in decorator_list {
|
|
||||||
visitor.visit_decorator(decorator);
|
|
||||||
}
|
|
||||||
if let Some(type_params) = type_params {
|
|
||||||
visitor.visit_type_params(type_params);
|
|
||||||
}
|
|
||||||
visitor.visit_parameters(parameters);
|
|
||||||
for expr in returns {
|
|
||||||
visitor.visit_annotation(expr);
|
|
||||||
}
|
|
||||||
visitor.visit_body(body);
|
|
||||||
}
|
|
||||||
Stmt::ClassDef(ast::StmtClassDef {
|
Stmt::ClassDef(ast::StmtClassDef {
|
||||||
arguments,
|
arguments,
|
||||||
body,
|
body,
|
||||||
|
@ -228,18 +208,6 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
visitor.visit_body(orelse);
|
visitor.visit_body(orelse);
|
||||||
}
|
}
|
||||||
Stmt::AsyncFor(ast::StmtAsyncFor {
|
|
||||||
target,
|
|
||||||
iter,
|
|
||||||
body,
|
|
||||||
orelse,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
visitor.visit_expr(iter);
|
|
||||||
visitor.visit_expr(target);
|
|
||||||
visitor.visit_body(body);
|
|
||||||
visitor.visit_body(orelse);
|
|
||||||
}
|
|
||||||
Stmt::While(ast::StmtWhile {
|
Stmt::While(ast::StmtWhile {
|
||||||
test,
|
test,
|
||||||
body,
|
body,
|
||||||
|
@ -271,12 +239,6 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
||||||
}
|
}
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
}
|
}
|
||||||
Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
|
||||||
for with_item in items {
|
|
||||||
visitor.visit_with_item(with_item);
|
|
||||||
}
|
|
||||||
visitor.visit_body(body);
|
|
||||||
}
|
|
||||||
Stmt::Match(ast::StmtMatch {
|
Stmt::Match(ast::StmtMatch {
|
||||||
subject,
|
subject,
|
||||||
cases,
|
cases,
|
||||||
|
|
|
@ -145,14 +145,6 @@ where
|
||||||
returns,
|
returns,
|
||||||
type_params,
|
type_params,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
parameters,
|
|
||||||
body,
|
|
||||||
decorator_list,
|
|
||||||
returns,
|
|
||||||
type_params,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
for decorator in decorator_list {
|
for decorator in decorator_list {
|
||||||
visitor.visit_decorator(decorator);
|
visitor.visit_decorator(decorator);
|
||||||
|
@ -261,13 +253,6 @@ where
|
||||||
body,
|
body,
|
||||||
orelse,
|
orelse,
|
||||||
..
|
..
|
||||||
})
|
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
|
||||||
target,
|
|
||||||
iter,
|
|
||||||
body,
|
|
||||||
orelse,
|
|
||||||
..
|
|
||||||
}) => {
|
}) => {
|
||||||
visitor.visit_expr(target);
|
visitor.visit_expr(target);
|
||||||
visitor.visit_expr(iter);
|
visitor.visit_expr(iter);
|
||||||
|
@ -302,11 +287,7 @@ where
|
||||||
Stmt::With(ast::StmtWith {
|
Stmt::With(ast::StmtWith {
|
||||||
items,
|
items,
|
||||||
body,
|
body,
|
||||||
range: _,
|
is_async: _,
|
||||||
})
|
|
||||||
| Stmt::AsyncWith(ast::StmtAsyncWith {
|
|
||||||
items,
|
|
||||||
body,
|
|
||||||
range: _,
|
range: _,
|
||||||
}) => {
|
}) => {
|
||||||
for with_item in items {
|
for with_item in items {
|
||||||
|
|
|
@ -23,7 +23,6 @@ mod precedence {
|
||||||
pub(crate) const YIELD_FROM: u8 = 7;
|
pub(crate) const YIELD_FROM: u8 = 7;
|
||||||
pub(crate) const IF: u8 = 9;
|
pub(crate) const IF: u8 = 9;
|
||||||
pub(crate) const FOR: u8 = 9;
|
pub(crate) const FOR: u8 = 9;
|
||||||
pub(crate) const ASYNC_FOR: u8 = 9;
|
|
||||||
pub(crate) const WHILE: u8 = 9;
|
pub(crate) const WHILE: u8 = 9;
|
||||||
pub(crate) const RETURN: u8 = 11;
|
pub(crate) const RETURN: u8 = 11;
|
||||||
pub(crate) const SLICE: u8 = 13;
|
pub(crate) const SLICE: u8 = 13;
|
||||||
|
@ -204,6 +203,7 @@ impl<'a> Generator<'a> {
|
||||||
|
|
||||||
match ast {
|
match ast {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
|
is_async,
|
||||||
name,
|
name,
|
||||||
parameters,
|
parameters,
|
||||||
body,
|
body,
|
||||||
|
@ -220,6 +220,9 @@ impl<'a> Generator<'a> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
statement!({
|
statement!({
|
||||||
|
if *is_async {
|
||||||
|
self.p("async ");
|
||||||
|
}
|
||||||
self.p("def ");
|
self.p("def ");
|
||||||
self.p_id(name);
|
self.p_id(name);
|
||||||
if let Some(type_params) = type_params {
|
if let Some(type_params) = type_params {
|
||||||
|
@ -239,42 +242,6 @@ impl<'a> Generator<'a> {
|
||||||
self.newlines(2);
|
self.newlines(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
|
||||||
name,
|
|
||||||
parameters,
|
|
||||||
body,
|
|
||||||
returns,
|
|
||||||
decorator_list,
|
|
||||||
type_params,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
|
||||||
for decorator in decorator_list {
|
|
||||||
statement!({
|
|
||||||
self.p("@");
|
|
||||||
self.unparse_expr(&decorator.expression, precedence::MAX);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
statement!({
|
|
||||||
self.p("async def ");
|
|
||||||
self.p_id(name);
|
|
||||||
if let Some(type_params) = type_params {
|
|
||||||
self.unparse_type_params(type_params);
|
|
||||||
}
|
|
||||||
self.p("(");
|
|
||||||
self.unparse_parameters(parameters);
|
|
||||||
self.p(")");
|
|
||||||
if let Some(returns) = returns {
|
|
||||||
self.p(" -> ");
|
|
||||||
self.unparse_expr(returns, precedence::MAX);
|
|
||||||
}
|
|
||||||
self.p(":");
|
|
||||||
});
|
|
||||||
self.body(body);
|
|
||||||
if self.indent_depth == 0 {
|
|
||||||
self.newlines(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Stmt::ClassDef(ast::StmtClassDef {
|
Stmt::ClassDef(ast::StmtClassDef {
|
||||||
name,
|
name,
|
||||||
arguments,
|
arguments,
|
||||||
|
@ -400,6 +367,7 @@ impl<'a> Generator<'a> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Stmt::For(ast::StmtFor {
|
Stmt::For(ast::StmtFor {
|
||||||
|
is_async,
|
||||||
target,
|
target,
|
||||||
iter,
|
iter,
|
||||||
body,
|
body,
|
||||||
|
@ -407,6 +375,9 @@ impl<'a> Generator<'a> {
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
statement!({
|
statement!({
|
||||||
|
if *is_async {
|
||||||
|
self.p("async ");
|
||||||
|
}
|
||||||
self.p("for ");
|
self.p("for ");
|
||||||
self.unparse_expr(target, precedence::FOR);
|
self.unparse_expr(target, precedence::FOR);
|
||||||
self.p(" in ");
|
self.p(" in ");
|
||||||
|
@ -421,28 +392,6 @@ impl<'a> Generator<'a> {
|
||||||
self.body(orelse);
|
self.body(orelse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::AsyncFor(ast::StmtAsyncFor {
|
|
||||||
target,
|
|
||||||
iter,
|
|
||||||
body,
|
|
||||||
orelse,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
statement!({
|
|
||||||
self.p("async for ");
|
|
||||||
self.unparse_expr(target, precedence::ASYNC_FOR);
|
|
||||||
self.p(" in ");
|
|
||||||
self.unparse_expr(iter, precedence::MAX);
|
|
||||||
self.p(":");
|
|
||||||
});
|
|
||||||
self.body(body);
|
|
||||||
if !orelse.is_empty() {
|
|
||||||
statement!({
|
|
||||||
self.p("else:");
|
|
||||||
});
|
|
||||||
self.body(orelse);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Stmt::While(ast::StmtWhile {
|
Stmt::While(ast::StmtWhile {
|
||||||
test,
|
test,
|
||||||
body,
|
body,
|
||||||
|
@ -490,21 +439,17 @@ impl<'a> Generator<'a> {
|
||||||
self.body(&clause.body);
|
self.body(&clause.body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
Stmt::With(ast::StmtWith {
|
||||||
|
is_async,
|
||||||
|
items,
|
||||||
|
body,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
statement!({
|
statement!({
|
||||||
self.p("with ");
|
if *is_async {
|
||||||
let mut first = true;
|
self.p("async ");
|
||||||
for item in items {
|
|
||||||
self.p_delim(&mut first, ", ");
|
|
||||||
self.unparse_with_item(item);
|
|
||||||
}
|
}
|
||||||
self.p(":");
|
self.p("with ");
|
||||||
});
|
|
||||||
self.body(body);
|
|
||||||
}
|
|
||||||
Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
|
||||||
statement!({
|
|
||||||
self.p("async with ");
|
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for item in items {
|
for item in items {
|
||||||
self.p_delim(&mut first, ", ");
|
self.p_delim(&mut first, ", ");
|
||||||
|
|
|
@ -67,9 +67,7 @@ pub(super) fn place_comment<'a>(
|
||||||
handle_module_level_own_line_comment_before_class_or_function_comment(comment, locator)
|
handle_module_level_own_line_comment_before_class_or_function_comment(comment, locator)
|
||||||
}
|
}
|
||||||
AnyNodeRef::WithItem(_) => handle_with_item_comment(comment, locator),
|
AnyNodeRef::WithItem(_) => handle_with_item_comment(comment, locator),
|
||||||
AnyNodeRef::StmtFunctionDef(_) | AnyNodeRef::StmtAsyncFunctionDef(_) => {
|
AnyNodeRef::StmtFunctionDef(_) => handle_leading_function_with_decorators_comment(comment),
|
||||||
handle_leading_function_with_decorators_comment(comment)
|
|
||||||
}
|
|
||||||
AnyNodeRef::StmtClassDef(class_def) => {
|
AnyNodeRef::StmtClassDef(class_def) => {
|
||||||
handle_leading_class_with_decorators_comment(comment, class_def)
|
handle_leading_class_with_decorators_comment(comment, class_def)
|
||||||
}
|
}
|
||||||
|
@ -178,7 +176,6 @@ fn handle_end_of_line_comment_around_body<'a>(
|
||||||
fn is_first_statement_in_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bool {
|
fn is_first_statement_in_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bool {
|
||||||
match has_body {
|
match has_body {
|
||||||
AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. })
|
AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. })
|
||||||
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
|
||||||
| AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => {
|
| AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
are_same_optional(statement, body.first())
|
are_same_optional(statement, body.first())
|
||||||
|| are_same_optional(statement, orelse.first())
|
|| are_same_optional(statement, orelse.first())
|
||||||
|
@ -208,7 +205,6 @@ fn is_first_statement_in_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bo
|
||||||
body, ..
|
body, ..
|
||||||
})
|
})
|
||||||
| AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. })
|
| AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. })
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
|
||||||
| AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) => {
|
| AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) => {
|
||||||
are_same_optional(statement, body.first())
|
are_same_optional(statement, body.first())
|
||||||
}
|
}
|
||||||
|
@ -773,9 +769,7 @@ fn handle_module_level_own_line_comment_before_class_or_function_comment<'a>(
|
||||||
// ... where the following is a function or class statement.
|
// ... where the following is a function or class statement.
|
||||||
if !matches!(
|
if !matches!(
|
||||||
following,
|
following,
|
||||||
AnyNodeRef::StmtAsyncFunctionDef(_)
|
AnyNodeRef::StmtFunctionDef(_) | AnyNodeRef::StmtClassDef(_)
|
||||||
| AnyNodeRef::StmtFunctionDef(_)
|
|
||||||
| AnyNodeRef::StmtClassDef(_)
|
|
||||||
) {
|
) {
|
||||||
return CommentPlacement::Default(comment);
|
return CommentPlacement::Default(comment);
|
||||||
}
|
}
|
||||||
|
@ -1408,10 +1402,8 @@ where
|
||||||
fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
||||||
let body = match node {
|
let body = match node {
|
||||||
AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. })
|
AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. })
|
||||||
| AnyNodeRef::StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
|
||||||
| AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. })
|
| AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. })
|
||||||
| AnyNodeRef::StmtWith(ast::StmtWith { body, .. })
|
| AnyNodeRef::StmtWith(ast::StmtWith { body, .. })
|
||||||
| AnyNodeRef::StmtAsyncWith(ast::StmtAsyncWith { body, .. })
|
|
||||||
| AnyNodeRef::MatchCase(MatchCase { body, .. })
|
| AnyNodeRef::MatchCase(MatchCase { body, .. })
|
||||||
| AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler {
|
| AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||||
body, ..
|
body, ..
|
||||||
|
@ -1424,7 +1416,6 @@ fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
||||||
}) => elif_else_clauses.last().map_or(body, |clause| &clause.body),
|
}) => elif_else_clauses.last().map_or(body, |clause| &clause.body),
|
||||||
|
|
||||||
AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. })
|
AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. })
|
||||||
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
|
||||||
| AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => {
|
| AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => {
|
||||||
if orelse.is_empty() {
|
if orelse.is_empty() {
|
||||||
body
|
body
|
||||||
|
@ -1477,7 +1468,6 @@ fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
||||||
fn is_first_statement_in_alternate_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bool {
|
fn is_first_statement_in_alternate_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bool {
|
||||||
match has_body {
|
match has_body {
|
||||||
AnyNodeRef::StmtFor(ast::StmtFor { orelse, .. })
|
AnyNodeRef::StmtFor(ast::StmtFor { orelse, .. })
|
||||||
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { orelse, .. })
|
|
||||||
| AnyNodeRef::StmtWhile(ast::StmtWhile { orelse, .. }) => {
|
| AnyNodeRef::StmtWhile(ast::StmtWhile { orelse, .. }) => {
|
||||||
are_same_optional(statement, orelse.first())
|
are_same_optional(statement, orelse.first())
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,6 @@ impl NeedsParentheses for ExprNamedExpr {
|
||||||
|| parent.is_with_item()
|
|| parent.is_with_item()
|
||||||
|| parent.is_stmt_delete()
|
|| parent.is_stmt_delete()
|
||||||
|| parent.is_stmt_for()
|
|| parent.is_stmt_for()
|
||||||
|| parent.is_stmt_async_for()
|
|
||||||
{
|
{
|
||||||
OptionalParentheses::Always
|
OptionalParentheses::Always
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -108,42 +108,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtFunctionDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormatRule<ast::StmtAsyncFunctionDef, PyFormatContext<'_>>
|
|
||||||
for crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn fmt(&self, node: &ast::StmtAsyncFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
FormatNodeRule::<ast::StmtAsyncFunctionDef>::fmt(self, node, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtAsyncFunctionDef {
|
|
||||||
type Format<'a> = FormatRefWithRule<
|
|
||||||
'a,
|
|
||||||
ast::StmtAsyncFunctionDef,
|
|
||||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn format(&self) -> Self::Format<'_> {
|
|
||||||
FormatRefWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtAsyncFunctionDef {
|
|
||||||
type Format = FormatOwnedWithRule<
|
|
||||||
ast::StmtAsyncFunctionDef,
|
|
||||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn into_format(self) -> Self::Format {
|
|
||||||
FormatOwnedWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FormatRule<ast::StmtClassDef, PyFormatContext<'_>>
|
impl FormatRule<ast::StmtClassDef, PyFormatContext<'_>>
|
||||||
for crate::statement::stmt_class_def::FormatStmtClassDef
|
for crate::statement::stmt_class_def::FormatStmtClassDef
|
||||||
{
|
{
|
||||||
|
@ -424,42 +388,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtFor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormatRule<ast::StmtAsyncFor, PyFormatContext<'_>>
|
|
||||||
for crate::statement::stmt_async_for::FormatStmtAsyncFor
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn fmt(&self, node: &ast::StmtAsyncFor, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
FormatNodeRule::<ast::StmtAsyncFor>::fmt(self, node, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtAsyncFor {
|
|
||||||
type Format<'a> = FormatRefWithRule<
|
|
||||||
'a,
|
|
||||||
ast::StmtAsyncFor,
|
|
||||||
crate::statement::stmt_async_for::FormatStmtAsyncFor,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn format(&self) -> Self::Format<'_> {
|
|
||||||
FormatRefWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_async_for::FormatStmtAsyncFor::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtAsyncFor {
|
|
||||||
type Format = FormatOwnedWithRule<
|
|
||||||
ast::StmtAsyncFor,
|
|
||||||
crate::statement::stmt_async_for::FormatStmtAsyncFor,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn into_format(self) -> Self::Format {
|
|
||||||
FormatOwnedWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_async_for::FormatStmtAsyncFor::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FormatRule<ast::StmtWhile, PyFormatContext<'_>>
|
impl FormatRule<ast::StmtWhile, PyFormatContext<'_>>
|
||||||
for crate::statement::stmt_while::FormatStmtWhile
|
for crate::statement::stmt_while::FormatStmtWhile
|
||||||
{
|
{
|
||||||
|
@ -554,42 +482,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtWith {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormatRule<ast::StmtAsyncWith, PyFormatContext<'_>>
|
|
||||||
for crate::statement::stmt_async_with::FormatStmtAsyncWith
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn fmt(&self, node: &ast::StmtAsyncWith, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
FormatNodeRule::<ast::StmtAsyncWith>::fmt(self, node, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtAsyncWith {
|
|
||||||
type Format<'a> = FormatRefWithRule<
|
|
||||||
'a,
|
|
||||||
ast::StmtAsyncWith,
|
|
||||||
crate::statement::stmt_async_with::FormatStmtAsyncWith,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn format(&self) -> Self::Format<'_> {
|
|
||||||
FormatRefWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_async_with::FormatStmtAsyncWith::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtAsyncWith {
|
|
||||||
type Format = FormatOwnedWithRule<
|
|
||||||
ast::StmtAsyncWith,
|
|
||||||
crate::statement::stmt_async_with::FormatStmtAsyncWith,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn into_format(self) -> Self::Format {
|
|
||||||
FormatOwnedWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_async_with::FormatStmtAsyncWith::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FormatRule<ast::StmtMatch, PyFormatContext<'_>>
|
impl FormatRule<ast::StmtMatch, PyFormatContext<'_>>
|
||||||
for crate::statement::stmt_match::FormatStmtMatch
|
for crate::statement::stmt_match::FormatStmtMatch
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,9 +5,6 @@ use ruff_python_ast::Stmt;
|
||||||
pub(crate) mod stmt_ann_assign;
|
pub(crate) mod stmt_ann_assign;
|
||||||
pub(crate) mod stmt_assert;
|
pub(crate) mod stmt_assert;
|
||||||
pub(crate) mod stmt_assign;
|
pub(crate) mod stmt_assign;
|
||||||
pub(crate) mod stmt_async_for;
|
|
||||||
pub(crate) mod stmt_async_function_def;
|
|
||||||
pub(crate) mod stmt_async_with;
|
|
||||||
pub(crate) mod stmt_aug_assign;
|
pub(crate) mod stmt_aug_assign;
|
||||||
pub(crate) mod stmt_break;
|
pub(crate) mod stmt_break;
|
||||||
pub(crate) mod stmt_class_def;
|
pub(crate) mod stmt_class_def;
|
||||||
|
@ -40,7 +37,6 @@ impl FormatRule<Stmt, PyFormatContext<'_>> for FormatStmt {
|
||||||
fn fmt(&self, item: &Stmt, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt(&self, item: &Stmt, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
match item {
|
match item {
|
||||||
Stmt::FunctionDef(x) => x.format().fmt(f),
|
Stmt::FunctionDef(x) => x.format().fmt(f),
|
||||||
Stmt::AsyncFunctionDef(x) => x.format().fmt(f),
|
|
||||||
Stmt::ClassDef(x) => x.format().fmt(f),
|
Stmt::ClassDef(x) => x.format().fmt(f),
|
||||||
Stmt::Return(x) => x.format().fmt(f),
|
Stmt::Return(x) => x.format().fmt(f),
|
||||||
Stmt::Delete(x) => x.format().fmt(f),
|
Stmt::Delete(x) => x.format().fmt(f),
|
||||||
|
@ -48,11 +44,9 @@ impl FormatRule<Stmt, PyFormatContext<'_>> for FormatStmt {
|
||||||
Stmt::AugAssign(x) => x.format().fmt(f),
|
Stmt::AugAssign(x) => x.format().fmt(f),
|
||||||
Stmt::AnnAssign(x) => x.format().fmt(f),
|
Stmt::AnnAssign(x) => x.format().fmt(f),
|
||||||
Stmt::For(x) => x.format().fmt(f),
|
Stmt::For(x) => x.format().fmt(f),
|
||||||
Stmt::AsyncFor(x) => x.format().fmt(f),
|
|
||||||
Stmt::While(x) => x.format().fmt(f),
|
Stmt::While(x) => x.format().fmt(f),
|
||||||
Stmt::If(x) => x.format().fmt(f),
|
Stmt::If(x) => x.format().fmt(f),
|
||||||
Stmt::With(x) => x.format().fmt(f),
|
Stmt::With(x) => x.format().fmt(f),
|
||||||
Stmt::AsyncWith(x) => x.format().fmt(f),
|
|
||||||
Stmt::Match(x) => x.format().fmt(f),
|
Stmt::Match(x) => x.format().fmt(f),
|
||||||
Stmt::Raise(x) => x.format().fmt(f),
|
Stmt::Raise(x) => x.format().fmt(f),
|
||||||
Stmt::Try(x) => x.format().fmt(f),
|
Stmt::Try(x) => x.format().fmt(f),
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
use crate::prelude::*;
|
|
||||||
use crate::statement::stmt_for::AnyStatementFor;
|
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
|
||||||
use ruff_formatter::FormatResult;
|
|
||||||
use ruff_python_ast::StmtAsyncFor;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct FormatStmtAsyncFor;
|
|
||||||
|
|
||||||
impl FormatNodeRule<StmtAsyncFor> for FormatStmtAsyncFor {
|
|
||||||
fn fmt_fields(&self, item: &StmtAsyncFor, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
AnyStatementFor::from(item).fmt(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_dangling_comments(
|
|
||||||
&self,
|
|
||||||
_node: &StmtAsyncFor,
|
|
||||||
_f: &mut PyFormatter,
|
|
||||||
) -> FormatResult<()> {
|
|
||||||
// Handled in `fmt_fields`
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
use ruff_python_ast::StmtAsyncFunctionDef;
|
|
||||||
|
|
||||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
|
||||||
|
|
||||||
use crate::prelude::*;
|
|
||||||
use crate::FormatNodeRule;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct FormatStmtAsyncFunctionDef;
|
|
||||||
|
|
||||||
impl FormatNodeRule<StmtAsyncFunctionDef> for FormatStmtAsyncFunctionDef {
|
|
||||||
fn fmt_fields(&self, item: &StmtAsyncFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
AnyFunctionDefinition::from(item).format().fmt(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_dangling_comments(
|
|
||||||
&self,
|
|
||||||
_node: &StmtAsyncFunctionDef,
|
|
||||||
_f: &mut PyFormatter,
|
|
||||||
) -> FormatResult<()> {
|
|
||||||
// Handled by `AnyFunctionDef`
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
use crate::prelude::*;
|
|
||||||
use crate::statement::stmt_with::AnyStatementWith;
|
|
||||||
use crate::FormatNodeRule;
|
|
||||||
use ruff_python_ast::StmtAsyncWith;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct FormatStmtAsyncWith;
|
|
||||||
|
|
||||||
impl FormatNodeRule<StmtAsyncWith> for FormatStmtAsyncWith {
|
|
||||||
fn fmt_fields(&self, item: &StmtAsyncWith, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
AnyStatementWith::from(item).fmt(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_dangling_comments(
|
|
||||||
&self,
|
|
||||||
_node: &StmtAsyncWith,
|
|
||||||
_f: &mut PyFormatter,
|
|
||||||
) -> FormatResult<()> {
|
|
||||||
// Handled in `fmt_fields`
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +1,12 @@
|
||||||
|
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||||
|
use ruff_python_ast::{Expr, Ranged, Stmt, StmtFor};
|
||||||
|
|
||||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||||
use crate::expression::expr_tuple::TupleParentheses;
|
use crate::expression::expr_tuple::TupleParentheses;
|
||||||
use crate::expression::maybe_parenthesize_expression;
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::{FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
|
||||||
use ruff_python_ast::{Expr, Ranged, Stmt, StmtAsyncFor, StmtFor, Suite};
|
|
||||||
use ruff_text_size::TextRange;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct ExprTupleWithoutParentheses<'a>(&'a Expr);
|
struct ExprTupleWithoutParentheses<'a>(&'a Expr);
|
||||||
|
@ -27,85 +26,19 @@ impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatStmtFor;
|
pub struct FormatStmtFor;
|
||||||
|
|
||||||
pub(super) enum AnyStatementFor<'a> {
|
impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
||||||
For(&'a StmtFor),
|
fn fmt_fields(&self, item: &StmtFor, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
AsyncFor(&'a StmtAsyncFor),
|
let StmtFor {
|
||||||
}
|
is_async,
|
||||||
|
target,
|
||||||
impl<'a> AnyStatementFor<'a> {
|
iter,
|
||||||
const fn is_async(&self) -> bool {
|
body,
|
||||||
matches!(self, AnyStatementFor::AsyncFor(_))
|
orelse,
|
||||||
}
|
range: _,
|
||||||
|
} = item;
|
||||||
fn target(&self) -> &Expr {
|
|
||||||
match self {
|
|
||||||
AnyStatementFor::For(stmt) => &stmt.target,
|
|
||||||
AnyStatementFor::AsyncFor(stmt) => &stmt.target,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::iter_not_returning_iterator)]
|
|
||||||
fn iter(&self) -> &Expr {
|
|
||||||
match self {
|
|
||||||
AnyStatementFor::For(stmt) => &stmt.iter,
|
|
||||||
AnyStatementFor::AsyncFor(stmt) => &stmt.iter,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn body(&self) -> &Suite {
|
|
||||||
match self {
|
|
||||||
AnyStatementFor::For(stmt) => &stmt.body,
|
|
||||||
AnyStatementFor::AsyncFor(stmt) => &stmt.body,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn orelse(&self) -> &Suite {
|
|
||||||
match self {
|
|
||||||
AnyStatementFor::For(stmt) => &stmt.orelse,
|
|
||||||
AnyStatementFor::AsyncFor(stmt) => &stmt.orelse,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ranged for AnyStatementFor<'_> {
|
|
||||||
fn range(&self) -> TextRange {
|
|
||||||
match self {
|
|
||||||
AnyStatementFor::For(stmt) => stmt.range(),
|
|
||||||
AnyStatementFor::AsyncFor(stmt) => stmt.range(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a StmtFor> for AnyStatementFor<'a> {
|
|
||||||
fn from(value: &'a StmtFor) -> Self {
|
|
||||||
AnyStatementFor::For(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a StmtAsyncFor> for AnyStatementFor<'a> {
|
|
||||||
fn from(value: &'a StmtAsyncFor) -> Self {
|
|
||||||
AnyStatementFor::AsyncFor(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&AnyStatementFor<'a>> for AnyNodeRef<'a> {
|
|
||||||
fn from(value: &AnyStatementFor<'a>) -> Self {
|
|
||||||
match value {
|
|
||||||
AnyStatementFor::For(stmt) => AnyNodeRef::StmtFor(stmt),
|
|
||||||
AnyStatementFor::AsyncFor(stmt) => AnyNodeRef::StmtAsyncFor(stmt),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Format<PyFormatContext<'_>> for AnyStatementFor<'_> {
|
|
||||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
let target = self.target();
|
|
||||||
let iter = self.iter();
|
|
||||||
let body = self.body();
|
|
||||||
let orelse = self.orelse();
|
|
||||||
|
|
||||||
let comments = f.context().comments().clone();
|
let comments = f.context().comments().clone();
|
||||||
let dangling_comments = comments.dangling_comments(self);
|
let dangling_comments = comments.dangling_comments(item);
|
||||||
let body_start = body.first().map_or(iter.end(), Stmt::start);
|
let body_start = body.first().map_or(iter.end(), Stmt::start);
|
||||||
let or_else_comments_start =
|
let or_else_comments_start =
|
||||||
dangling_comments.partition_point(|comment| comment.slice().end() < body_start);
|
dangling_comments.partition_point(|comment| comment.slice().end() < body_start);
|
||||||
|
@ -116,15 +49,14 @@ impl Format<PyFormatContext<'_>> for AnyStatementFor<'_> {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[
|
[
|
||||||
self.is_async()
|
is_async.then_some(format_args![text("async"), space()]),
|
||||||
.then_some(format_args![text("async"), space()]),
|
|
||||||
text("for"),
|
text("for"),
|
||||||
space(),
|
space(),
|
||||||
ExprTupleWithoutParentheses(target),
|
ExprTupleWithoutParentheses(target),
|
||||||
space(),
|
space(),
|
||||||
text("in"),
|
text("in"),
|
||||||
space(),
|
space(),
|
||||||
maybe_parenthesize_expression(iter, self, Parenthesize::IfBreaks),
|
maybe_parenthesize_expression(iter, item, Parenthesize::IfBreaks),
|
||||||
text(":"),
|
text(":"),
|
||||||
trailing_comments(trailing_condition_comments),
|
trailing_comments(trailing_condition_comments),
|
||||||
block_indent(&body.format())
|
block_indent(&body.format())
|
||||||
|
@ -153,12 +85,6 @@ impl Format<PyFormatContext<'_>> for AnyStatementFor<'_> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
|
||||||
fn fmt_fields(&self, item: &StmtFor, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
AnyStatementFor::from(item).fmt(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_dangling_comments(&self, _node: &StmtFor, _f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_dangling_comments(&self, _node: &StmtFor, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
// Handled in `fmt_fields`
|
// Handled in `fmt_fields`
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
|
use ruff_formatter::write;
|
||||||
use ruff_python_ast::{Ranged, StmtFunctionDef};
|
use ruff_python_ast::{Ranged, StmtFunctionDef};
|
||||||
|
|
||||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
|
||||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
|
||||||
use ruff_python_trivia::lines_after_ignoring_trivia;
|
use ruff_python_trivia::lines_after_ignoring_trivia;
|
||||||
|
|
||||||
use crate::comments::{leading_comments, trailing_comments};
|
use crate::comments::{leading_comments, trailing_comments};
|
||||||
|
|
||||||
use crate::expression::parentheses::{optional_parentheses, Parentheses};
|
use crate::expression::parentheses::{optional_parentheses, Parentheses};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::statement::suite::SuiteKind;
|
use crate::statement::suite::SuiteKind;
|
||||||
|
@ -16,24 +13,6 @@ pub struct FormatStmtFunctionDef;
|
||||||
|
|
||||||
impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
|
impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
|
||||||
fn fmt_fields(&self, item: &StmtFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, item: &StmtFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
AnyFunctionDefinition::from(item).format().fmt(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_dangling_comments(
|
|
||||||
&self,
|
|
||||||
_node: &StmtFunctionDef,
|
|
||||||
_f: &mut PyFormatter,
|
|
||||||
) -> FormatResult<()> {
|
|
||||||
// Handled by `AnyFunctionDef`
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct FormatAnyFunctionDef;
|
|
||||||
|
|
||||||
impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFunctionDef {
|
|
||||||
fn fmt(&self, item: &AnyFunctionDefinition<'_>, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
let comments = f.context().comments().clone();
|
let comments = f.context().comments().clone();
|
||||||
|
|
||||||
let dangling_comments = comments.dangling_comments(item);
|
let dangling_comments = comments.dangling_comments(item);
|
||||||
|
@ -43,9 +22,9 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
||||||
let (leading_definition_comments, trailing_definition_comments) =
|
let (leading_definition_comments, trailing_definition_comments) =
|
||||||
dangling_comments.split_at(trailing_definition_comments_start);
|
dangling_comments.split_at(trailing_definition_comments_start);
|
||||||
|
|
||||||
if let Some(last_decorator) = item.decorators().last() {
|
if let Some(last_decorator) = item.decorator_list.last() {
|
||||||
f.join_with(hard_line_break())
|
f.join_with(hard_line_break())
|
||||||
.entries(item.decorators().iter().formatted())
|
.entries(item.decorator_list.iter().formatted())
|
||||||
.finish()?;
|
.finish()?;
|
||||||
|
|
||||||
if leading_definition_comments.is_empty() {
|
if leading_definition_comments.is_empty() {
|
||||||
|
@ -69,21 +48,19 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if item.is_async() {
|
if item.is_async {
|
||||||
write!(f, [text("async"), space()])?;
|
write!(f, [text("async"), space()])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = item.name();
|
write!(f, [text("def"), space(), item.name.format()])?;
|
||||||
|
|
||||||
write!(f, [text("def"), space(), name.format()])?;
|
if let Some(type_params) = item.type_params.as_ref() {
|
||||||
|
|
||||||
if let Some(type_params) = item.type_params() {
|
|
||||||
write!(f, [type_params.format()])?;
|
write!(f, [type_params.format()])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(f, [item.arguments().format()])?;
|
write!(f, [item.parameters.format()])?;
|
||||||
|
|
||||||
if let Some(return_annotation) = item.returns() {
|
if let Some(return_annotation) = item.returns.as_ref() {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[
|
[
|
||||||
|
@ -102,33 +79,17 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
||||||
[
|
[
|
||||||
text(":"),
|
text(":"),
|
||||||
trailing_comments(trailing_definition_comments),
|
trailing_comments(trailing_definition_comments),
|
||||||
block_indent(&item.body().format().with_options(SuiteKind::Function))
|
block_indent(&item.body.format().with_options(SuiteKind::Function))
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<'def, 'ast> AsFormat<PyFormatContext<'ast>> for AnyFunctionDefinition<'def> {
|
fn fmt_dangling_comments(
|
||||||
type Format<'a> = FormatRefWithRule<
|
&self,
|
||||||
'a,
|
_node: &StmtFunctionDef,
|
||||||
AnyFunctionDefinition<'def>,
|
_f: &mut PyFormatter,
|
||||||
FormatAnyFunctionDef,
|
) -> FormatResult<()> {
|
||||||
PyFormatContext<'ast>,
|
// Handled in `fmt_fields`
|
||||||
> where Self: 'a;
|
Ok(())
|
||||||
|
|
||||||
fn format(&self) -> Self::Format<'_> {
|
|
||||||
FormatRefWithRule::new(self, FormatAnyFunctionDef)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'def, 'ast> IntoFormat<PyFormatContext<'ast>> for AnyFunctionDefinition<'def> {
|
|
||||||
type Format = FormatOwnedWithRule<
|
|
||||||
AnyFunctionDefinition<'def>,
|
|
||||||
FormatAnyFunctionDef,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
|
|
||||||
fn into_format(self) -> Self::Format {
|
|
||||||
FormatOwnedWithRule::new(self, FormatAnyFunctionDef)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
use ruff_python_ast::{Ranged, StmtAsyncWith, StmtWith, Suite, WithItem};
|
|
||||||
use ruff_text_size::TextRange;
|
|
||||||
|
|
||||||
use ruff_formatter::{format_args, write, FormatError};
|
use ruff_formatter::{format_args, write, FormatError};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::{Ranged, StmtWith};
|
||||||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
|
use ruff_text_size::TextRange;
|
||||||
|
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
|
@ -12,81 +10,29 @@ use crate::expression::parentheses::{
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
pub(super) enum AnyStatementWith<'a> {
|
#[derive(Default)]
|
||||||
With(&'a StmtWith),
|
pub struct FormatStmtWith;
|
||||||
AsyncWith(&'a StmtAsyncWith),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> AnyStatementWith<'a> {
|
impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
||||||
const fn is_async(&self) -> bool {
|
fn fmt_fields(&self, item: &StmtWith, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
matches!(self, AnyStatementWith::AsyncWith(_))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn items(&self) -> &[WithItem] {
|
|
||||||
match self {
|
|
||||||
AnyStatementWith::With(with) => with.items.as_slice(),
|
|
||||||
AnyStatementWith::AsyncWith(with) => with.items.as_slice(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn body(&self) -> &Suite {
|
|
||||||
match self {
|
|
||||||
AnyStatementWith::With(with) => &with.body,
|
|
||||||
AnyStatementWith::AsyncWith(with) => &with.body,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ranged for AnyStatementWith<'_> {
|
|
||||||
fn range(&self) -> TextRange {
|
|
||||||
match self {
|
|
||||||
AnyStatementWith::With(with) => with.range(),
|
|
||||||
AnyStatementWith::AsyncWith(with) => with.range(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a StmtWith> for AnyStatementWith<'a> {
|
|
||||||
fn from(value: &'a StmtWith) -> Self {
|
|
||||||
AnyStatementWith::With(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a StmtAsyncWith> for AnyStatementWith<'a> {
|
|
||||||
fn from(value: &'a StmtAsyncWith) -> Self {
|
|
||||||
AnyStatementWith::AsyncWith(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&AnyStatementWith<'a>> for AnyNodeRef<'a> {
|
|
||||||
fn from(value: &AnyStatementWith<'a>) -> Self {
|
|
||||||
match value {
|
|
||||||
AnyStatementWith::With(with) => AnyNodeRef::StmtWith(with),
|
|
||||||
AnyStatementWith::AsyncWith(with) => AnyNodeRef::StmtAsyncWith(with),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
|
|
||||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
let comments = f.context().comments().clone();
|
let comments = f.context().comments().clone();
|
||||||
let dangling_comments = comments.dangling_comments(self);
|
let dangling_comments = comments.dangling_comments(item);
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[
|
[
|
||||||
self.is_async()
|
item.is_async
|
||||||
.then_some(format_args![text("async"), space()]),
|
.then_some(format_args![text("async"), space()]),
|
||||||
text("with"),
|
text("with"),
|
||||||
space()
|
space()
|
||||||
]
|
]
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if are_with_items_parenthesized(self, f.context())? {
|
if are_with_items_parenthesized(item, f.context())? {
|
||||||
optional_parentheses(&format_with(|f| {
|
optional_parentheses(&format_with(|f| {
|
||||||
let mut joiner = f.join_comma_separated(self.body().first().unwrap().start());
|
let mut joiner = f.join_comma_separated(item.body.first().unwrap().start());
|
||||||
|
|
||||||
for item in self.items() {
|
for item in &item.items {
|
||||||
joiner.entry_with_line_separator(
|
joiner.entry_with_line_separator(
|
||||||
item,
|
item,
|
||||||
&item.format(),
|
&item.format(),
|
||||||
|
@ -98,7 +44,7 @@ impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
|
||||||
.fmt(f)?;
|
.fmt(f)?;
|
||||||
} else {
|
} else {
|
||||||
f.join_with(format_args![text(","), space()])
|
f.join_with(format_args![text(","), space()])
|
||||||
.entries(self.items().iter().formatted())
|
.entries(item.items.iter().formatted())
|
||||||
.finish()?;
|
.finish()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,18 +53,20 @@ impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
|
||||||
[
|
[
|
||||||
text(":"),
|
text(":"),
|
||||||
trailing_comments(dangling_comments),
|
trailing_comments(dangling_comments),
|
||||||
block_indent(&self.body().format())
|
block_indent(&item.body.format())
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fmt_dangling_comments(&self, _node: &StmtWith, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
|
// Handled in `fmt_fields`
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn are_with_items_parenthesized(
|
fn are_with_items_parenthesized(with: &StmtWith, context: &PyFormatContext) -> FormatResult<bool> {
|
||||||
with: &AnyStatementWith,
|
|
||||||
context: &PyFormatContext,
|
|
||||||
) -> FormatResult<bool> {
|
|
||||||
let first_with_item = with
|
let first_with_item = with
|
||||||
.items()
|
.items
|
||||||
.first()
|
.first()
|
||||||
.ok_or(FormatError::syntax_error("Expected at least one with item"))?;
|
.ok_or(FormatError::syntax_error("Expected at least one with item"))?;
|
||||||
let before_first_with_item = TextRange::new(with.start(), first_with_item.start());
|
let before_first_with_item = TextRange::new(with.start(), first_with_item.start());
|
||||||
|
@ -145,17 +93,3 @@ fn are_with_items_parenthesized(
|
||||||
None => Ok(false),
|
None => Ok(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct FormatStmtWith;
|
|
||||||
|
|
||||||
impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
|
||||||
fn fmt_fields(&self, item: &StmtWith, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
AnyStatementWith::from(item).fmt(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_dangling_comments(&self, _node: &StmtWith, _f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
// Handled in `fmt_fields`
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -210,10 +210,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
||||||
|
|
||||||
/// Returns `true` if a [`Stmt`] is a class or function definition.
|
/// Returns `true` if a [`Stmt`] is a class or function definition.
|
||||||
const fn is_class_or_function_definition(stmt: &Stmt) -> bool {
|
const fn is_class_or_function_definition(stmt: &Stmt) -> bool {
|
||||||
matches!(
|
matches!(stmt, Stmt::FunctionDef(_) | Stmt::ClassDef(_))
|
||||||
stmt,
|
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if a [`Stmt`] is an import.
|
/// Returns `true` if a [`Stmt`] is an import.
|
||||||
|
|
|
@ -858,11 +858,7 @@ ForStatement: ast::Stmt = {
|
||||||
.end();
|
.end();
|
||||||
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
||||||
let iter = Box::new(iter);
|
let iter = Box::new(iter);
|
||||||
if is_async.is_some() {
|
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, is_async: is_async.is_some(), range: (location..end_location).into() })
|
||||||
ast::Stmt::AsyncFor(ast::StmtAsyncFor { target, iter, body, orelse, range: (location..end_location).into() })
|
|
||||||
} else {
|
|
||||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, range: (location..end_location).into() })
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -975,11 +971,7 @@ ExceptClause: ast::ExceptHandler = {
|
||||||
WithStatement: ast::Stmt = {
|
WithStatement: ast::Stmt = {
|
||||||
<location:@L> <is_async:"async"?> "with" <items:WithItems> ":" <body:Suite> => {
|
<location:@L> <is_async:"async"?> "with" <items:WithItems> ":" <body:Suite> => {
|
||||||
let end_location = body.last().unwrap().end();
|
let end_location = body.last().unwrap().end();
|
||||||
if is_async.is_some() {
|
ast::StmtWith { items, body, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||||
ast::StmtAsyncWith { items, body, range: (location..end_location).into() }.into()
|
|
||||||
} else {
|
|
||||||
ast::StmtWith { items, body, range: (location..end_location).into() }.into()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1014,11 +1006,7 @@ FuncDef: ast::Stmt = {
|
||||||
let args = Box::new(args);
|
let args = Box::new(args);
|
||||||
let returns = r.map(Box::new);
|
let returns = r.map(Box::new);
|
||||||
let end_location = body.last().unwrap().end();
|
let end_location = body.last().unwrap().end();
|
||||||
if is_async.is_some() {
|
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||||
ast::StmtAsyncFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
|
||||||
} else {
|
|
||||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// auto-generated: "lalrpop 0.20.0"
|
// auto-generated: "lalrpop 0.20.0"
|
||||||
// sha3: f99d8cb29227bfbe1fa07719f655304a9a93fd4715726687ef40c091adbdbad5
|
// sha3: d713a7771107f8c20353ce5e890fba004b3c5491f513d28e9348a49cd510c59b
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use ruff_text_size::TextSize;
|
use ruff_text_size::TextSize;
|
||||||
use ruff_python_ast::{self as ast, Ranged, MagicKind};
|
use ruff_python_ast::{self as ast, Ranged, MagicKind};
|
||||||
|
@ -33081,11 +33081,7 @@ fn __action147<
|
||||||
.end();
|
.end();
|
||||||
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
||||||
let iter = Box::new(iter);
|
let iter = Box::new(iter);
|
||||||
if is_async.is_some() {
|
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, is_async: is_async.is_some(), range: (location..end_location).into() })
|
||||||
ast::Stmt::AsyncFor(ast::StmtAsyncFor { target, iter, body, orelse, range: (location..end_location).into() })
|
|
||||||
} else {
|
|
||||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, range: (location..end_location).into() })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33306,11 +33302,7 @@ fn __action155<
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
let end_location = body.last().unwrap().end();
|
let end_location = body.last().unwrap().end();
|
||||||
if is_async.is_some() {
|
ast::StmtWith { items, body, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||||
ast::StmtAsyncWith { items, body, range: (location..end_location).into() }.into()
|
|
||||||
} else {
|
|
||||||
ast::StmtWith { items, body, range: (location..end_location).into() }.into()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33407,11 +33399,7 @@ fn __action161<
|
||||||
let args = Box::new(args);
|
let args = Box::new(args);
|
||||||
let returns = r.map(Box::new);
|
let returns = r.map(Box::new);
|
||||||
let end_location = body.last().unwrap().end();
|
let end_location = body.last().unwrap().end();
|
||||||
if is_async.is_some() {
|
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||||
ast::StmtAsyncFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
|
||||||
} else {
|
|
||||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
||||||
For(
|
For(
|
||||||
StmtFor {
|
StmtFor {
|
||||||
range: 0..24,
|
range: 0..24,
|
||||||
|
is_async: false,
|
||||||
target: Name(
|
target: Name(
|
||||||
ExprName {
|
ExprName {
|
||||||
range: 4..5,
|
range: 4..5,
|
||||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 0..17,
|
range: 0..17,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 5..11,
|
range: 5..11,
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..23,
|
range: 0..23,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..29,
|
range: 0..29,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..13,
|
range: 0..13,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..13,
|
range: 0..13,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..32,
|
range: 0..32,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..38,
|
range: 0..38,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..42,
|
range: 0..42,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..52,
|
range: 0..52,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..20,
|
range: 0..20,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..26,
|
range: 0..26,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..20,
|
range: 0..20,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "f",
|
id: "f",
|
||||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..34,
|
range: 0..34,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [
|
decorator_list: [
|
||||||
Decorator {
|
Decorator {
|
||||||
range: 0..13,
|
range: 0..13,
|
||||||
|
|
|
@ -125,6 +125,7 @@ Module(
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 566..626,
|
range: 566..626,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "foo",
|
id: "foo",
|
||||||
|
@ -199,6 +200,7 @@ Module(
|
||||||
For(
|
For(
|
||||||
StmtFor {
|
StmtFor {
|
||||||
range: 701..727,
|
range: 701..727,
|
||||||
|
is_async: false,
|
||||||
target: Name(
|
target: Name(
|
||||||
ExprName {
|
ExprName {
|
||||||
range: 705..706,
|
range: 705..706,
|
||||||
|
|
|
@ -38,6 +38,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 18..44,
|
range: 18..44,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "__init__",
|
id: "__init__",
|
||||||
|
@ -78,6 +79,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 46..98,
|
range: 46..98,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "method_with_default",
|
id: "method_with_default",
|
||||||
|
|
|
@ -6,6 +6,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 0..20,
|
range: 0..20,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "func",
|
id: "func",
|
||||||
|
@ -53,6 +54,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 22..53,
|
range: 22..53,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "func",
|
id: "func",
|
||||||
|
@ -132,6 +134,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 55..91,
|
range: 55..91,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "func",
|
id: "func",
|
||||||
|
@ -219,6 +222,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 93..138,
|
range: 93..138,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "func",
|
id: "func",
|
||||||
|
@ -321,6 +325,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 140..171,
|
range: 140..171,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "func",
|
id: "func",
|
||||||
|
@ -393,6 +398,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 173..230,
|
range: 173..230,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "func",
|
id: "func",
|
||||||
|
@ -496,6 +502,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 232..273,
|
range: 232..273,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "func",
|
id: "func",
|
||||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
||||||
FunctionDef(
|
FunctionDef(
|
||||||
StmtFunctionDef {
|
StmtFunctionDef {
|
||||||
range: 1..49,
|
range: 1..49,
|
||||||
|
is_async: false,
|
||||||
decorator_list: [],
|
decorator_list: [],
|
||||||
name: Identifier {
|
name: Identifier {
|
||||||
id: "args_to_tuple",
|
id: "args_to_tuple",
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_parser/src/parser.rs
|
source: crates/ruff_python_parser/src/parser.rs
|
||||||
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 0..12,
|
range: 0..12,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 5..6,
|
range: 5..6,
|
||||||
|
@ -33,6 +34,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 13..30,
|
range: 13..30,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 18..24,
|
range: 18..24,
|
||||||
|
@ -68,6 +70,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 31..46,
|
range: 31..46,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 36..37,
|
range: 36..37,
|
||||||
|
@ -108,6 +111,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 47..72,
|
range: 47..72,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 52..58,
|
range: 52..58,
|
||||||
|
@ -164,6 +168,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 73..97,
|
range: 73..97,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 78..91,
|
range: 78..91,
|
||||||
|
@ -214,6 +219,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 98..127,
|
range: 98..127,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 103..121,
|
range: 103..121,
|
||||||
|
@ -272,6 +278,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 128..141,
|
range: 128..141,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 133..135,
|
range: 133..135,
|
||||||
|
@ -297,6 +304,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 142..160,
|
range: 142..160,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 147..154,
|
range: 147..154,
|
||||||
|
@ -330,6 +338,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 161..175,
|
range: 161..175,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 167..168,
|
range: 167..168,
|
||||||
|
@ -357,6 +366,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 176..195,
|
range: 176..195,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 181..189,
|
range: 181..189,
|
||||||
|
@ -392,6 +402,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 196..211,
|
range: 196..211,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 202..203,
|
range: 202..203,
|
||||||
|
@ -419,6 +430,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 212..232,
|
range: 212..232,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 217..226,
|
range: 217..226,
|
||||||
|
@ -462,6 +474,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 233..250,
|
range: 233..250,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 239..243,
|
range: 239..243,
|
||||||
|
@ -502,6 +515,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 251..273,
|
range: 251..273,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 256..267,
|
range: 256..267,
|
||||||
|
@ -554,6 +568,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 274..290,
|
range: 274..290,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 279..284,
|
range: 279..284,
|
||||||
|
@ -593,6 +608,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 291..312,
|
range: 291..312,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 296..306,
|
range: 296..306,
|
||||||
|
@ -640,6 +656,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 313..331,
|
range: 313..331,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 318..325,
|
range: 318..325,
|
||||||
|
@ -688,6 +705,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 332..355,
|
range: 332..355,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 337..349,
|
range: 337..349,
|
||||||
|
@ -744,6 +762,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 356..375,
|
range: 356..375,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 361..369,
|
range: 361..369,
|
||||||
|
@ -783,6 +802,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 376..400,
|
range: 376..400,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 381..394,
|
range: 381..394,
|
||||||
|
@ -830,6 +850,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 401..428,
|
range: 401..428,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 406..422,
|
range: 406..422,
|
||||||
|
@ -898,6 +919,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 429..461,
|
range: 429..461,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 434..455,
|
range: 434..455,
|
||||||
|
@ -974,6 +996,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 462..481,
|
range: 462..481,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 468..474,
|
range: 468..474,
|
||||||
|
@ -1009,6 +1032,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 482..502,
|
range: 482..502,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 488..494,
|
range: 488..494,
|
||||||
|
@ -1044,6 +1068,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 503..530,
|
range: 503..530,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 509..515,
|
range: 509..515,
|
||||||
|
@ -1100,6 +1125,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
With(
|
With(
|
||||||
StmtWith {
|
StmtWith {
|
||||||
range: 531..559,
|
range: 531..559,
|
||||||
|
is_async: false,
|
||||||
items: [
|
items: [
|
||||||
WithItem {
|
WithItem {
|
||||||
range: 537..543,
|
range: 537..543,
|
||||||
|
|
|
@ -178,8 +178,7 @@ impl ModuleSource<'_> {
|
||||||
|
|
||||||
pub(crate) fn function_visibility(stmt: &Stmt) -> Visibility {
|
pub(crate) fn function_visibility(stmt: &Stmt) -> Visibility {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => {
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => {
|
|
||||||
if name.starts_with('_') {
|
if name.starts_with('_') {
|
||||||
Visibility::Private
|
Visibility::Private
|
||||||
} else {
|
} else {
|
||||||
|
@ -191,52 +190,45 @@ pub(crate) fn function_visibility(stmt: &Stmt) -> Visibility {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn method_visibility(stmt: &Stmt) -> Visibility {
|
pub(crate) fn method_visibility(stmt: &Stmt) -> Visibility {
|
||||||
match stmt {
|
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
name,
|
||||||
name,
|
decorator_list,
|
||||||
decorator_list,
|
..
|
||||||
..
|
}) = stmt
|
||||||
|
else {
|
||||||
|
panic!("Found non-FunctionDef in method_visibility")
|
||||||
|
};
|
||||||
|
|
||||||
|
// Is this a setter or deleter?
|
||||||
|
if decorator_list.iter().any(|decorator| {
|
||||||
|
collect_call_path(&decorator.expression).is_some_and(|call_path| {
|
||||||
|
call_path.as_slice() == [name, "setter"] || call_path.as_slice() == [name, "deleter"]
|
||||||
})
|
})
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
}) {
|
||||||
name,
|
return Visibility::Private;
|
||||||
decorator_list,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
// Is this a setter or deleter?
|
|
||||||
if decorator_list.iter().any(|decorator| {
|
|
||||||
collect_call_path(&decorator.expression).is_some_and(|call_path| {
|
|
||||||
call_path.as_slice() == [name, "setter"]
|
|
||||||
|| call_path.as_slice() == [name, "deleter"]
|
|
||||||
})
|
|
||||||
}) {
|
|
||||||
return Visibility::Private;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is the method non-private?
|
|
||||||
if !name.starts_with('_') {
|
|
||||||
return Visibility::Public;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is this a magic method?
|
|
||||||
if name.starts_with("__") && name.ends_with("__") {
|
|
||||||
return Visibility::Public;
|
|
||||||
}
|
|
||||||
|
|
||||||
Visibility::Private
|
|
||||||
}
|
|
||||||
_ => panic!("Found non-FunctionDef in method_visibility"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Is the method non-private?
|
||||||
|
if !name.starts_with('_') {
|
||||||
|
return Visibility::Public;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is this a magic method?
|
||||||
|
if name.starts_with("__") && name.ends_with("__") {
|
||||||
|
return Visibility::Public;
|
||||||
|
}
|
||||||
|
|
||||||
|
Visibility::Private
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn class_visibility(stmt: &Stmt) -> Visibility {
|
pub(crate) fn class_visibility(stmt: &Stmt) -> Visibility {
|
||||||
match stmt {
|
let Stmt::ClassDef(ast::StmtClassDef { name, .. }) = stmt else {
|
||||||
Stmt::ClassDef(ast::StmtClassDef { name, .. }) => {
|
panic!("Found non-ClassDef in class_visibility")
|
||||||
if name.starts_with('_') {
|
};
|
||||||
Visibility::Private
|
|
||||||
} else {
|
if name.starts_with('_') {
|
||||||
Visibility::Public
|
Visibility::Private
|
||||||
}
|
} else {
|
||||||
}
|
Visibility::Public
|
||||||
_ => panic!("Found non-ClassDef in function_visibility"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,6 @@ impl<'a> Member<'a> {
|
||||||
pub fn name(&self) -> Option<&'a str> {
|
pub fn name(&self) -> Option<&'a str> {
|
||||||
match &self.stmt {
|
match &self.stmt {
|
||||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
||||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. })
|
|
||||||
| Stmt::ClassDef(ast::StmtClassDef { name, .. }) => Some(name),
|
| Stmt::ClassDef(ast::StmtClassDef { name, .. }) => Some(name),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ impl<'a> StatementVisitor<'a> for GlobalsVisitor<'a> {
|
||||||
self.0.insert(name.as_str(), *range);
|
self.0.insert(name.as_str(), *range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||||
// Don't recurse.
|
// Don't recurse.
|
||||||
}
|
}
|
||||||
_ => walk_stmt(self, stmt),
|
_ => walk_stmt(self, stmt),
|
||||||
|
|
|
@ -478,7 +478,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
seen_function |= scope.kind.is_any_function();
|
seen_function |= scope.kind.is_function();
|
||||||
import_starred = import_starred || scope.uses_star_imports();
|
import_starred = import_starred || scope.uses_star_imports();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -540,7 +540,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
seen_function |= scope.kind.is_any_function();
|
seen_function |= scope.kind.is_function();
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
@ -1015,11 +1015,8 @@ impl<'a> SemanticModel<'a> {
|
||||||
/// Return `true` if the model is in an async context.
|
/// Return `true` if the model is in an async context.
|
||||||
pub fn in_async_context(&self) -> bool {
|
pub fn in_async_context(&self) -> bool {
|
||||||
for scope in self.current_scopes() {
|
for scope in self.current_scopes() {
|
||||||
if scope.kind.is_async_function() {
|
if let ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) = scope.kind {
|
||||||
return true;
|
return *is_async;
|
||||||
}
|
|
||||||
if scope.kind.is_function() {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
|
|
|
@ -178,19 +178,12 @@ bitflags! {
|
||||||
pub enum ScopeKind<'a> {
|
pub enum ScopeKind<'a> {
|
||||||
Class(&'a ast::StmtClassDef),
|
Class(&'a ast::StmtClassDef),
|
||||||
Function(&'a ast::StmtFunctionDef),
|
Function(&'a ast::StmtFunctionDef),
|
||||||
AsyncFunction(&'a ast::StmtAsyncFunctionDef),
|
|
||||||
Generator,
|
Generator,
|
||||||
Module,
|
Module,
|
||||||
Type,
|
Type,
|
||||||
Lambda(&'a ast::ExprLambda),
|
Lambda(&'a ast::ExprLambda),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScopeKind<'_> {
|
|
||||||
pub const fn is_any_function(&self) -> bool {
|
|
||||||
matches!(self, ScopeKind::Function(_) | ScopeKind::AsyncFunction(_))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Id uniquely identifying a scope in a program.
|
/// Id uniquely identifying a scope in a program.
|
||||||
///
|
///
|
||||||
/// Using a `u32` is sufficient because Ruff only supports parsing documents with a size of max `u32::max`
|
/// Using a `u32` is sufficient because Ruff only supports parsing documents with a size of max `u32::max`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue