From c040fac12f2022c74c59daa903d55aacca105740 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 3 Oct 2023 00:36:51 -0400 Subject: [PATCH] Preserve trailing comments in C414 fixes (#7775) Closes https://github.com/astral-sh/ruff/issues/7772. --- .../fixtures/flake8_comprehensions/C414.py | 13 +++++ .../src/rules/flake8_comprehensions/fixes.rs | 19 +++++-- ...8_comprehensions__tests__C414_C414.py.snap | 55 +++++++++++++++++-- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C414.py b/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C414.py index ad61eec08a6fd..7db605fecbe64 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C414.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C414.py @@ -32,3 +32,16 @@ sorted(sorted(x, key=lambda y: y), key=lambda x: x) sorted(sorted(x), reverse=True) sorted(sorted(x, reverse=False), reverse=True) + +# Preserve trailing comments. +xxxxxxxxxxx_xxxxx_xxxxx = sorted( + list(x_xxxx_xxxxxxxxxxx_xxxxx.xxxx()), + # xxxxxxxxxxx xxxxx xxxx xxx xx Nxxx, xxx xxxxxx3 xxxxxxxxx xx + # xx xxxx xxxxxxx xxxx xxx xxxxxxxx Nxxx + key=lambda xxxxx: xxxxx or "", +) + +xxxxxxxxxxx_xxxxx_xxxxx = sorted( + list(x_xxxx_xxxxxxxxxxx_xxxxx.xxxx()), # xxxxxxxxxxx xxxxx xxxx xxx xx Nxxx + key=lambda xxxxx: xxxxx or "", +) diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs index 2900d9353e000..b16a1371d69f3 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs @@ -7,6 +7,7 @@ use libcst_native::{ RightCurlyBrace, RightParen, RightSquareBracket, Set, SetComp, SimpleString, SimpleWhitespace, TrailingWhitespace, Tuple, }; +use std::iter; use ruff_diagnostics::{Edit, Fix}; use ruff_python_ast::Expr; @@ -823,14 +824,20 @@ 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)?; - inner_call + if let Some(arg) = inner_call .args .iter() - .filter(|argument| argument.keyword.is_none()) - .take(1) - .chain(rest.iter()) - .cloned() - .collect::>() + .find(|argument| argument.keyword.is_none()) + { + let mut arg = arg.clone(); + arg.comma = first.comma.clone(); + arg.whitespace_after_arg = first.whitespace_after_arg.clone(); + iter::once(arg) + .chain(rest.iter().cloned()) + .collect::>() + } else { + rest.to_vec() + } } None => bail!("Expected at least one argument in outer function call"), }; diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C414_C414.py.snap b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C414_C414.py.snap index d2cae0af64f84..396a8d26ab0eb 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C414_C414.py.snap +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C414_C414.py.snap @@ -181,7 +181,7 @@ C414.py:10:1: C414 [*] Unnecessary `sorted` call within `set()` 8 8 | set(tuple(x)) 9 9 | set(sorted(x)) 10 |-set(sorted(x, key=lambda y: y)) - 10 |+set(x, ) + 10 |+set(x) 11 11 | set(reversed(x)) 12 12 | sorted(list(x)) 13 13 | sorted(tuple(x)) @@ -378,11 +378,10 @@ C414.py:19:1: C414 [*] Unnecessary `list` call within `tuple()` 21 |- [x, 3, "hell"\ 20 |+ [x, 3, "hell"\ 22 21 | "o"] -23 22 | ) -24 |-) +23 |- ) +24 22 | ) 25 23 | set(set()) 26 24 | set(list()) -27 25 | set(tuple()) C414.py:25:1: C414 [*] Unnecessary `set` call within `set()` | @@ -467,4 +466,52 @@ C414.py:28:1: C414 [*] Unnecessary `reversed` call within `sorted()` 30 30 | # Nested sorts with differing keyword arguments. Not flagged. 31 31 | sorted(sorted(x, key=lambda y: y)) +C414.py:37:27: C414 [*] Unnecessary `list` call within `sorted()` + | +36 | # Preserve trailing comments. +37 | xxxxxxxxxxx_xxxxx_xxxxx = sorted( + | ___________________________^ +38 | | list(x_xxxx_xxxxxxxxxxx_xxxxx.xxxx()), +39 | | # xxxxxxxxxxx xxxxx xxxx xxx xx Nxxx, xxx xxxxxx3 xxxxxxxxx xx +40 | | # xx xxxx xxxxxxx xxxx xxx xxxxxxxx Nxxx +41 | | key=lambda xxxxx: xxxxx or "", +42 | | ) + | |_^ C414 +43 | +44 | xxxxxxxxxxx_xxxxx_xxxxx = sorted( + | + = help: Remove the inner `list` call + +ℹ Suggested fix +35 35 | +36 36 | # Preserve trailing comments. +37 37 | xxxxxxxxxxx_xxxxx_xxxxx = sorted( +38 |- list(x_xxxx_xxxxxxxxxxx_xxxxx.xxxx()), + 38 |+ x_xxxx_xxxxxxxxxxx_xxxxx.xxxx(), +39 39 | # xxxxxxxxxxx xxxxx xxxx xxx xx Nxxx, xxx xxxxxx3 xxxxxxxxx xx +40 40 | # xx xxxx xxxxxxx xxxx xxx xxxxxxxx Nxxx +41 41 | key=lambda xxxxx: xxxxx or "", + +C414.py:44:27: C414 [*] Unnecessary `list` call within `sorted()` + | +42 | ) +43 | +44 | xxxxxxxxxxx_xxxxx_xxxxx = sorted( + | ___________________________^ +45 | | list(x_xxxx_xxxxxxxxxxx_xxxxx.xxxx()), # xxxxxxxxxxx xxxxx xxxx xxx xx Nxxx +46 | | key=lambda xxxxx: xxxxx or "", +47 | | ) + | |_^ C414 + | + = help: Remove the inner `list` call + +ℹ Suggested fix +42 42 | ) +43 43 | +44 44 | xxxxxxxxxxx_xxxxx_xxxxx = sorted( +45 |- list(x_xxxx_xxxxxxxxxxx_xxxxx.xxxx()), # xxxxxxxxxxx xxxxx xxxx xxx xx Nxxx + 45 |+ x_xxxx_xxxxxxxxxxx_xxxxx.xxxx(), # xxxxxxxxxxx xxxxx xxxx xxx xx Nxxx +46 46 | key=lambda xxxxx: xxxxx or "", +47 47 | ) +