diff --git a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C414.py b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C414.py index 29fb74661ace6..ad61eec08a6fd 100644 --- a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C414.py +++ b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C414.py @@ -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)) diff --git a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs index e366c4a2de063..952d6bf6f1c62 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs @@ -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::>() } None => bail!("Expected at least one argument in outer function call"), }; diff --git a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C414_C414.py.snap b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C414_C414.py.snap index fb2d7cefc930f..2aabf2b2f96c4 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C414_C414.py.snap +++ b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C414_C414.py.snap @@ -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 @@ -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))