Skip to content

Commit aa96a05

Browse files
authored
[perflint] Clarify that PERF402 applies to any iterable (#26242)
First contribution here, hi! Picking up #21593 as a small intro PR. PERF402's current "What it does" says it catches copies of an "existing list", but the rule also fires whenever a `for` loop appends every item of any iterable to a list while the source isn't required to be a list (only the destination is). The issue reporter pointed this out and suggested wording closer to "for loops that can be replaced with `list()`", which is what I went with here. Scope is intentionally tiny: just the `## What it does` and `## Why is this bad?` doc comments. Violation message, example, rule logic, and tests are unchanged so no snapshots move. Closes #21593.
1 parent 67cbf61 commit aa96a05

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

crates/ruff_linter/src/rules/perflint/rules/manual_list_copy.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ use crate::Violation;
77
use crate::checkers::ast::Checker;
88

99
/// ## What it does
10-
/// Checks for `for` loops that can be replaced by a making a copy of a list.
10+
/// Checks for `for` loops that append every item of an iterable to a list,
11+
/// which can be replaced with a call to `list`.
1112
///
1213
/// ## Why is this bad?
13-
/// When creating a copy of an existing list using a for-loop, prefer
14-
/// `list` or `list.copy` instead. Making a direct copy is more readable and
15-
/// more performant.
14+
/// When populating a list from an iterable with a `for` loop, prefer `list`
15+
/// instead. The `list` call is more readable and more performant. For an
16+
/// existing list, you can also use `list.copy` instead of `list`.
1617
///
1718
/// Using the below as an example, the `list`-based copy is ~2x faster on
1819
/// Python 3.11.

0 commit comments

Comments
 (0)