Skip to content

Commit

Permalink
fixup! [pyflakes] Fix check of unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
gpilikin committed Dec 19, 2024
1 parent 4f20c21 commit 2d2f4dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
5 changes: 1 addition & 4 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,10 +1135,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
range: _,
ctx,
attr: _,
}) => match ctx {
ExprContext::Load => self.handle_attribute_load(expr),
_ => {}
},
}) => if ctx == &ExprContext::Load { self.handle_attribute_load(expr) }
Expr::Name(ast::ExprName { id, ctx, range: _ }) => match ctx {
ExprContext::Load => self.handle_node_load(expr),
ExprContext::Store => self.handle_node_store(id, expr),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut

let binding_name = top_binding
.name(checker.source())
.split(".")
.split('.')
.next()
.unwrap_or("");

Expand Down
37 changes: 17 additions & 20 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl<'a> SemanticModel<'a> {
&mut self,
binding_id: BindingId,
name_expr: &ExprName,
scope_id: &ScopeId,
scope_id: ScopeId,
) -> Option<ReadResult> {
let reference_id = self.resolved_references.push(
self.scope_id,
Expand All @@ -370,7 +370,7 @@ impl<'a> SemanticModel<'a> {
self.bindings[binding_id].references.push(reference_id);

if let Some(binding_id) =
self.resolve_submodule(name_expr.id.as_str(), *scope_id, binding_id)
self.resolve_submodule(name_expr.id.as_str(), scope_id, binding_id)
{
let reference_id = self.resolved_references.push(
self.scope_id,
Expand Down Expand Up @@ -465,7 +465,7 @@ impl<'a> SemanticModel<'a> {

// Mark any submodule aliases as used.
if let Some(binding_id) =
self.resolve_submodule(name_expr.id.as_str(), *scope_id, binding_id)
self.resolve_submodule(name_expr.id.as_str(), scope_id, binding_id)
{
let reference_id = self.resolved_references.push(
self.scope_id,
Expand Down Expand Up @@ -536,17 +536,17 @@ impl<'a> SemanticModel<'a> {
let ancestor_scope_ids: Vec<_> = self.scopes.ancestor_ids(self.scope_id).collect();
let mut binding_ids: Vec<(BindingId, ScopeId)> = vec![];

for (_index, scope_id) in ancestor_scope_ids.into_iter().enumerate() {
for scope_id in ancestor_scope_ids {
for binding_id in self.scopes[scope_id].get_all(name_expr.unwrap().id.as_str()) {
binding_ids.push((binding_id, scope_id));
}
}

for (binding_id, scope_id) in binding_ids.iter() {
for (binding_id, scope_id) in &binding_ids {
if let BindingKind::SubmoduleImport(binding_kind) = &self.binding(*binding_id).kind {
if binding_kind.qualified_name.to_string() == full_name {
if let Some(result) =
self.resolve_binding(*binding_id, &name_expr.unwrap(), scope_id)
self.resolve_binding(*binding_id, name_expr.unwrap(), *scope_id)
{
return result;
}
Expand All @@ -559,14 +559,14 @@ impl<'a> SemanticModel<'a> {

// TODO: need to move the block implementation to resolve_load, but carefully
// start check module import
for (binding_id, scope_id) in binding_ids.iter() {
for (binding_id, scope_id) in &binding_ids {
let Some(import) = self.binding(*binding_id).as_any_import() else {
continue;
};
let name = &import
.qualified_name()
.to_string()
.split(".")
.split('.')
.next()
.unwrap_or("")
.to_owned();
Expand All @@ -575,26 +575,25 @@ impl<'a> SemanticModel<'a> {
BindingKind::SubmoduleImport(_) if !is_name_exist => continue,
BindingKind::WithItemVar => continue,
BindingKind::SubmoduleImport(_) => {
result = self.resolve_binding(*binding_id, &name_expr.unwrap(), scope_id);
result = self.resolve_binding(*binding_id, name_expr.unwrap(), *scope_id);
}
BindingKind::Import(_) => {
if already_checked_imports.contains(&name.to_string()) {
continue;
} else {
already_checked_imports.insert(name.to_string());
}
already_checked_imports.insert(name.to_string());

result = self.resolve_binding(*binding_id, &name_expr.unwrap(), scope_id);
result = self.resolve_binding(*binding_id, name_expr.unwrap(), *scope_id);
}
_ => {}
}
}
// end check module import

if result.is_none() {
ReadResult::NotFound
if let Some(result) = result {
result
} else {
result.unwrap()
ReadResult::NotFound
}
}

Expand Down Expand Up @@ -639,7 +638,6 @@ impl<'a> SemanticModel<'a> {
let mut seen_function = false;
let mut import_starred = false;
let mut class_variables_visible = true;
let mut result = None;

let ancestor_scope_ids: Vec<_> = self.scopes.ancestor_ids(self.scope_id).collect();

Expand Down Expand Up @@ -711,11 +709,10 @@ impl<'a> SemanticModel<'a> {
}
}

result = self.resolve_binding(binding_id, &name, &scope_id);

if !result.is_none() {
return result.unwrap();
if let Some(res) = self.resolve_binding(binding_id, name, scope_id) {
return res;
}

}
// Allow usages of `__module__` and `__qualname__` within class scopes, e.g.:
//
Expand Down

0 comments on commit 2d2f4dc

Please sign in to comment.