Skip to content

Commit

Permalink
Avoid PERF401 false positive on list access in loop
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 1, 2023
1 parent 38b5726 commit ce197b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
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

0 comments on commit ce197b5

Please sign in to comment.