diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 29370c35a56534..772e0f9a2dbfc7 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -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, @@ -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, diff --git a/crates/ruff/src/rules/pyflakes/mod.rs b/crates/ruff/src/rules/pyflakes/mod.rs index eca705373b3f72..1271ca2cec435e 100644 --- a/crates/ruff/src/rules/pyflakes/mod.rs +++ b/crates/ruff/src/rules/pyflakes/mod.rs @@ -2022,11 +2022,7 @@ mod tests { try: pass except Exception as fu: pass "#, - &[ - Rule::UnusedImport, - Rule::RedefinedWhileUnused, - Rule::UnusedVariable, - ], + &[Rule::RedefinedWhileUnused, Rule::UnusedVariable], ); } diff --git a/crates/ruff/src/rules/pyflakes/rules/unused_import.rs b/crates/ruff/src/rules/pyflakes/rules/unused_import.rs index 9017eea015918a..4ad8f1c7195db5 100644 --- a/crates/ruff/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff/src/rules/pyflakes/rules/unused_import.rs @@ -100,16 +100,9 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut let mut unused: FxHashMap<(NodeId, Exceptions), Vec> = FxHashMap::default(); let mut ignored: FxHashMap<(NodeId, Exceptions), Vec> = 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() diff --git a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__print_after_shadowing_except.snap b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__print_after_shadowing_except.snap deleted file mode 100644 index de3359d17c10f9..00000000000000 --- a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__print_after_shadowing_except.snap +++ /dev/null @@ -1,24 +0,0 @@ ---- -source: crates/ruff/src/rules/pyflakes/mod.rs ---- -:7:25: F841 [*] Local variable `x` is assigned to but never used - | -5 | try: -6 | 1 / 0 -7 | except Exception as x: - | ^ F841 -8 | pass - | - = help: Remove assignment to unused variable `x` - -ℹ Fix -4 4 | -5 5 | try: -6 6 | 1 / 0 -7 |- except Exception as x: - 7 |+ except Exception: -8 8 | pass -9 9 | -10 10 | print(x) - - diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index 7499e118a10b90..3ea95b2dee1a89 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -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.