Skip to content

Commit

Permalink
Add a BindingKind for WithItem variables (#8594)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Nov 10, 2023
1 parent 0ac124a commit 346a828
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,11 @@ impl<'a> Checker<'a> {
return;
}

if parent.is_with_stmt() {
self.add_binding(id, expr.range(), BindingKind::WithItemVar, flags);
return;
}

let scope = self.semantic.current_scope();

if scope.kind.is_module()
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/renamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ impl Renamer {
| BindingKind::Assignment
| BindingKind::BoundException
| BindingKind::LoopVar
| BindingKind::WithItemVar
| BindingKind::Global
| BindingKind::Nonlocal(_)
| BindingKind::ClassDefinition(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mu
.bindings()
.map(|(name, binding_id)| (name, checker.semantic().binding(binding_id)))
.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()
|| matches!(checker.settings.preview, PreviewMode::Enabled))
&& !binding.is_nonlocal()
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/non_ascii_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub(crate) fn non_ascii_name(binding: &Binding, locator: &Locator) -> Option<Dia
BindingKind::Assignment => Kind::Assignment,
BindingKind::TypeParam => Kind::TypeParam,
BindingKind::LoopVar => Kind::LoopVar,
BindingKind::WithItemVar => Kind::WithItemVar,
BindingKind::Global => Kind::Global,
BindingKind::Nonlocal(_) => Kind::Nonlocal,
BindingKind::ClassDefinition(_) => Kind::ClassDefinition,
Expand Down Expand Up @@ -87,6 +88,7 @@ enum Kind {
Assignment,
TypeParam,
LoopVar,
WithItemVar,
Global,
Nonlocal,
ClassDefinition,
Expand All @@ -103,6 +105,7 @@ impl fmt::Display for Kind {
Kind::Assignment => f.write_str("Variable"),
Kind::TypeParam => f.write_str("Type parameter"),
Kind::LoopVar => f.write_str("Variable"),
Kind::WithItemVar => f.write_str("Variable"),
Kind::Global => f.write_str("Global"),
Kind::Nonlocal => f.write_str("Nonlocal"),
Kind::ClassDefinition => f.write_str("Class"),
Expand Down
7 changes: 7 additions & 0 deletions crates/ruff_python_semantic/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ pub enum BindingKind<'a> {
/// ```
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:
/// ```python
/// def foo():
Expand Down

0 comments on commit 346a828

Please sign in to comment.