mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-02 12:58:27 +00:00
[flake8-comprehensions] Fix C420 to prepend whitespace when needed (#18616)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:
- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
requests.)
- Does this pull request include references to any relevant issues?
-->
## Summary
This PR fixes rule C420's fix. The fix replaces `{...}` with
`dict....(...)`. Therefore, if there is any identifier or such right
before the fix, the fix will fuse that previous token with `dict...`.
The example in the issue is
```python
0 or{x: None for x in "x"}
# gets "fixed" to
0 ordict.fromkeys(iterable)
```
## Related Issues
Fixes: https://github.com/astral-sh/ruff/issues/18599
This commit is contained in:
parent
9f0d3cca89
commit
34052a1185
4 changed files with 33 additions and 6 deletions
5
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C420_2.py
vendored
Normal file
5
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C420_2.py
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
foo or{x: None for x in bar}
|
||||
|
||||
|
||||
# C420 fix must make sure to insert a leading space if needed,
|
||||
# See https://github.com/astral-sh/ruff/issues/18599
|
||||
|
|
@ -24,6 +24,7 @@ mod tests {
|
|||
#[test_case(Rule::UnnecessaryComprehensionInCall, Path::new("C419_2.py"))]
|
||||
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420.py"))]
|
||||
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420_1.py"))]
|
||||
#[test_case(Rule::UnnecessaryDictComprehensionForIterable, Path::new("C420_2.py"))]
|
||||
#[test_case(Rule::UnnecessaryDoubleCastOrProcess, Path::new("C414.py"))]
|
||||
#[test_case(Rule::UnnecessaryGeneratorDict, Path::new("C402.py"))]
|
||||
#[test_case(Rule::UnnecessaryGeneratorList, Path::new("C400.py"))]
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use ruff_python_ast::{self as ast, Arguments, Comprehension, Expr, ExprCall, Exp
|
|||
use ruff_text_size::{Ranged, TextRange};
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::fix::edits::pad_start;
|
||||
use crate::{Edit, Fix, FixAvailability, Violation};
|
||||
|
||||
/// ## What it does
|
||||
|
|
@ -136,12 +137,16 @@ pub(crate) fn unnecessary_dict_comprehension_for_iterable(
|
|||
|
||||
if checker.semantic().has_builtin_binding("dict") {
|
||||
let edit = Edit::range_replacement(
|
||||
checker
|
||||
.generator()
|
||||
.expr(&fix_unnecessary_dict_comprehension(
|
||||
dict_comp.value.as_ref(),
|
||||
generator,
|
||||
)),
|
||||
pad_start(
|
||||
checker
|
||||
.generator()
|
||||
.expr(&fix_unnecessary_dict_comprehension(
|
||||
dict_comp.value.as_ref(),
|
||||
generator,
|
||||
)),
|
||||
dict_comp.start(),
|
||||
checker.locator(),
|
||||
),
|
||||
dict_comp.range(),
|
||||
);
|
||||
diagnostic.set_fix(Fix::applicable_edit(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
|
||||
---
|
||||
C420_2.py:1:7: C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
|
||||
|
|
||||
1 | foo or{x: None for x in bar}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ C420
|
||||
|
|
||||
= help: Replace with `dict.fromkeys(iterable, value)`)
|
||||
|
||||
ℹ Safe fix
|
||||
1 |-foo or{x: None for x in bar}
|
||||
1 |+foo or dict.fromkeys(bar)
|
||||
2 2 |
|
||||
3 3 |
|
||||
4 4 | # C420 fix must make sure to insert a leading space if needed,
|
||||
Loading…
Add table
Add a link
Reference in a new issue