From c0fb235a703684bca1cc1767015bb7e19ab1c22f Mon Sep 17 00:00:00 2001 From: Dan Parizher <105245560+danparizher@users.noreply.github.com> Date: Fri, 19 Sep 2025 09:27:14 -0400 Subject: [PATCH] [`flake8-comprehensions`] Preserve trailing commas for single-element lists (`C409`) (#19571) ## Summary Fixes #19568 --- .../fixtures/flake8_comprehensions/C409.py | 3 ++ .../unnecessary_literal_within_tuple_call.rs | 2 +- ...8_comprehensions__tests__C409_C409.py.snap | 33 +++++++++++++++++++ ...ensions__tests__preview__C409_C409.py.snap | 33 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C409.py b/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C409.py index c2c093b253..b8c72773d6 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C409.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C409.py @@ -42,3 +42,6 @@ tuple( x for x in [1,2,3] } ) + +t9 = tuple([1],) +t10 = tuple([1, 2],) diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs index b2d3af263f..30a250e7ce 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs @@ -124,7 +124,7 @@ pub(crate) fn unnecessary_literal_within_tuple_call( let needs_trailing_comma = if let [item] = elts.as_slice() { SimpleTokenizer::new( checker.locator().contents(), - TextRange::new(item.end(), call.end()), + TextRange::new(item.end(), argument.end()), ) .all(|token| token.kind != SimpleTokenKind::Comma) } else { diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C409_C409.py.snap b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C409_C409.py.snap index f05149dffd..f8c7534525 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C409_C409.py.snap +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C409_C409.py.snap @@ -247,3 +247,36 @@ help: Rewrite as a tuple literal 28 | tuple([x for x in range(5)]) 29 | tuple({x for x in range(10)}) note: This is an unsafe fix and may change runtime behavior + +C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal) + --> C409.py:46:6 + | +44 | ) +45 | +46 | t9 = tuple([1],) + | ^^^^^^^^^^^ +47 | t10 = tuple([1, 2],) + | +help: Rewrite as a tuple literal +43 | } +44 | ) +45 | + - t9 = tuple([1],) +46 + t9 = (1,) +47 | t10 = tuple([1, 2],) +note: This is an unsafe fix and may change runtime behavior + +C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal) + --> C409.py:47:7 + | +46 | t9 = tuple([1],) +47 | t10 = tuple([1, 2],) + | ^^^^^^^^^^^^^^ + | +help: Rewrite as a tuple literal +44 | ) +45 | +46 | t9 = tuple([1],) + - t10 = tuple([1, 2],) +47 + t10 = (1, 2) +note: This is an unsafe fix and may change runtime behavior diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__preview__C409_C409.py.snap b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__preview__C409_C409.py.snap index fbf5aa8fcd..23657a1310 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__preview__C409_C409.py.snap +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__preview__C409_C409.py.snap @@ -344,3 +344,36 @@ help: Rewrite as a generator 42 | x for x in [1,2,3] 43 | } note: This is an unsafe fix and may change runtime behavior + +C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal) + --> C409.py:46:6 + | +44 | ) +45 | +46 | t9 = tuple([1],) + | ^^^^^^^^^^^ +47 | t10 = tuple([1, 2],) + | +help: Rewrite as a tuple literal +43 | } +44 | ) +45 | + - t9 = tuple([1],) +46 + t9 = (1,) +47 | t10 = tuple([1, 2],) +note: This is an unsafe fix and may change runtime behavior + +C409 [*] Unnecessary list literal passed to `tuple()` (rewrite as a tuple literal) + --> C409.py:47:7 + | +46 | t9 = tuple([1],) +47 | t10 = tuple([1, 2],) + | ^^^^^^^^^^^^^^ + | +help: Rewrite as a tuple literal +44 | ) +45 | +46 | t9 = tuple([1],) + - t10 = tuple([1, 2],) +47 + t10 = (1, 2) +note: This is an unsafe fix and may change runtime behavior