From 36610666efbed59ce21a6dbf2e5175f819531d8d Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 5 Jun 2024 16:12:23 -0500 Subject: [PATCH] Work in all scopes --- .../pylint/control_var_used_after_block.py | 13 +++---- .../rules/control_var_used_after_block.rs | 39 +++++++------------ 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/control_var_used_after_block.py b/crates/ruff_linter/resources/test/fixtures/pylint/control_var_used_after_block.py index 25b68dbd1e053..4b14cd5050180 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/control_var_used_after_block.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/control_var_used_after_block.py @@ -1,12 +1,11 @@ import typing from typing import cast -# TODO: Figure this out -# for global_var in []: -# _ = global_var -# pass +for global_var in []: + _ = global_var + pass -# _ = global_var +_ = global_var def foo(): # For control var used outside block @@ -16,7 +15,7 @@ def foo(): _ = event - # # Tuple destructuring + # Tuple destructuring for a, b, c in []: pass @@ -25,7 +24,7 @@ def foo(): _ = c - # # Array destructuring + # Array destructuring for [d, e, f] in []: pass diff --git a/crates/ruff_linter/src/rules/pylint/rules/control_var_used_after_block.rs b/crates/ruff_linter/src/rules/pylint/rules/control_var_used_after_block.rs index 36fb600cc8265..f4aec237a458b 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/control_var_used_after_block.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/control_var_used_after_block.rs @@ -109,33 +109,20 @@ pub(crate) fn control_var_used_after_block( // return; // } - println!("nodes {:#?}", checker.semantic().nodes()); + // println!("nodes {:#?}", checker.semantic().nodes()); for (name, binding) in scope .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() - // || binding.kind.is_with_item_var()) - // && (!binding.is_unpacked_assignment() || checker.settings.preview.is_enabled()) - // && !binding.is_nonlocal() - // && !binding.is_global() - // && !binding.is_used() - // && !checker.settings.dummy_variable_rgx.is_match(name) - // && !matches!( - // name, - // "__tracebackhide__" - // | "__traceback_info__" - // | "__traceback_supplement__" - // | "__debuggerskip__" - // ) - // { - // return Some((name, binding)); - // } - - // None - // }) + .filter_map(|(name, binding)| { + println!("name={:?} kind={:?}", name, binding.kind); + + if binding.kind.is_loop_var() || binding.kind.is_with_item_var() { + return Some((name, binding)); + } + + None + }) { println!("Binding {:?} {:?}", name, binding); // Find for-loop variable bindings @@ -175,7 +162,7 @@ pub(crate) fn control_var_used_after_block( "\t\tbinding_source_node_id={:?} binding_statement_id={:?} ancestor_node_id={:?}", binding_source_node_id, binding_statement_id, ancestor_node_id ); - if (binding_statement_id == ancestor_node_id) { + if binding_statement_id == ancestor_node_id { println!("\t\to"); found_match = true; break; @@ -184,7 +171,7 @@ pub(crate) fn control_var_used_after_block( } } - if (!found_match) { + if !found_match { println!("\t\temits error={:?}", reference_node_id); let block_kind = match binding_statement { Stmt::For(_) => BlockKind::For, @@ -197,7 +184,7 @@ pub(crate) fn control_var_used_after_block( diagnostics.push(Diagnostic::new( ControlVarUsedAfterBlock { control_var_name: name.to_owned(), - block_kind: block_kind, + block_kind, }, reference.range(), ))