mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-02 12:58:27 +00:00
[flake8-comprehensions] Fix false positive for C420 with attribute, subscript, or slice assignment targets (#19513)
## Summary Fixes #19511
This commit is contained in:
parent
0095ff4c1a
commit
0ec4801b0d
4 changed files with 63 additions and 0 deletions
|
|
@ -25,6 +25,7 @@ mod tests {
|
|||
#[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::UnnecessaryDictComprehensionForIterable, Path::new("C420_3.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"))]
|
||||
|
|
|
|||
|
|
@ -96,6 +96,12 @@ pub(crate) fn unnecessary_dict_comprehension_for_iterable(
|
|||
return;
|
||||
}
|
||||
|
||||
// Don't suggest `dict.fromkeys` if the target contains side-effecting expressions
|
||||
// (attributes, subscripts, or slices).
|
||||
if contains_side_effecting_sub_expression(&generator.target) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't suggest `dict.fromkeys` if the value is not a constant or constant-like.
|
||||
if !is_constant_like(dict_comp.value.as_ref()) {
|
||||
return;
|
||||
|
|
@ -217,3 +223,12 @@ fn fix_unnecessary_dict_comprehension(value: &Expr, generator: &Comprehension) -
|
|||
node_index: ruff_python_ast::AtomicNodeIndex::dummy(),
|
||||
})
|
||||
}
|
||||
|
||||
fn contains_side_effecting_sub_expression(target: &Expr) -> bool {
|
||||
any_over_expr(target, &|expr| {
|
||||
matches!(
|
||||
expr,
|
||||
Expr::Attribute(_) | Expr::Subscript(_) | Expr::Slice(_)
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
|
||||
---
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue