Skip to content

Commit

Permalink
Accept empty inner calls in C414 (#6725)
Browse files Browse the repository at this point in the history
Closes #6716.
  • Loading branch information
charliermarsh authored Aug 21, 2023
1 parent 3c2dd5e commit be96e00
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"o"]
)
)
set(set())
set(list())
set(tuple())
sorted(reversed())

# Nested sorts with differing keyword arguments. Not flagged.
sorted(sorted(x, key=lambda y: y))
Expand Down
15 changes: 8 additions & 7 deletions crates/ruff/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,13 +765,14 @@ pub(crate) fn fix_unnecessary_double_cast_or_process(
outer_call.args = match outer_call.args.split_first() {
Some((first, rest)) => {
let inner_call = match_call(&first.value)?;
if let Some(iterable) = inner_call.args.first() {
let mut args = vec![iterable.clone()];
args.extend_from_slice(rest);
args
} else {
bail!("Expected at least one argument in inner function call");
}
inner_call
.args
.iter()
.filter(|argument| argument.keyword.is_none())
.take(1)
.chain(rest.iter())
.cloned()
.collect::<Vec<_>>()
}
None => bail!("Expected at least one argument in outer function call"),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ C414.py:19:1: C414 [*] Unnecessary `list` call within `tuple()`
23 | | )
24 | | )
| |_^ C414
25 |
26 | # Nested sorts with differing keyword arguments. Not flagged.
25 | set(set())
26 | set(list())
|
= help: Remove the inner `list` call

Expand All @@ -380,8 +380,91 @@ C414.py:19:1: C414 [*] Unnecessary `list` call within `tuple()`
22 21 | "o"]
23 22 | )
24 |-)
25 23 |
26 24 | # Nested sorts with differing keyword arguments. Not flagged.
27 25 | sorted(sorted(x, key=lambda y: y))
25 23 | set(set())
26 24 | set(list())
27 25 | set(tuple())

C414.py:25:1: C414 [*] Unnecessary `set` call within `set()`
|
23 | )
24 | )
25 | set(set())
| ^^^^^^^^^^ C414
26 | set(list())
27 | set(tuple())
|
= help: Remove the inner `set` call

Suggested fix
22 22 | "o"]
23 23 | )
24 24 | )
25 |-set(set())
25 |+set()
26 26 | set(list())
27 27 | set(tuple())
28 28 | sorted(reversed())

C414.py:26:1: C414 [*] Unnecessary `list` call within `set()`
|
24 | )
25 | set(set())
26 | set(list())
| ^^^^^^^^^^^ C414
27 | set(tuple())
28 | sorted(reversed())
|
= help: Remove the inner `list` call

Suggested fix
23 23 | )
24 24 | )
25 25 | set(set())
26 |-set(list())
26 |+set()
27 27 | set(tuple())
28 28 | sorted(reversed())
29 29 |

C414.py:27:1: C414 [*] Unnecessary `tuple` call within `set()`
|
25 | set(set())
26 | set(list())
27 | set(tuple())
| ^^^^^^^^^^^^ C414
28 | sorted(reversed())
|
= help: Remove the inner `tuple` call

Suggested fix
24 24 | )
25 25 | set(set())
26 26 | set(list())
27 |-set(tuple())
27 |+set()
28 28 | sorted(reversed())
29 29 |
30 30 | # Nested sorts with differing keyword arguments. Not flagged.

C414.py:28:1: C414 [*] Unnecessary `reversed` call within `sorted()`
|
26 | set(list())
27 | set(tuple())
28 | sorted(reversed())
| ^^^^^^^^^^^^^^^^^^ C414
29 |
30 | # Nested sorts with differing keyword arguments. Not flagged.
|
= help: Remove the inner `reversed` call

Suggested fix
25 25 | set(set())
26 26 | set(list())
27 27 | set(tuple())
28 |-sorted(reversed())
28 |+sorted()
29 29 |
30 30 | # Nested sorts with differing keyword arguments. Not flagged.
31 31 | sorted(sorted(x, key=lambda y: y))


0 comments on commit be96e00

Please sign in to comment.