mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:10 +00:00
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:
parent
bae183b823
commit
56476dfd61
67 changed files with 251 additions and 220 deletions
|
@ -35,7 +35,7 @@ pub fn classify(
|
|||
semantic
|
||||
.resolve_call_path(map_callable(&decorator.expression))
|
||||
.map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["", "staticmethod"]
|
||||
matches!(call_path.as_slice(), ["", "staticmethod"])
|
||||
|| staticmethod_decorators
|
||||
.iter()
|
||||
.any(|decorator| call_path == from_qualified_name(decorator))
|
||||
|
@ -55,7 +55,7 @@ pub fn classify(
|
|||
|| decorator_list.iter().any(|decorator| {
|
||||
// The method is decorated with a class method decorator (like `@classmethod`).
|
||||
semantic.resolve_call_path(map_callable(&decorator.expression)).map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["", "classmethod"] ||
|
||||
matches!(call_path.as_slice(), ["", "classmethod"]) ||
|
||||
classmethod_decorators
|
||||
.iter()
|
||||
.any(|decorator| call_path == from_qualified_name(decorator))
|
||||
|
|
|
@ -58,7 +58,7 @@ pub fn exc_info<'a>(keywords: &'a [Keyword], semantic: &SemanticModel) -> Option
|
|||
// Ex) `logging.error("...", exc_info=sys.exc_info())`
|
||||
if let Expr::Call(ast::ExprCall { func, .. }) = &exc_info.value {
|
||||
if semantic.resolve_call_path(func).map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["sys", "exc_info"]
|
||||
matches!(call_path.as_slice(), ["sys", "exc_info"])
|
||||
}) {
|
||||
return Some(exc_info);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ pub fn is_staticmethod(decorator_list: &[Decorator], semantic: &SemanticModel) -
|
|||
semantic
|
||||
.resolve_call_path(map_callable(&decorator.expression))
|
||||
.map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["", "staticmethod"]
|
||||
matches!(call_path.as_slice(), ["", "staticmethod"])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ pub fn is_classmethod(decorator_list: &[Decorator], semantic: &SemanticModel) ->
|
|||
semantic
|
||||
.resolve_call_path(map_callable(&decorator.expression))
|
||||
.map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["", "classmethod"]
|
||||
matches!(call_path.as_slice(), ["", "classmethod"])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -81,11 +81,12 @@ pub fn is_property(
|
|||
semantic
|
||||
.resolve_call_path(map_callable(&decorator.expression))
|
||||
.map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["", "property"]
|
||||
|| call_path.as_slice() == ["functools", "cached_property"]
|
||||
|| extra_properties
|
||||
.iter()
|
||||
.any(|extra_property| extra_property.as_slice() == call_path.as_slice())
|
||||
matches!(
|
||||
call_path.as_slice(),
|
||||
["", "property"] | ["functools", "cached_property"]
|
||||
) || extra_properties
|
||||
.iter()
|
||||
.any(|extra_property| extra_property.as_slice() == call_path.as_slice())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue