mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Visit AugAssign target after value (#5325)
## Summary When visiting AugAssign in evaluation order, the AugAssign `target` should be visited after it's `value`. Based on my testing, the pseudo code for `a += b` is effectively: ```python tmp = a a = tmp.__iadd__(b) ``` That is, an ideal traversal order would look something like this: 1. load a 2. b 3. op 4. store a But, there is only a single AST node which captures `a` in the statement `a += b`, so it cannot be traversed both before and after the traversal of `b` and the `op`. Nonetheless, I think traversing `a` after `b` and the `op` makes the most sense for a number of reasons: 1. All the other assignment expressions traverse their `value`s before their `target`s. Having `AugAssign` traverse in the same order would be more consistent. 2. Within the AST, the `ctx` of the `target` for an `AugAssign` is `Store` (though technically this is a `Load` and `Store` operation, the AST only indicates it as a `Store`). Since the the store portion of the `AugAssign` occurs last, I think it makes sense to traverse the `target` last as well. The effect of this is marginal, but it may have an impact on the behavior of #5271.
This commit is contained in:
parent
2f03159c8b
commit
f85eb709e2
1 changed files with 2 additions and 2 deletions
|
@ -167,9 +167,9 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
value,
|
||||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_expr(target);
|
||||
visitor.visit_operator(op);
|
||||
visitor.visit_expr(value);
|
||||
visitor.visit_operator(op);
|
||||
visitor.visit_expr(target);
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue