Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aafb689
fix invalid hoist
w0nder1ng Nov 15, 2024
fd71328
tentative fix
w0nder1ng Nov 15, 2024
f30118e
don't use a comprehension when the list is referenced
w0nder1ng Nov 15, 2024
238d17a
check for external references to the for loop variable
w0nder1ng Nov 18, 2024
46bd851
stop duplicating comments inside the fixed append
w0nder1ng Nov 18, 2024
9a336fd
fix annotated returns, but lose type info
w0nder1ng Nov 18, 2024
63b0e1e
apply fix to type-annotated lists
w0nder1ng Nov 18, 2024
527a050
fix async extends, implicit tuples, and comments in iterator
w0nder1ng Dec 3, 2024
f339d70
stop deleting semicolon statements in the same line as binding
w0nder1ng Dec 6, 2024
f6ccb39
move manual list comprehension to deferred check
w0nder1ng Dec 6, 2024
6a98542
add debugging code to show binding
w0nder1ng Dec 6, 2024
e2eec2e
find the right binding for the loop variable
w0nder1ng Dec 9, 2024
9d41fb7
check that the correct for loop is picked
w0nder1ng Dec 9, 2024
104b69e
don't stop fix if a reference to target is actually another binding
w0nder1ng Dec 9, 2024
e44fcaa
allow shadowed bindings from any scope
w0nder1ng Dec 9, 2024
4696878
simplify check for loop variable usages
w0nder1ng Dec 10, 2024
74afdb0
cargo fmt
w0nder1ng Dec 10, 2024
db8d0d6
Few simplifications
MichaReiser Dec 11, 2024
77a0290
Merge branch 'main' into perf401_invalid_hoist
w0nder1ng Dec 11, 2024
c9b394c
fix merge
w0nder1ng Dec 11, 2024
9c55f1b
replace tokenization with simple character iterator
w0nder1ng Dec 11, 2024
523808d
Use simple tokenizer
MichaReiser Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
don't use a comprehension when the list is referenced
  • Loading branch information
w0nder1ng committed Nov 15, 2024
commit f30118e7e754f3ca8baf7c900121005a9a5ab388
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,10 @@ def f():
result = [] # comment should be protected
for i in range(10):
result.append(i*2) # PERF401


def f():
result = []
result.append(1)
for i in range(10):
result.append(i*2) # PERF401
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ pub(crate) fn manual_list_comprehension(checker: &mut Checker, for_stmt: &ast::S
}
};

// If the binding gets used in between the assignment and the for loop, a list comprehension is no longer safe
let binding_unused_between = binding_stmt.is_some_and(|binding_stmt| {
let from_assign_to_loop = binding_stmt.range.cover(for_stmt.range);
// count the number of references between the assignment and the for loop
let count = binding
.references()
.map(|ref_id| checker.semantic().reference(ref_id).range())
.filter(|text_range| from_assign_to_loop.contains_range(*text_range))
.count();
// if there's more than one, then it's been accessed in the middle somewhere, so it's not safe to change into a list comprehension
count < 2
});

// If the binding has multiple statements on its line, it gets more complicated to fix, so we use an extend
// TODO: make this work
let binding_only_stmt_on_line = binding_stmt.is_some_and(|_binding_stmt| true);
Expand All @@ -258,6 +271,7 @@ pub(crate) fn manual_list_comprehension(checker: &mut Checker, for_stmt: &ast::S
&& assignment_in_same_statement
&& binding_has_one_target
&& binding_only_stmt_on_line
&& binding_unused_between
{
ComprehensionType::ListComprehension
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ PERF401.py:151:9: PERF401 Use a list comprehension to create a transformed list
| ^^^^^^^^^^^^^^^^^^ PERF401
|
= help: Replace for loop with list comprehension

PERF401.py:158:9: PERF401 Use `list.extend` to create a transformed list
|
156 | result.append(1)
157 | for i in range(10):
158 | result.append(i*2) # PERF401
| ^^^^^^^^^^^^^^^^^^ PERF401
|
= help: Replace for loop with list.extend
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,23 @@ PERF401.py:151:9: PERF401 [*] Use a list comprehension to create a transformed l
151 |- result.append(i*2) # PERF401
149 |+ # comment should be protected
150 |+ result = [i*2 for i in range(10)] # PERF401
152 151 |
153 152 |
154 153 | def f():

PERF401.py:158:9: PERF401 [*] Use `list.extend` to create a transformed list
|
156 | result.append(1)
157 | for i in range(10):
158 | result.append(i*2) # PERF401
| ^^^^^^^^^^^^^^^^^^ PERF401
|
= help: Replace for loop with list.extend

ℹ Unsafe fix
154 154 | def f():
155 155 | result = []
156 156 | result.append(1)
157 |- for i in range(10):
158 |- result.append(i*2) # PERF401
157 |+ result.extend(i*2 for i in range(10)) # PERF401