mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-14 23:51:03 +00:00
Support variable keys in static dictionary key rule (#9411)
Closes https://github.com/astral-sh/ruff/issues/9410.
This commit is contained in:
parent
c2c9997682
commit
701697c37e
5 changed files with 114 additions and 39 deletions
|
@ -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> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue