Support variable keys in static dictionary key rule (#9411)

Closes https://github.com/astral-sh/ruff/issues/9410.
This commit is contained in:
Charlie Marsh 2024-01-06 15:44:40 -05:00 committed by GitHub
parent c2c9997682
commit 701697c37e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 39 deletions

View file

@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::path::Path;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use ruff_python_trivia::CommentRanges;
@ -891,6 +892,25 @@ pub fn resolve_imported_module_path<'a>(
Some(Cow::Owned(qualified_path))
}
/// A [`Visitor`] to collect all [`Expr::Name`] nodes in an AST.
#[derive(Debug, Default)]
pub struct NameFinder<'a> {
/// A map from identifier to defining expression.
pub names: FxHashMap<&'a str, &'a ast::ExprName>,
}
impl<'a, 'b> Visitor<'b> for NameFinder<'a>
where
'b: 'a,
{
fn visit_expr(&mut self, expr: &'a Expr) {
if let Expr::Name(name) = expr {
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> {