diff --git a/crates/ruff/src/rules/ruff/rules/quadratic_list_summation.rs b/crates/ruff/src/rules/ruff/rules/quadratic_list_summation.rs index 21966e5778777..427f1d052c6cd 100644 --- a/crates/ruff/src/rules/ruff/rules/quadratic_list_summation.rs +++ b/crates/ruff/src/rules/ruff/rules/quadratic_list_summation.rs @@ -119,11 +119,11 @@ fn func_is_builtin(func: &Expr, name: &str, semantic: &SemanticModel) -> bool { /// Returns `true` if the `start` argument to a `sum()` call is an empty list. fn start_is_empty_list(arguments: &Arguments, semantic: &SemanticModel) -> bool { - let Some(keyword) = arguments.find_keyword("start") else { + let Some(start_arg) = arguments.find_argument("start", 1) else { return false; }; - match &keyword.value { + match start_arg { Expr::Call(ast::ExprCall { func, arguments, .. }) => arguments.is_empty() && func_is_builtin(func, "list", semantic), diff --git a/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF017_RUF017.py.snap b/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF017_RUF017.py.snap index 161fa644c1986..b05d32ea23020 100644 --- a/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF017_RUF017.py.snap +++ b/crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF017_RUF017.py.snap @@ -24,6 +24,31 @@ RUF017.py:5:1: RUF017 [*] Avoid quadratic list summation 7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[]) 8 10 | sum([[1, 2, 3], [4, 5, 6]], []) +RUF017.py:6:1: RUF017 [*] Avoid quadratic list summation + | +4 | # RUF017 +5 | sum([x, y], start=[]) +6 | sum([x, y], []) + | ^^^^^^^^^^^^^^^ RUF017 +7 | sum([[1, 2, 3], [4, 5, 6]], start=[]) +8 | sum([[1, 2, 3], [4, 5, 6]], []) + | + = help: Replace with `functools.reduce` + +ℹ Suggested fix + 1 |+import functools + 2 |+import operator +1 3 | x = [1, 2, 3] +2 4 | y = [4, 5, 6] +3 5 | +4 6 | # RUF017 +5 7 | sum([x, y], start=[]) +6 |-sum([x, y], []) + 8 |+functools.reduce(operator.iadd, [x, y], []) +7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[]) +8 10 | sum([[1, 2, 3], [4, 5, 6]], []) +9 11 | sum([[1, 2, 3], [4, 5, 6]], + RUF017.py:7:1: RUF017 [*] Avoid quadratic list summation | 5 | sum([x, y], start=[]) @@ -50,4 +75,60 @@ RUF017.py:7:1: RUF017 [*] Avoid quadratic list summation 9 11 | sum([[1, 2, 3], [4, 5, 6]], 10 12 | []) +RUF017.py:8:1: RUF017 [*] Avoid quadratic list summation + | + 6 | sum([x, y], []) + 7 | sum([[1, 2, 3], [4, 5, 6]], start=[]) + 8 | sum([[1, 2, 3], [4, 5, 6]], []) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF017 + 9 | sum([[1, 2, 3], [4, 5, 6]], +10 | []) + | + = help: Replace with `functools.reduce` + +ℹ Suggested fix + 1 |+import functools + 2 |+import operator +1 3 | x = [1, 2, 3] +2 4 | y = [4, 5, 6] +3 5 | +-------------------------------------------------------------------------------- +5 7 | sum([x, y], start=[]) +6 8 | sum([x, y], []) +7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[]) +8 |-sum([[1, 2, 3], [4, 5, 6]], []) + 10 |+functools.reduce(operator.iadd, [[1, 2, 3], [4, 5, 6]], []) +9 11 | sum([[1, 2, 3], [4, 5, 6]], +10 12 | []) +11 13 | + +RUF017.py:9:1: RUF017 [*] Avoid quadratic list summation + | + 7 | sum([[1, 2, 3], [4, 5, 6]], start=[]) + 8 | sum([[1, 2, 3], [4, 5, 6]], []) + 9 | / sum([[1, 2, 3], [4, 5, 6]], +10 | | []) + | |_______^ RUF017 +11 | +12 | # OK + | + = help: Replace with `functools.reduce` + +ℹ Suggested fix + 1 |+import functools + 2 |+import operator +1 3 | x = [1, 2, 3] +2 4 | y = [4, 5, 6] +3 5 | +-------------------------------------------------------------------------------- +6 8 | sum([x, y], []) +7 9 | sum([[1, 2, 3], [4, 5, 6]], start=[]) +8 10 | sum([[1, 2, 3], [4, 5, 6]], []) +9 |-sum([[1, 2, 3], [4, 5, 6]], +10 |- []) + 11 |+functools.reduce(operator.iadd, [[1, 2, 3], [4, 5, 6]], []) +11 12 | +12 13 | # OK +13 14 | sum([x, y]) +