Replace .map_or(false, $closure) with .is_some_and(closure) (#6244)

**Summary**
[Option::is_some_and](https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.is_some_and)
and
[Result::is_ok_and](https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok_and)
are new methods is rust 1.70. I find them way more readable than
`.map_or(false, ...)`.

The changes are `s/.map_or(false,/.is_some_and(/g`, then manually
switching to `is_ok_and` where the value is a Result rather than an
Option.

**Test Plan** n/a^
This commit is contained in:
konsti 2023-08-01 19:29:42 +02:00 committed by GitHub
parent 2e1754e5fc
commit 1df7e9831b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
162 changed files with 344 additions and 476 deletions

View file

@ -12,7 +12,7 @@ fn common_ancestor(
stop: Option<NodeId>,
node_tree: &Nodes,
) -> Option<NodeId> {
if stop.map_or(false, |stop| left == stop || right == stop) {
if stop.is_some_and(|stop| left == stop || right == stop) {
return None;
}
@ -89,7 +89,7 @@ fn descendant_of<'a>(
node_tree: &Nodes<'a>,
) -> bool {
ancestors.iter().any(|ancestor| {
node_tree.node_id(ancestor).map_or(false, |ancestor| {
node_tree.node_id(ancestor).is_some_and(|ancestor| {
common_ancestor(stmt, ancestor, Some(stop), node_tree).is_some()
})
})

View file

@ -31,7 +31,7 @@ pub fn classify(
// `@staticmethod`).
semantic
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
.is_some_and(|call_path| {
matches!(
call_path.as_slice(),
["", "staticmethod"] | ["abc", "abstractstaticmethod"]
@ -47,7 +47,7 @@ pub fn classify(
// The class itself extends a known metaclass, so all methods are class methods.
semantic
.resolve_call_path(map_callable(expr))
.map_or(false, |call_path| {
.is_some_and( |call_path| {
matches!(call_path.as_slice(), ["", "type"] | ["abc", "ABCMeta"])
})
})
@ -55,7 +55,7 @@ pub fn classify(
// The method is decorated with a class method decorator (like `@classmethod`).
semantic
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
.is_some_and( |call_path| {
matches!(
call_path.as_slice(),
["", "classmethod"] | ["abc", "abstractclassmethod"]

View file

@ -74,9 +74,7 @@ pub fn exc_info<'a>(keywords: &'a [Keyword], semantic: &SemanticModel) -> Option
.value
.as_call_expr()
.and_then(|call| semantic.resolve_call_path(&call.func))
.map_or(false, |call_path| {
matches!(call_path.as_slice(), ["sys", "exc_info"])
})
.is_some_and(|call_path| matches!(call_path.as_slice(), ["sys", "exc_info"]))
{
return Some(exc_info);
}

View file

@ -116,7 +116,7 @@ pub fn to_pep585_generic(expr: &Expr, semantic: &SemanticModel) -> Option<Module
/// Return whether a given expression uses a PEP 585 standard library generic.
pub fn is_pep585_generic(expr: &Expr, semantic: &SemanticModel) -> bool {
semantic.resolve_call_path(expr).map_or(false, |call_path| {
semantic.resolve_call_path(expr).is_some_and(|call_path| {
let [module, name] = call_path.as_slice() else {
return false;
};
@ -189,14 +189,13 @@ pub fn to_pep604_operator(
pub fn is_immutable_annotation(expr: &Expr, semantic: &SemanticModel) -> bool {
match expr {
Expr::Name(_) | Expr::Attribute(_) => {
semantic.resolve_call_path(expr).map_or(false, |call_path| {
semantic.resolve_call_path(expr).is_some_and(|call_path| {
is_immutable_non_generic_type(call_path.as_slice())
|| is_immutable_generic_type(call_path.as_slice())
})
}
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => semantic
.resolve_call_path(value)
.map_or(false, |call_path| {
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
semantic.resolve_call_path(value).is_some_and(|call_path| {
if is_immutable_generic_type(call_path.as_slice()) {
true
} else if matches!(call_path.as_slice(), ["typing", "Union"]) {
@ -211,14 +210,15 @@ pub fn is_immutable_annotation(expr: &Expr, semantic: &SemanticModel) -> bool {
} 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))
.is_some_and(|elt| is_immutable_annotation(elt, semantic))
} else {
false
}
} else {
false
}
}),
})
}
Expr::BinOp(ast::ExprBinOp {
left,
op: Operator::BitOr,
@ -239,7 +239,7 @@ pub fn is_immutable_func(
semantic: &SemanticModel,
extend_immutable_calls: &[CallPath],
) -> bool {
semantic.resolve_call_path(func).map_or(false, |call_path| {
semantic.resolve_call_path(func).is_some_and(|call_path| {
is_immutable_return_type(call_path.as_slice())
|| extend_immutable_calls
.iter()
@ -253,7 +253,7 @@ pub fn is_mutable_func(func: &Expr, semantic: &SemanticModel) -> bool {
.resolve_call_path(func)
.as_ref()
.map(CallPath::as_slice)
.map_or(false, is_mutable_return_type)
.is_some_and(is_mutable_return_type)
}
/// Return `true` if `expr` is an expression that resolves to a mutable value.
@ -291,9 +291,10 @@ 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| {
matches!(call_path.as_slice(), ["typing", "TYPE_CHECKING"])
}) {
if semantic
.resolve_call_path(test)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["typing", "TYPE_CHECKING"]))
{
return true;
}

View file

@ -18,9 +18,7 @@ pub fn is_staticmethod(decorator_list: &[Decorator], semantic: &SemanticModel) -
decorator_list.iter().any(|decorator| {
semantic
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
matches!(call_path.as_slice(), ["", "staticmethod"])
})
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "staticmethod"]))
})
}
@ -29,9 +27,7 @@ pub fn is_classmethod(decorator_list: &[Decorator], semantic: &SemanticModel) ->
decorator_list.iter().any(|decorator| {
semantic
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
matches!(call_path.as_slice(), ["", "classmethod"])
})
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "classmethod"]))
})
}
@ -54,7 +50,7 @@ pub fn is_abstract(decorator_list: &[Decorator], semantic: &SemanticModel) -> bo
decorator_list.iter().any(|decorator| {
semantic
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
.is_some_and(|call_path| {
matches!(
call_path.as_slice(),
[
@ -80,7 +76,7 @@ pub fn is_property(
decorator_list.iter().any(|decorator| {
semantic
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
.is_some_and(|call_path| {
matches!(
call_path.as_slice(),
["", "property"] | ["functools", "cached_property"]
@ -208,7 +204,7 @@ pub(crate) fn method_visibility(stmt: &Stmt) -> Visibility {
}) => {
// Is this a setter or deleter?
if decorator_list.iter().any(|decorator| {
collect_call_path(&decorator.expression).map_or(false, |call_path| {
collect_call_path(&decorator.expression).is_some_and(|call_path| {
call_path.as_slice() == [name, "setter"]
|| call_path.as_slice() == [name, "deleter"]
})

View file

@ -150,8 +150,8 @@ impl<'a> Definitions<'a> {
MemberKind::Class => {
let parent = &definitions[member.parent];
if parent.visibility.is_private()
|| exports.map_or(false, |exports| {
member.name().map_or(false, |name| !exports.contains(&name))
|| exports.is_some_and(|exports| {
member.name().is_some_and(|name| !exports.contains(&name))
})
{
Visibility::Private
@ -180,8 +180,8 @@ impl<'a> Definitions<'a> {
MemberKind::Function => {
let parent = &definitions[member.parent];
if parent.visibility.is_private()
|| exports.map_or(false, |exports| {
member.name().map_or(false, |name| !exports.contains(&name))
|| exports.is_some_and(|exports| {
member.name().is_some_and(|name| !exports.contains(&name))
})
{
Visibility::Private

View file

@ -163,9 +163,8 @@ impl<'a> SemanticModel<'a> {
/// Return `true` if the `Expr` is a reference to `typing.${target}`.
pub fn match_typing_expr(&self, expr: &Expr, target: &str) -> bool {
self.resolve_call_path(expr).map_or(false, |call_path| {
self.match_typing_call_path(&call_path, target)
})
self.resolve_call_path(expr)
.is_some_and(|call_path| self.match_typing_call_path(&call_path, target))
}
/// Return `true` if the call path is a reference to `typing.${target}`.
@ -245,7 +244,7 @@ impl<'a> SemanticModel<'a> {
/// Return `true` if `member` is bound as a builtin.
pub fn is_builtin(&self, member: &str) -> bool {
self.find_binding(member)
.map_or(false, |binding| binding.kind.is_builtin())
.is_some_and(|binding| binding.kind.is_builtin())
}
/// Return `true` if `member` is an "available" symbol, i.e., a symbol that has not been bound