Skip to content

Commit

Permalink
Use any_over_body
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 30, 2024
1 parent bf40a37 commit c522aaa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
for color in colors:
colors.remove("red")

odds = {1, 3, 5}
for num in odds:
if num > 1:
odds.add(num + 1)

# OK

nums = {1, 2, 3}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Expr, Stmt, StmtFor};
use ruff_python_ast::helpers::any_over_body;
use ruff_python_ast::{self as ast, Expr, StmtFor};
use ruff_python_semantic::analyze::typing::is_set;
use ruff_text_size::Ranged;

Expand Down Expand Up @@ -69,19 +70,8 @@ pub(crate) fn modified_iterating_set(checker: &mut Checker, for_stmt: &StmtFor)
return;
}

if for_stmt.body.iter().any(|stmt| {
// name_of_set.modify_method()
// ^---------^ ^-----------^
// value attr
// ^-----------------------^
// func
// ^-------------------------^
// expr, stmt
let Stmt::Expr(ast::StmtExpr { value: expr, .. }) = stmt else {
return false;
};

let Some(func) = expr.as_call_expr().map(|exprcall| &exprcall.func) else {
if any_over_body(&for_stmt.body, &|expr| {
let Some(func) = expr.as_call_expr().map(|call| &call.func) else {
return false;
};

Expand All @@ -93,11 +83,11 @@ pub(crate) fn modified_iterating_set(checker: &mut Checker, for_stmt: &StmtFor)
return false;
};

let Some(binding_id_value) = checker.semantic().only_binding(value) else {
let Some(value_id) = checker.semantic().only_binding(value) else {
return false;
};

binding_id == binding_id_value && modifies_set(attr.as_str())
binding_id == value_id && modifies_set(attr.as_str())
}) {
let mut diagnostic = Diagnostic::new(
ModifiedIteratingSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ modified_iterating_set.py:20:1: PLE4703 [*] Iterated set `colors` is modified wi
21 | | colors.remove("red")
| |________________________^ PLE4703
22 |
23 | # OK
23 | odds = {1, 3, 5}
|
= help: Iterate over a copy of `colors`

Expand All @@ -104,4 +104,26 @@ modified_iterating_set.py:20:1: PLE4703 [*] Iterated set `colors` is modified wi
20 |+for color in colors.copy():
21 21 | colors.remove("red")
22 22 |
23 23 | # OK
23 23 | odds = {1, 3, 5}

modified_iterating_set.py:24:1: PLE4703 [*] Iterated set `odds` is modified within the `for` loop
|
23 | odds = {1, 3, 5}
24 | / for num in odds:
25 | | if num > 1:
26 | | odds.add(num + 1)
| |_________________________^ PLE4703
27 |
28 | # OK
|
= help: Iterate over a copy of `odds`

Unsafe fix
21 21 | colors.remove("red")
22 22 |
23 23 | odds = {1, 3, 5}
24 |-for num in odds:
24 |+for num in odds.copy():
25 25 | if num > 1:
26 26 | odds.add(num + 1)
27 27 |

0 comments on commit c522aaa

Please sign in to comment.