Skip to content

Commit

Permalink
[perflint] fix invalid hoist in perf401 (#14369)
Browse files Browse the repository at this point in the history
Co-authored-by: Micha Reiser <[email protected]>
  • Loading branch information
w0nder1ng and MichaReiser authored Dec 12, 2024
1 parent 033ecf5 commit 2eac00c
Show file tree
Hide file tree
Showing 6 changed files with 863 additions and 203 deletions.
132 changes: 122 additions & 10 deletions crates/ruff_linter/resources/test/fixtures/perflint/PERF401.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,49 +74,57 @@ def f():
result.append(i) # Ok


def f():
async def f():
items = [1, 2, 3, 4]
result = []
async for i in items:
if i % 2:
result.append(i) # PERF401


def f():
async def f():
items = [1, 2, 3, 4]
result = []
async for i in items:
result.append(i) # PERF401


async def f():
items = [1, 2, 3, 4]
result = [1, 2]
async for i in items:
result.append(i) # PERF401


def f():
result, _ = [1,2,3,4], ...
result, _ = [1, 2, 3, 4], ...
for i in range(10):
result.append(i*2) # PERF401
result.append(i * 2) # PERF401


def f():
result = []
if True:
for i in range(10): # single-line comment 1 should be protected
# single-line comment 2 should be protected
if i % 2: # single-line comment 3 should be protected
result.append(i) # PERF401
if i % 2: # single-line comment 3 should be protected
result.append(i) # PERF401


def f():
result = [] # comment after assignment should be protected
result = [] # comment after assignment should be protected
for i in range(10): # single-line comment 1 should be protected
# single-line comment 2 should be protected
if i % 2: # single-line comment 3 should be protected
result.append(i) # PERF401
if i % 2: # single-line comment 3 should be protected
result.append(i) # PERF401


def f():
result = []
for i in range(10):
"""block comment stops the fix"""
result.append(i*2) # Ok
result.append(i * 2) # Ok


def f(param):
# PERF401
Expand All @@ -125,3 +133,107 @@ def f(param):
new_layers = []
for value in param:
new_layers.append(value * 3)


def f():
result = []
var = 1
for _ in range(10):
result.append(var + 1) # PERF401


def f():
# make sure that `tmp` is not deleted
tmp = 1; result = [] # commment should be protected
for i in range(10):
result.append(i + 1) # PERF401


def f():
# make sure that `tmp` is not deleted
result = []; tmp = 1 # commment should be protected
for i in range(10):
result.append(i + 1) # PERF401


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


def f():
result = []
result += [1]
for i in range(10):
result.append(i * 2) # PERF401


def f():
result = []
for val in range(5):
result.append(val * 2) # Ok
print(val)


def f():
result = []
for val in range(5):
result.append(val * 2) # PERF401
val = 1
print(val)


def f():
i = [1, 2, 3]
result = []
for i in i:
result.append(i + 1) # PERF401


def f():
result = []
for i in range( # Comment 1 should not be duplicated
(
2 # Comment 2
+ 1
)
): # Comment 3
if i % 2: # Comment 4
result.append(
(
i + 1,
# Comment 5
2,
)
) # PERF401


def f():
result: list[int] = []
for i in range(10):
result.append(i * 2) # PERF401


def f():
a, b = [1, 2, 3], [4, 5, 6]
result = []
for i in a, b:
result.append(i[0] + i[1]) # PERF401
return result


def f():
values = [1, 2, 3]
result = []
for a in values:
print(a)
for a in values:
result.append(a + 1) # PERF401
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub(crate) fn deferred_for_loops(checker: &mut Checker) {
let Stmt::For(stmt_for) = checker.semantic.current_statement() else {
unreachable!("Expected Stmt::For");
};

if checker.enabled(Rule::UnusedLoopControlVariable) {
flake8_bugbear::rules::unused_loop_control_variable(checker, stmt_for);
}
Expand All @@ -36,6 +35,9 @@ pub(crate) fn deferred_for_loops(checker: &mut Checker) {
if checker.enabled(Rule::DictIndexMissingItems) {
pylint::rules::dict_index_missing_items(checker, stmt_for);
}
if checker.enabled(Rule::ManualListComprehension) {
perflint::rules::manual_list_comprehension(checker, stmt_for);
}
}
}
}
4 changes: 1 addition & 3 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
Rule::UnnecessaryEnumerate,
Rule::UnusedLoopControlVariable,
Rule::YieldInForLoop,
Rule::ManualListComprehension,
]) {
checker.analyze.for_loops.push(checker.semantic.snapshot());
}
Expand All @@ -1390,9 +1391,6 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::DictIterMissingItems) {
pylint::rules::dict_iter_missing_items(checker, target, iter);
}
if checker.enabled(Rule::ManualListComprehension) {
perflint::rules::manual_list_comprehension(checker, for_stmt);
}
if checker.enabled(Rule::ManualListCopy) {
perflint::rules::manual_list_copy(checker, for_stmt);
}
Expand Down
Loading

0 comments on commit 2eac00c

Please sign in to comment.