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
stop duplicating comments inside the fixed append
  • Loading branch information
w0nder1ng committed Nov 18, 2024
commit 46bd85114c7378dde8516bcf0df1f6ec193b8c16
11 changes: 11 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/perflint/PERF401.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,14 @@ def f():
for val in range(5):
result.append(val * 2) # Ok
print(val)

def f():
result = []
for i in range(2):
result.append(
(
i+1,
# Comment should not be duplicated
2
)
) # PERF401
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ fn convert_to_list_extend(
.comment_ranges()
.comments_in_range(range)
.iter()
// Ignore comments inside of the append, since these are preserved
.filter(|comment| !to_append.range().contains_range(**comment))
.map(|range| locator.slice(range).trim_whitespace_start())
.collect()
};
Expand Down Expand Up @@ -398,7 +400,6 @@ fn convert_to_list_extend(
.ok_or(anyhow!(
"Binding must have a statement to convert into a list comprehension"
))?;

let mut comments_to_move =
comment_strings_in_range(locator.full_lines_range(binding_stmt.range));
comments_to_move.extend(for_loop_inline_comments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,19 @@ PERF401.py:169:9: PERF401 Use a list comprehension to create a transformed list
170 | print(val)
|
= help: Replace for loop with list comprehension

PERF401.py:175:9: PERF401 Use a list comprehension to create a transformed list
|
173 | result = []
174 | for i in range(2):
175 | result.append(
| _________^
176 | | (
177 | | i+1,
178 | | # Comment should not be duplicated
179 | | 2
180 | | )
181 | | ) # PERF401
| |_________^ PERF401
|
= help: Replace for loop with list comprehension
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,37 @@ PERF401.py:169:9: PERF401 [*] Use a list comprehension to create a transformed l
169 |- result.append(val * 2) # Ok
167 |+ result = [val * 2 for val in range(5)] # Ok
170 168 | print(val)
171 169 |
172 170 | def f():

PERF401.py:175:9: PERF401 [*] Use a list comprehension to create a transformed list
|
173 | result = []
174 | for i in range(2):
175 | result.append(
| _________^
176 | | (
177 | | i+1,
178 | | # Comment should not be duplicated
179 | | 2
180 | | )
181 | | ) # PERF401
| |_________^ PERF401
|
= help: Replace for loop with list comprehension

ℹ Unsafe fix
170 170 | print(val)
171 171 |
172 172 | def f():
173 |- result = []
174 |- for i in range(2):
175 |- result.append(
176 |- (
173 |+ result = [(
177 174 | i+1,
178 175 | # Comment should not be duplicated
179 176 | 2
180 |- )
181 |- ) # PERF401
177 |+ ) for i in range(2)] # PERF401