Skip to content

Commit

Permalink
Preserve trailing comments in C414 fixes (#7775)
Browse files Browse the repository at this point in the history
Closes #7772.
  • Loading branch information
charliermarsh committed Oct 3, 2023
1 parent a6ebbf2 commit c040fac
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "",
)
19 changes: 13 additions & 6 deletions crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Vec<_>>()
.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::<Vec<_>>()
} else {
rest.to_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 @@ -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))
Expand Down Expand Up @@ -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()`
|
Expand Down Expand Up @@ -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 | )


0 comments on commit c040fac

Please sign in to comment.