diff --git a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C404.py b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C404.py index a4a6d67bc9..3ad9545844 100644 --- a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C404.py +++ b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C404.py @@ -11,3 +11,6 @@ f"{dict([(s,f(s)) for s in 'ab'])}" f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}' f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' + +# Regression test for: https://github.com/astral-sh/ruff/issues/7087 +saved.append(dict([(k, v)for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]])) diff --git a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs index e13404ead2..b0d46e81e5 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs @@ -212,10 +212,20 @@ pub(crate) fn fix_unnecessary_list_comprehension_dict( bail!("Expected tuple with 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 = list_comp.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: list_comp.for_in.clone(), + for_in, whitespace_before_colon: ParenthesizableWhitespace::default(), whitespace_after_colon: space(), lbrace: LeftCurlyBrace { diff --git a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C404_C404.py.snap b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C404_C404.py.snap index 07c6d005b4..8e4eb5a2ed 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C404_C404.py.snap +++ b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C404_C404.py.snap @@ -115,6 +115,8 @@ C404.py:12:4: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` com 12 |-f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}' 12 |+f'{ {s: s for s in "ab"} | dict([(s,s) for s in "ab"])}' 13 13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' +14 14 | +15 15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 C404.py:12:34: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` comprehension) | @@ -133,12 +135,16 @@ C404.py:12:34: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` co 12 |-f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}' 12 |+f'{dict([(s,s) for s in "ab"]) | {s: s for s in "ab"} }' 13 13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' +14 14 | +15 15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 C404.py:13:5: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` comprehension) | 12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}' 13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ C404 +14 | +15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 | = help: Rewrite as a `dict` comprehension @@ -148,12 +154,17 @@ C404.py:13:5: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` com 12 12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}' 13 |-f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' 13 |+f'{ {s: s for s in "ab"} | dict([(s,s) for s in "ab"]) }' +14 14 | +15 15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 +16 16 | saved.append(dict([(k, v)for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]])) C404.py:13:35: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` comprehension) | 12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}' 13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ C404 +14 | +15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 | = help: Rewrite as a `dict` comprehension @@ -163,5 +174,23 @@ C404.py:13:35: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` co 12 12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}' 13 |-f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' 13 |+f'{ dict([(s,s) for s in "ab"]) | {s: s for s in "ab"} }' +14 14 | +15 15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 +16 16 | saved.append(dict([(k, v)for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]])) + +C404.py:16:14: C404 [*] Unnecessary `list` comprehension (rewrite as a `dict` comprehension) + | +15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 +16 | saved.append(dict([(k, v)for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]])) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C404 + | + = help: Rewrite as a `dict` comprehension + +ℹ Suggested fix +13 13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }' +14 14 | +15 15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087 +16 |-saved.append(dict([(k, v)for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]])) + 16 |+saved.append({k: v for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]})