Use matches! for CallPath comparisons (#5099)

## Summary

This PR consistently uses `matches! for static `CallPath` comparisons.
In some cases, we can significantly reduce the number of cases or
checks.

## Test Plan

`cargo test `
This commit is contained in:
Charlie Marsh 2023-06-14 17:06:34 -04:00 committed by GitHub
parent bae183b823
commit 56476dfd61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
67 changed files with 251 additions and 220 deletions

View file

@ -191,16 +191,16 @@ pub fn is_immutable_annotation(expr: &Expr, semantic: &SemanticModel) -> bool {
.any(|target| call_path.as_slice() == *target)
{
true
} else if call_path.as_slice() == ["typing", "Union"] {
} else if matches!(call_path.as_slice(), ["typing", "Union"]) {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.iter()
.all(|elt| is_immutable_annotation(elt, semantic))
} else {
false
}
} else if call_path.as_slice() == ["typing", "Optional"] {
} else if matches!(call_path.as_slice(), ["typing", "Optional"]) {
is_immutable_annotation(slice, semantic)
} else if call_path.as_slice() == ["typing", "Annotated"] {
} else if matches!(call_path.as_slice(), ["typing", "Annotated"]) {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.first()
.map_or(false, |elt| is_immutable_annotation(elt, semantic))
@ -290,7 +290,7 @@ pub fn is_type_checking_block(stmt: &ast::StmtIf, semantic: &SemanticModel) -> b
// Ex) `if typing.TYPE_CHECKING:`
if semantic.resolve_call_path(test).map_or(false, |call_path| {
call_path.as_slice() == ["typing", "TYPE_CHECKING"]
matches!(call_path.as_slice(), ["typing", "TYPE_CHECKING"])
}) {
return true;
}