Skip to content

Commit

Permalink
No clone
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 21, 2023
1 parent 612e20b commit 0a708e0
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 31 deletions.
15 changes: 3 additions & 12 deletions crates/ruff/src/checkers/ast/mod.rs
Expand Up @@ -3852,10 +3852,8 @@ where
);
}

// Grab the existing binding.
let existing_id = self.semantic.scope().get(name);

// Add the bound exception name to the scope.
let is_bound = self.semantic.scope().has(name);
let binding_id = self.add_binding(
name,
range,
Expand Down Expand Up @@ -3885,15 +3883,8 @@ where
}
}

if let Some(existing_id) = existing_id {
// If the name was already bound, restore the existing binding. We can't
// know whether this handler will trigger, so we proceed as if it
// didn't. Treat it as an entirely new binding to avoid creating a cycle
// in the shadowing graph.
let binding_id = self.semantic.copy_binding(existing_id);
self.semantic.scope_mut().add(name, binding_id);
} else {
// If the name wasn't already bound, mark it as unbound.
// If the name wasn't already bound, mark it as unbound.
if !is_bound {
self.add_binding(
name,
range,
Expand Down
6 changes: 1 addition & 5 deletions crates/ruff/src/rules/pyflakes/mod.rs
Expand Up @@ -2022,11 +2022,7 @@ mod tests {
try: pass
except Exception as fu: pass
"#,
&[
Rule::UnusedImport,
Rule::RedefinedWhileUnused,
Rule::UnusedVariable,
],
&[Rule::RedefinedWhileUnused, Rule::UnusedVariable],
);
}

Expand Down
9 changes: 1 addition & 8 deletions crates/ruff/src/rules/pyflakes/rules/unused_import.rs
Expand Up @@ -100,16 +100,9 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut
let mut unused: FxHashMap<(NodeId, Exceptions), Vec<Import>> = FxHashMap::default();
let mut ignored: FxHashMap<(NodeId, Exceptions), Vec<Import>> = FxHashMap::default();

for (name, binding_id) in scope.bindings() {
for binding_id in scope.binding_ids() {
let binding = checker.semantic().binding(binding_id);

if !binding.kind.is_builtin() {
println!("binding: {:?}", binding);
for b in scope.get_all(name) {
println!(" {:?}", b);
}
}

if binding.is_used()
|| binding.is_explicit_export()
|| binding.is_nonlocal()
Expand Down
6 changes: 0 additions & 6 deletions crates/ruff_python_semantic/src/model.rs
Expand Up @@ -228,12 +228,6 @@ impl<'a> SemanticModel<'a> {
.map(|binding_id| &self.bindings[binding_id])
}

/// Create a copy of the given [`BindingId`] and return the new [`BindingId`].
pub fn copy_binding(&mut self, id: BindingId) -> BindingId {
let binding = self.bindings[id].clone();
self.bindings.push(binding)
}

/// Return the [`BindingId`] that the given [`BindingId`] shadows, if any.
///
/// Note that this will only return bindings that are shadowed by a binding in a parent scope.
Expand Down

0 comments on commit 0a708e0

Please sign in to comment.