mirror of https://github.com/astral-sh/ruff
[`perflint`] Parenthesize generator expressions (`PERF401`) (#19325)
## Summary closes #19204 ## Test Plan 1. test case is added in dedicated file 2. locally tested the code manually --------- Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com> Co-authored-by: CodeMan62 <sharmahimanshu150082007@gmail.com>
This commit is contained in:
parent
ba629fe262
commit
1dcef1a011
|
|
@ -278,3 +278,15 @@ def f():
|
|||
for i in src:
|
||||
if lambda: 0:
|
||||
dst.append(i)
|
||||
|
||||
def f():
|
||||
i = "xyz"
|
||||
result = []
|
||||
for i in range(3):
|
||||
result.append(x for x in [i])
|
||||
|
||||
def f():
|
||||
i = "xyz"
|
||||
result = []
|
||||
for i in range(3):
|
||||
result.append((x for x in [i]))
|
||||
|
|
@ -406,7 +406,14 @@ fn convert_to_list_extend(
|
|||
};
|
||||
let target_str = locator.slice(for_stmt.target.range());
|
||||
let elt_str = locator.slice(to_append);
|
||||
let generator_str = format!("{elt_str} {for_type} {target_str} in {for_iter_str}{if_str}");
|
||||
let generator_str = if to_append
|
||||
.as_generator_expr()
|
||||
.is_some_and(|generator| !generator.parenthesized)
|
||||
{
|
||||
format!("({elt_str}) {for_type} {target_str} in {for_iter_str}{if_str}")
|
||||
} else {
|
||||
format!("{elt_str} {for_type} {target_str} in {for_iter_str}{if_str}")
|
||||
};
|
||||
|
||||
let variable_name = locator.slice(binding);
|
||||
let for_loop_inline_comments = comment_strings_in_range(
|
||||
|
|
|
|||
|
|
@ -241,5 +241,27 @@ PERF401.py:280:13: PERF401 Use `list.extend` to create a transformed list
|
|||
279 | if lambda: 0:
|
||||
280 | dst.append(i)
|
||||
| ^^^^^^^^^^^^^ PERF401
|
||||
281 |
|
||||
282 | def f():
|
||||
|
|
||||
= help: Replace for loop with list.extend
|
||||
|
||||
PERF401.py:286:9: PERF401 Use a list comprehension to create a transformed list
|
||||
|
|
||||
284 | result = []
|
||||
285 | for i in range(3):
|
||||
286 | result.append(x for x in [i])
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PERF401
|
||||
287 |
|
||||
288 | def f():
|
||||
|
|
||||
= help: Replace for loop with list comprehension
|
||||
|
||||
PERF401.py:292:9: PERF401 Use a list comprehension to create a transformed list
|
||||
|
|
||||
290 | result = []
|
||||
291 | for i in range(3):
|
||||
292 | result.append((x for x in [i]))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PERF401
|
||||
|
|
||||
= help: Replace for loop with list comprehension
|
||||
|
|
|
|||
|
|
@ -566,6 +566,8 @@ PERF401.py:280:13: PERF401 [*] Use `list.extend` to create a transformed list
|
|||
279 | if lambda: 0:
|
||||
280 | dst.append(i)
|
||||
| ^^^^^^^^^^^^^ PERF401
|
||||
281 |
|
||||
282 | def f():
|
||||
|
|
||||
= help: Replace for loop with list.extend
|
||||
|
||||
|
|
@ -577,3 +579,47 @@ PERF401.py:280:13: PERF401 [*] Use `list.extend` to create a transformed list
|
|||
279 |- if lambda: 0:
|
||||
280 |- dst.append(i)
|
||||
278 |+ dst.extend(i for i in src if (lambda: 0))
|
||||
281 279 |
|
||||
282 280 | def f():
|
||||
283 281 | i = "xyz"
|
||||
|
||||
PERF401.py:286:9: PERF401 [*] Use a list comprehension to create a transformed list
|
||||
|
|
||||
284 | result = []
|
||||
285 | for i in range(3):
|
||||
286 | result.append(x for x in [i])
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PERF401
|
||||
287 |
|
||||
288 | def f():
|
||||
|
|
||||
= help: Replace for loop with list comprehension
|
||||
|
||||
ℹ Unsafe fix
|
||||
281 281 |
|
||||
282 282 | def f():
|
||||
283 283 | i = "xyz"
|
||||
284 |- result = []
|
||||
285 |- for i in range(3):
|
||||
286 |- result.append(x for x in [i])
|
||||
284 |+ result = [(x for x in [i]) for i in range(3)]
|
||||
287 285 |
|
||||
288 286 | def f():
|
||||
289 287 | i = "xyz"
|
||||
|
||||
PERF401.py:292:9: PERF401 [*] Use a list comprehension to create a transformed list
|
||||
|
|
||||
290 | result = []
|
||||
291 | for i in range(3):
|
||||
292 | result.append((x for x in [i]))
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PERF401
|
||||
|
|
||||
= help: Replace for loop with list comprehension
|
||||
|
||||
ℹ Unsafe fix
|
||||
287 287 |
|
||||
288 288 | def f():
|
||||
289 289 | i = "xyz"
|
||||
290 |- result = []
|
||||
291 |- for i in range(3):
|
||||
292 |- result.append((x for x in [i]))
|
||||
290 |+ result = [(x for x in [i]) for i in range(3)]
|
||||
|
|
|
|||
Loading…
Reference in New Issue