mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Preserve trailing comments in C414 fixes (#7775)
Closes https://github.com/astral-sh/ruff/issues/7772.
This commit is contained in:
parent
a6ebbf21c3
commit
c040fac12f
3 changed files with 77 additions and 10 deletions
|
@ -32,3 +32,16 @@ sorted(sorted(x, key=lambda y: y))
|
||||||
sorted(sorted(x, key=lambda y: y), key=lambda x: x)
|
sorted(sorted(x, key=lambda y: y), key=lambda x: x)
|
||||||
sorted(sorted(x), reverse=True)
|
sorted(sorted(x), reverse=True)
|
||||||
sorted(sorted(x, reverse=False), 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 "",
|
||||||
|
)
|
||||||
|
|
|
@ -7,6 +7,7 @@ use libcst_native::{
|
||||||
RightCurlyBrace, RightParen, RightSquareBracket, Set, SetComp, SimpleString, SimpleWhitespace,
|
RightCurlyBrace, RightParen, RightSquareBracket, Set, SetComp, SimpleString, SimpleWhitespace,
|
||||||
TrailingWhitespace, Tuple,
|
TrailingWhitespace, Tuple,
|
||||||
};
|
};
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
use ruff_diagnostics::{Edit, Fix};
|
use ruff_diagnostics::{Edit, Fix};
|
||||||
use ruff_python_ast::Expr;
|
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() {
|
outer_call.args = match outer_call.args.split_first() {
|
||||||
Some((first, rest)) => {
|
Some((first, rest)) => {
|
||||||
let inner_call = match_call(&first.value)?;
|
let inner_call = match_call(&first.value)?;
|
||||||
inner_call
|
if let Some(arg) = inner_call
|
||||||
.args
|
.args
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|argument| argument.keyword.is_none())
|
.find(|argument| argument.keyword.is_none())
|
||||||
.take(1)
|
{
|
||||||
.chain(rest.iter())
|
let mut arg = arg.clone();
|
||||||
.cloned()
|
arg.comma = first.comma.clone();
|
||||||
.collect::<Vec<_>>()
|
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"),
|
None => bail!("Expected at least one argument in outer function call"),
|
||||||
};
|
};
|
||||||
|
|
|
@ -181,7 +181,7 @@ C414.py:10:1: C414 [*] Unnecessary `sorted` call within `set()`
|
||||||
8 8 | set(tuple(x))
|
8 8 | set(tuple(x))
|
||||||
9 9 | set(sorted(x))
|
9 9 | set(sorted(x))
|
||||||
10 |-set(sorted(x, key=lambda y: y))
|
10 |-set(sorted(x, key=lambda y: y))
|
||||||
10 |+set(x, )
|
10 |+set(x)
|
||||||
11 11 | set(reversed(x))
|
11 11 | set(reversed(x))
|
||||||
12 12 | sorted(list(x))
|
12 12 | sorted(list(x))
|
||||||
13 13 | sorted(tuple(x))
|
13 13 | sorted(tuple(x))
|
||||||
|
@ -378,11 +378,10 @@ C414.py:19:1: C414 [*] Unnecessary `list` call within `tuple()`
|
||||||
21 |- [x, 3, "hell"\
|
21 |- [x, 3, "hell"\
|
||||||
20 |+ [x, 3, "hell"\
|
20 |+ [x, 3, "hell"\
|
||||||
22 21 | "o"]
|
22 21 | "o"]
|
||||||
23 22 | )
|
23 |- )
|
||||||
24 |-)
|
24 22 | )
|
||||||
25 23 | set(set())
|
25 23 | set(set())
|
||||||
26 24 | set(list())
|
26 24 | set(list())
|
||||||
27 25 | set(tuple())
|
|
||||||
|
|
||||||
C414.py:25:1: C414 [*] Unnecessary `set` call within `set()`
|
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.
|
30 30 | # Nested sorts with differing keyword arguments. Not flagged.
|
||||||
31 31 | sorted(sorted(x, key=lambda y: y))
|
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 | )
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue