From 8c4c61886838887542d9bf1ec287d9951059ad51 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 6 Sep 2023 18:25:02 +0200 Subject: [PATCH] Add required space to C416 fix (#7204) Closes https://github.com/astral-sh/ruff/issues/7196. --- .../test/fixtures/flake8_comprehensions/C416.py | 3 +++ .../ruff/src/rules/flake8_comprehensions/fixes.rs | 3 ++- ...lake8_comprehensions__tests__C416_C416.py.snap | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C416.py b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C416.py index df76e93eb80b55..6ca19ee5746715 100644 --- a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C416.py +++ b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C416.py @@ -17,3 +17,6 @@ {k.foo: k for k in y} {k["foo"]: k for k in y} {k: v if v else None for k, v in y} + +# Regression test for: https://github.com/astral-sh/ruff/issues/7196 +any(len(symbol_table.get_by_type(symbol_type)) > 0 for symbol_type in[t for t in SymbolType]) diff --git a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs index a7f094358986e1..dfd60f2d4d013a 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs @@ -16,6 +16,7 @@ use ruff_python_semantic::SemanticModel; use ruff_source_file::Locator; use crate::autofix::codemods::CodegenStylist; +use crate::autofix::edits::pad; use crate::cst::helpers::space; use crate::rules::flake8_comprehensions::rules::ObjectType; use crate::{ @@ -922,7 +923,7 @@ pub(crate) fn fix_unnecessary_comprehension( } Ok(Edit::range_replacement( - tree.codegen_stylist(stylist), + pad(tree.codegen_stylist(stylist), expr.range(), locator), expr.range(), )) } diff --git a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C416_C416.py.snap b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C416_C416.py.snap index 253dc8744ae76c..7268cef9f7778c 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C416_C416.py.snap +++ b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C416_C416.py.snap @@ -83,4 +83,19 @@ C416.py:9:1: C416 [*] Unnecessary `dict` comprehension (rewrite using `dict()`) 11 11 | [i for i, in z] 12 12 | [i for i, j in y] +C416.py:22:70: C416 [*] Unnecessary `list` comprehension (rewrite using `list()`) + | +21 | # Regression test for: https://github.com/astral-sh/ruff/issues/7196 +22 | any(len(symbol_table.get_by_type(symbol_type)) > 0 for symbol_type in[t for t in SymbolType]) + | ^^^^^^^^^^^^^^^^^^^^^^^ C416 + | + = help: Rewrite using `list()` + +ℹ Suggested fix +19 19 | {k: v if v else None for k, v in y} +20 20 | +21 21 | # Regression test for: https://github.com/astral-sh/ruff/issues/7196 +22 |-any(len(symbol_table.get_by_type(symbol_type)) > 0 for symbol_type in[t for t in SymbolType]) + 22 |+any(len(symbol_table.get_by_type(symbol_type)) > 0 for symbol_type in list(SymbolType)) +