mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-27 05:44:18 +00:00
Add check for is comparison with mutable initialisers to rule F632 (#8607)
## Summary Adds an extra check to F632 to check for any `is` comparisons to a mutable initialisers. Implements #8589 . Example: ```Python named_var = {} if named_var is {}: # F632 (fix) pass ``` The if condition will always evaluate to False because it checks on identity and it's impossible to take the same identity as a hard coded list/set/dict initializer. ## Test Plan Multiple test cases were added to ensure the rule works + doesn't flag false positives + the fix works correctly.
This commit is contained in:
parent
8207d6df82
commit
39728a1198
6 changed files with 594 additions and 3 deletions
|
@ -607,6 +607,19 @@ pub const fn is_const_false(expr: &Expr) -> bool {
|
|||
)
|
||||
}
|
||||
|
||||
/// Return `true` if the [`Expr`] is a mutable iterable initializer, like `{}` or `[]`.
|
||||
pub const fn is_mutable_iterable_initializer(expr: &Expr) -> bool {
|
||||
matches!(
|
||||
expr,
|
||||
Expr::Set(_)
|
||||
| Expr::SetComp(_)
|
||||
| Expr::List(_)
|
||||
| Expr::ListComp(_)
|
||||
| Expr::Dict(_)
|
||||
| Expr::DictComp(_)
|
||||
)
|
||||
}
|
||||
|
||||
/// Extract the names of all handled exceptions.
|
||||
pub fn extract_handled_exceptions(handlers: &[ExceptHandler]) -> Vec<&Expr> {
|
||||
let mut handled_exceptions = Vec::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue