[ruff] Avoid treating named expressions as static keys (RUF011) (#9494)

Closes https://github.com/astral-sh/ruff/issues/9487.
This commit is contained in:
Charlie Marsh 2024-01-12 14:33:45 -05:00 committed by GitHub
parent 7504bf347b
commit 009430e034
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 49 deletions

View file

@ -914,6 +914,27 @@ where
}
}
/// A [`Visitor`] to collect all stored [`Expr::Name`] nodes in an AST.
#[derive(Debug, Default)]
pub struct StoredNameFinder<'a> {
/// A map from identifier to defining expression.
pub names: FxHashMap<&'a str, &'a ast::ExprName>,
}
impl<'a, 'b> Visitor<'b> for StoredNameFinder<'a>
where
'b: 'a,
{
fn visit_expr(&mut self, expr: &'a Expr) {
if let Expr::Name(name) = expr {
if name.ctx.is_store() {
self.names.insert(&name.id, name);
}
}
crate::visitor::walk_expr(self, expr);
}
}
/// A [`StatementVisitor`] that collects all `return` statements in a function or method.
#[derive(Default)]
pub struct ReturnStatementVisitor<'a> {