diff --git a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py index 736bdaff0c..56a1f6cfc8 100644 --- a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py +++ b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py @@ -16,3 +16,6 @@ def f(x): return x print(f'Hello {dict((x,f(x)) for x in "abc")} World') + +# Regression test for: https://github.com/astral-sh/ruff/issues/7086 +dict((k,v)for k,v in d.iteritems() if k in only_args) diff --git a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs index d895f339d0..ce972cdc2e 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs @@ -110,10 +110,20 @@ pub(crate) fn fix_unnecessary_generator_dict(checker: &Checker, expr: &Expr) -> bail!("Expected tuple to contain two elements"); }; + // Insert whitespace before the `for`, since we're removing parentheses, as in: + // ```python + // dict((x, x)for x in range(3)) + // ``` + let mut for_in = generator_exp.for_in.clone(); + if for_in.whitespace_before == ParenthesizableWhitespace::default() { + for_in.whitespace_before = + ParenthesizableWhitespace::SimpleWhitespace(SimpleWhitespace(" ")); + } + tree = Expression::DictComp(Box::new(DictComp { key: Box::new(key.clone()), value: Box::new(value.clone()), - for_in: generator_exp.for_in.clone(), + for_in, lbrace: LeftCurlyBrace { whitespace_after: call.whitespace_before_args.clone(), }, diff --git a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap index a0733820e6..8fe86d0663 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap +++ b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap @@ -231,6 +231,8 @@ C402.py:18:16: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension 17 | 18 | print(f'Hello {dict((x,f(x)) for x in "abc")} World') | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C402 +19 | +20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086 | = help: Rewrite as a `dict` comprehension @@ -240,5 +242,23 @@ C402.py:18:16: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension 17 17 | 18 |-print(f'Hello {dict((x,f(x)) for x in "abc")} World') 18 |+print(f'Hello { {x: f(x) for x in "abc"} } World') +19 19 | +20 20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086 +21 21 | dict((k,v)for k,v in d.iteritems() if k in only_args) + +C402.py:21:1: C402 [*] Unnecessary generator (rewrite as a `dict` comprehension) + | +20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086 +21 | dict((k,v)for k,v in d.iteritems() if k in only_args) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C402 + | + = help: Rewrite as a `dict` comprehension + +ℹ Suggested fix +18 18 | print(f'Hello {dict((x,f(x)) for x in "abc")} World') +19 19 | +20 20 | # Regression test for: https://github.com/astral-sh/ruff/issues/7086 +21 |-dict((k,v)for k,v in d.iteritems() if k in only_args) + 21 |+{k: v for k,v in d.iteritems() if k in only_args}