Move unused deletion tracking to deferred analysis (#5852)

## Summary

This PR moves the "unused exception" rule out of the visitor and into a
deferred check. When we can base rules solely on the semantic model, we
probably should, as it greatly simplifies the `Checker` itself.
This commit is contained in:
Charlie Marsh 2023-07-18 20:43:12 -04:00 committed by GitHub
parent 2d505e2b04
commit 7ffcd93afd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 33 deletions

View file

@ -5,6 +5,7 @@ use ruff_text_size::TextRange;
use rustpython_parser::ast::Ranged;
use ruff_index::{newtype_index, IndexSlice, IndexVec};
use ruff_python_ast::source_code::Locator;
use crate::context::ExecutionContext;
use crate::model::SemanticModel;
@ -163,6 +164,11 @@ impl<'a> Binding<'a> {
}
}
/// Returns the name of the binding (e.g., `x` in `x = 1`).
pub fn name<'b>(&self, locator: &'b Locator) -> &'b str {
locator.slice(self.range)
}
/// Returns the range of the binding's parent.
pub fn parent_range(&self, semantic: &SemanticModel) -> Option<TextRange> {
self.source
@ -417,7 +423,16 @@ pub enum BindingKind<'a> {
/// ```
Deletion,
/// A binding to unbind the local variable, like `x` in:
/// A binding to bind an exception to a local variable, like `x` in:
/// ```python
/// try:
/// ...
/// except Exception as x:
/// ...
/// ```
BoundException,
/// A binding to unbind a bound local exception, like `x` in:
/// ```python
/// try:
/// ...
@ -428,7 +443,6 @@ pub enum BindingKind<'a> {
/// After the `except` block, `x` is unbound, despite the lack
/// of an explicit `del` statement.
///
///
/// Stores the ID of the binding that was shadowed in the enclosing
/// scope, if any.
UnboundException(Option<BindingId>),