mirror of https://github.com/astral-sh/ruff
Add a `BindingKind` for `WithItem` variables (#8594)
This commit is contained in:
parent
0ac124acef
commit
346a828db2
|
|
@ -1635,6 +1635,11 @@ impl<'a> Checker<'a> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if parent.is_with_stmt() {
|
||||||
|
self.add_binding(id, expr.range(), BindingKind::WithItemVar, flags);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let scope = self.semantic.current_scope();
|
let scope = self.semantic.current_scope();
|
||||||
|
|
||||||
if scope.kind.is_module()
|
if scope.kind.is_module()
|
||||||
|
|
|
||||||
|
|
@ -248,6 +248,7 @@ impl Renamer {
|
||||||
| BindingKind::Assignment
|
| BindingKind::Assignment
|
||||||
| BindingKind::BoundException
|
| BindingKind::BoundException
|
||||||
| BindingKind::LoopVar
|
| BindingKind::LoopVar
|
||||||
|
| BindingKind::WithItemVar
|
||||||
| BindingKind::Global
|
| BindingKind::Global
|
||||||
| BindingKind::Nonlocal(_)
|
| BindingKind::Nonlocal(_)
|
||||||
| BindingKind::ClassDefinition(_)
|
| BindingKind::ClassDefinition(_)
|
||||||
|
|
|
||||||
|
|
@ -322,7 +322,9 @@ pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mu
|
||||||
.bindings()
|
.bindings()
|
||||||
.map(|(name, binding_id)| (name, checker.semantic().binding(binding_id)))
|
.map(|(name, binding_id)| (name, checker.semantic().binding(binding_id)))
|
||||||
.filter_map(|(name, binding)| {
|
.filter_map(|(name, binding)| {
|
||||||
if (binding.kind.is_assignment() || binding.kind.is_named_expr_assignment())
|
if (binding.kind.is_assignment()
|
||||||
|
|| binding.kind.is_named_expr_assignment()
|
||||||
|
|| binding.kind.is_with_item_var())
|
||||||
&& (!binding.is_unpacked_assignment()
|
&& (!binding.is_unpacked_assignment()
|
||||||
|| matches!(checker.settings.preview, PreviewMode::Enabled))
|
|| matches!(checker.settings.preview, PreviewMode::Enabled))
|
||||||
&& !binding.is_nonlocal()
|
&& !binding.is_nonlocal()
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@ pub(crate) fn non_ascii_name(binding: &Binding, locator: &Locator) -> Option<Dia
|
||||||
BindingKind::Assignment => Kind::Assignment,
|
BindingKind::Assignment => Kind::Assignment,
|
||||||
BindingKind::TypeParam => Kind::TypeParam,
|
BindingKind::TypeParam => Kind::TypeParam,
|
||||||
BindingKind::LoopVar => Kind::LoopVar,
|
BindingKind::LoopVar => Kind::LoopVar,
|
||||||
|
BindingKind::WithItemVar => Kind::WithItemVar,
|
||||||
BindingKind::Global => Kind::Global,
|
BindingKind::Global => Kind::Global,
|
||||||
BindingKind::Nonlocal(_) => Kind::Nonlocal,
|
BindingKind::Nonlocal(_) => Kind::Nonlocal,
|
||||||
BindingKind::ClassDefinition(_) => Kind::ClassDefinition,
|
BindingKind::ClassDefinition(_) => Kind::ClassDefinition,
|
||||||
|
|
@ -87,6 +88,7 @@ enum Kind {
|
||||||
Assignment,
|
Assignment,
|
||||||
TypeParam,
|
TypeParam,
|
||||||
LoopVar,
|
LoopVar,
|
||||||
|
WithItemVar,
|
||||||
Global,
|
Global,
|
||||||
Nonlocal,
|
Nonlocal,
|
||||||
ClassDefinition,
|
ClassDefinition,
|
||||||
|
|
@ -103,6 +105,7 @@ impl fmt::Display for Kind {
|
||||||
Kind::Assignment => f.write_str("Variable"),
|
Kind::Assignment => f.write_str("Variable"),
|
||||||
Kind::TypeParam => f.write_str("Type parameter"),
|
Kind::TypeParam => f.write_str("Type parameter"),
|
||||||
Kind::LoopVar => f.write_str("Variable"),
|
Kind::LoopVar => f.write_str("Variable"),
|
||||||
|
Kind::WithItemVar => f.write_str("Variable"),
|
||||||
Kind::Global => f.write_str("Global"),
|
Kind::Global => f.write_str("Global"),
|
||||||
Kind::Nonlocal => f.write_str("Nonlocal"),
|
Kind::Nonlocal => f.write_str("Nonlocal"),
|
||||||
Kind::ClassDefinition => f.write_str("Class"),
|
Kind::ClassDefinition => f.write_str("Class"),
|
||||||
|
|
|
||||||
|
|
@ -432,6 +432,13 @@ pub enum BindingKind<'a> {
|
||||||
/// ```
|
/// ```
|
||||||
LoopVar,
|
LoopVar,
|
||||||
|
|
||||||
|
/// A binding for a with statement variable, like `x` in:
|
||||||
|
/// ```python
|
||||||
|
/// with open('foo.py') as x:
|
||||||
|
/// ...
|
||||||
|
/// ```
|
||||||
|
WithItemVar,
|
||||||
|
|
||||||
/// A binding for a global variable, like `x` in:
|
/// A binding for a global variable, like `x` in:
|
||||||
/// ```python
|
/// ```python
|
||||||
/// def foo():
|
/// def foo():
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue