Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid PERF401 false positive on list access in loop #6220

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions crates/ruff/resources/test/fixtures/perflint/PERF401.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,18 @@ def f():
for i in items:
if i not in result:
result.append(i) # OK


def f():
fibonacci = [0, 1]
for i in range(20):
fibonacci.append(sum(fibonacci[-2:])) # OK
print(fibonacci)


def f():
foo = object()
foo.fibonacci = [0, 1]
for i in range(20):
foo.fibonacci.append(sum(foo.fibonacci[-2:])) # OK
print(foo.fibonacci)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ruff_python_ast::{self as ast, Expr, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::helpers::any_over_expr;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -129,6 +130,13 @@ pub(crate) fn manual_list_comprehension(checker: &mut Checker, target: &Expr, bo
return;
}

// Avoid, e.g., `for x in y: filtered.append(filtered[-1] * 2)`.
if any_over_expr(arg, &|expr| {
ComparableExpr::from(expr) == ComparableExpr::from(value)
}) {
return;
}

// Avoid if the value is used in the conditional test, e.g.,
//
// ```python
Expand Down
5 changes: 2 additions & 3 deletions crates/ruff/src/rules/perflint/rules/manual_list_copy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use ruff_python_ast::{self as ast, Expr, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::any_over_expr;
use ruff_python_ast::{self as ast, Expr, Stmt};

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -89,7 +88,7 @@ pub(crate) fn manual_list_copy(checker: &mut Checker, target: &Expr, body: &[Stm
return;
}

// Avoid, e.g., `for x in y: filtered[x].append(x * x)`.
// Avoid, e.g., `for x in y: filtered[x].append(x)`.
if any_over_expr(value, &|expr| {
expr.as_name_expr().map_or(false, |expr| expr.id == *id)
}) {
Expand Down
Loading