mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
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:
parent
2e1754e5fc
commit
1df7e9831b
162 changed files with 344 additions and 476 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue