Skip to content

Commit

Permalink
Apply RUF017 when start is passed via position (#6664)
Browse files Browse the repository at this point in the history
As discussed in
#6489 (comment).
Linking #5073
  • Loading branch information
hauntsaninja authored Aug 18, 2023
1 parent 5892c69 commit a128fe5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/ruff/rules/quadratic_list_summation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])
Expand All @@ -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])


0 comments on commit a128fe5

Please sign in to comment.