Add required space to C416 fix (#7204)

Closes https://github.com/astral-sh/ruff/issues/7196.
This commit is contained in:
Charlie Marsh 2023-09-06 18:25:02 +02:00 committed by GitHub
parent c1af1c291d
commit fda48afc23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 1 deletions

View file

@ -17,3 +17,6 @@ d = {"a": 1, "b": 2, "c": 3}
{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])

View file

@ -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(),
))
}

View file

@ -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))