Skip to content

Commit c0fb235

Browse files
authored
[flake8-comprehensions] Preserve trailing commas for single-element lists (C409) (#19571)
## Summary Fixes #19568
1 parent b5a3503 commit c0fb235

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C409.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@
4242
x for x in [1,2,3]
4343
}
4444
)
45+
46+
t9 = tuple([1],)
47+
t10 = tuple([1, 2],)

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub(crate) fn unnecessary_literal_within_tuple_call(
124124
let needs_trailing_comma = if let [item] = elts.as_slice() {
125125
SimpleTokenizer::new(
126126
checker.locator().contents(),
127-
TextRange::new(item.end(), call.end()),
127+
TextRange::new(item.end(), argument.end()),
128128
)
129129
.all(|token| token.kind != SimpleTokenKind::Comma)
130130
} else {

crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C409_C409.py.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,36 @@ help: Rewrite as a tuple literal
247247
28 | tuple([x for x in range(5)])
248248
29 | tuple({x for x in range(10)})
249249
note: This is an unsafe fix and may change runtime behavior
250+
251+
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
252+
--> C409.py:46:6
253+
|
254+
44 | )
255+
45 |
256+
46 | t9 = tuple([1],)
257+
| ^^^^^^^^^^^
258+
47 | t10 = tuple([1, 2],)
259+
|
260+
help: Rewrite as a tuple literal
261+
43 | }
262+
44 | )
263+
45 |
264+
- t9 = tuple([1],)
265+
46 + t9 = (1,)
266+
47 | t10 = tuple([1, 2],)
267+
note: This is an unsafe fix and may change runtime behavior
268+
269+
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
270+
--> C409.py:47:7
271+
|
272+
46 | t9 = tuple([1],)
273+
47 | t10 = tuple([1, 2],)
274+
| ^^^^^^^^^^^^^^
275+
|
276+
help: Rewrite as a tuple literal
277+
44 | )
278+
45 |
279+
46 | t9 = tuple([1],)
280+
- t10 = tuple([1, 2],)
281+
47 + t10 = (1, 2)
282+
note: This is an unsafe fix and may change runtime behavior

crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__preview__C409_C409.py.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,36 @@ help: Rewrite as a generator
344344
42 | x for x in [1,2,3]
345345
43 | }
346346
note: This is an unsafe fix and may change runtime behavior
347+
348+
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
349+
--> C409.py:46:6
350+
|
351+
44 | )
352+
45 |
353+
46 | t9 = tuple([1],)
354+
| ^^^^^^^^^^^
355+
47 | t10 = tuple([1, 2],)
356+
|
357+
help: Rewrite as a tuple literal
358+
43 | }
359+
44 | )
360+
45 |
361+
- t9 = tuple([1],)
362+
46 + t9 = (1,)
363+
47 | t10 = tuple([1, 2],)
364+
note: This is an unsafe fix and may change runtime behavior
365+
366+
C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal)
367+
--> C409.py:47:7
368+
|
369+
46 | t9 = tuple([1],)
370+
47 | t10 = tuple([1, 2],)
371+
| ^^^^^^^^^^^^^^
372+
|
373+
help: Rewrite as a tuple literal
374+
44 | )
375+
45 |
376+
46 | t9 = tuple([1],)
377+
- t10 = tuple([1, 2],)
378+
47 + t10 = (1, 2)
379+
note: This is an unsafe fix and may change runtime behavior

0 commit comments

Comments
 (0)