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:
Jesse Serrao 2023-11-11 00:29:23 +00:00 committed by GitHub
parent 8207d6df82
commit 39728a1198
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 594 additions and 3 deletions

View file

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