Simplify instances of Option::map_or(true, …) in Clippy sources

This commit is contained in:
Samuel Tardieu 2024-11-05 00:02:57 +01:00
parent d1688b53f1
commit ca963b653e
34 changed files with 39 additions and 42 deletions

View file

@ -91,7 +91,7 @@ impl ApproxConstant {
let s = s.as_str();
if s.parse::<f64>().is_ok() {
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| self.msrv.meets(msrv)) {
if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(msrv)) {
span_lint_and_help(
cx,
APPROX_CONSTANT,

View file

@ -100,13 +100,13 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
// TODO: This check currently bails if the local variable has no initializer.
// That is overly conservative - the lint should fire even if there was no initializer,
// but the variable has been initialized before `lhs` was evaluated.
&& path_to_local(lhs).map_or(true, |lhs| local_is_initialized(cx, lhs))
&& path_to_local(lhs).is_none_or(|lhs| local_is_initialized(cx, lhs))
&& let Some(resolved_impl) = cx.tcx.impl_of_method(resolved_fn.def_id())
// Derived forms don't implement `clone_from`/`clone_into`.
// See https://github.com/rust-lang/rust/pull/98445#issuecomment-1190681305
&& !cx.tcx.is_builtin_derived(resolved_impl)
// Don't suggest calling a function we're implementing.
&& resolved_impl.as_local().map_or(true, |block_id| {
&& resolved_impl.as_local().is_none_or(|block_id| {
cx.tcx.hir().parent_owner_iter(e.hir_id).all(|(id, _)| id.def_id != block_id)
})
&& let resolved_assoc_items = cx.tcx.associated_items(resolved_impl)

View file

@ -57,7 +57,7 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
&& !addrof_target.span.from_expansion()
&& let ref_ty = cx.typeck_results().expr_ty(deref_target)
&& let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind()
&& get_parent_expr(cx, e).map_or(true, |parent| {
&& get_parent_expr(cx, e).is_none_or(|parent| {
match parent.kind {
// `*&*foo` should lint `deref_addrof` instead.
ExprKind::Unary(UnOp::Deref, _) => is_lint_allowed(cx, DEREF_ADDROF, parent.hir_id),

View file

@ -43,7 +43,7 @@ fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, fiel
}
fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: Option<&T>) -> bool {
value.map_or(true, |s| s.as_ref().is_empty())
value.is_none_or(|s| s.as_ref().is_empty())
}
fn is_empty_vec(value: &[String]) -> bool {

View file

@ -134,7 +134,7 @@ pub(super) fn check(
};
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
let cast_from_ptr_size = def.repr().int.map_or(true, |ty| matches!(ty, IntegerType::Pointer(_),));
let cast_from_ptr_size = def.repr().int.is_none_or(|ty| matches!(ty, IntegerType::Pointer(_),));
let suffix = match (cast_from_ptr_size, is_isize_or_usize(cast_to)) {
(_, false) if from_nbits > to_nbits => "",
(false, true) if from_nbits > 64 => "",

View file

@ -212,7 +212,7 @@ fn lint_if_same_then_else(cx: &LateContext<'_>, conds: &[&Expr<'_>], blocks: &[&
.array_windows::<2>()
.enumerate()
.fold(true, |all_eq, (i, &[lhs, rhs])| {
if eq.eq_block(lhs, rhs) && !contains_let(conds[i]) && conds.get(i + 1).map_or(true, |e| !contains_let(e)) {
if eq.eq_block(lhs, rhs) && !contains_let(conds[i]) && conds.get(i + 1).is_none_or(|e| !contains_let(e)) {
span_lint_and_note(
cx,
IF_SAME_THEN_ELSE,
@ -470,7 +470,7 @@ fn scan_block_for_eq<'tcx>(
b.stmts
// the bounds check will catch the underflow
.get(b.stmts.len().wrapping_sub(offset + 1))
.map_or(true, |s| hash != hash_stmt(cx, s))
.is_none_or(|s| hash != hash_stmt(cx, s))
})
})
.map_or(block.stmts.len() - start_end_eq, |(i, _)| i);

View file

@ -452,7 +452,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
));
} else if stability.is_deref_stable()
// Auto-deref doesn't combine with other adjustments
&& next_adjust.map_or(true, |a| matches!(a.kind, Adjust::Deref(_) | Adjust::Borrow(_)))
&& next_adjust.is_none_or(|a| matches!(a.kind, Adjust::Deref(_) | Adjust::Borrow(_)))
&& iter.all(|a| matches!(a.kind, Adjust::Deref(_) | Adjust::Borrow(_)))
{
self.state = Some((State::Borrow { mutability }, StateData {

View file

@ -276,7 +276,7 @@ fn check_inputs(
&& typeck
.expr_adjustments(arg)
.last()
.map_or(true, |a| a.target == typeck.expr_ty(arg))
.is_none_or(|a| a.target == typeck.expr_ty(arg))
})
}

View file

@ -165,7 +165,7 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
&& fn_header.abi == Abi::Rust
&& fn_decl.inputs.len() as u64 > self.max_fn_params_bools
&& get_parent_as_impl(cx.tcx, cx.tcx.local_def_id_to_hir_id(def_id))
.map_or(true, |impl_item| impl_item.of_trait.is_none())
.is_none_or(|impl_item| impl_item.of_trait.is_none())
{
check_fn_decl(cx, fn_decl, span, self.max_fn_params_bools);
}

View file

@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for FromOverInto {
|diag| {
// If the target type is likely foreign mention the orphan rules as it's a common source of
// confusion
if path_def_id(cx, target_ty.peel_refs()).map_or(true, |id| !id.is_local()) {
if path_def_id(cx, target_ty.peel_refs()).is_none_or(|id| !id.is_local()) {
diag.help(
"`impl From<Local> for Foreign` is allowed by the orphan rules, for more information see\n\
https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence"

View file

@ -86,7 +86,7 @@ fn mutex_lock_call<'tcx>(
&& path.ident.as_str() == "lock"
&& let ty = cx.typeck_results().expr_ty(self_arg).peel_refs()
&& is_type_diagnostic_item(cx, ty, sym::Mutex)
&& op_mutex.map_or(true, |op| eq_expr_value(cx, self_arg, op))
&& op_mutex.is_none_or(|op| eq_expr_value(cx, self_arg, op))
{
ControlFlow::Break(self_arg)
} else {

View file

@ -29,7 +29,7 @@ pub(super) fn check(
if !msrv.meets(msrvs::ARRAY_INTO_ITERATOR) {
return;
}
} else if count.try_to_target_usize(cx.tcx).map_or(true, |x| x > 32) && !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN) {
} else if count.try_to_target_usize(cx.tcx).is_none_or(|x| x > 32) && !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN) {
return;
}
}

View file

@ -72,14 +72,13 @@ fn check_arm<'tcx>(
(Some(a), Some(b)) => SpanlessEq::new(cx).eq_expr(a, b),
}
// the binding must not be used in the if guard
&& outer_guard.map_or(
true,
&& outer_guard.is_none_or(
|e| !is_local_used(cx, e, binding_id)
)
// ...or anywhere in the inner expression
&& match inner {
IfLetOrMatch::IfLet(_, _, body, els, _) => {
!is_local_used(cx, body, binding_id) && els.map_or(true, |e| !is_local_used(cx, e, binding_id))
!is_local_used(cx, body, binding_id) && els.is_none_or(|e| !is_local_used(cx, e, binding_id))
},
IfLetOrMatch::Match(_, arms, ..) => !arms.iter().any(|arm| is_local_used(cx, arm, binding_id)),
}

View file

@ -30,7 +30,7 @@ pub(super) fn check(
.type_dependent_def_id(expr.hir_id)
.and_then(|id| cx.tcx.trait_of_item(id))
.zip(cx.tcx.lang_items().clone_trait())
.map_or(true, |(x, y)| x != y)
.is_none_or(|(x, y)| x != y)
{
return;
}

View file

@ -4531,7 +4531,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
&& method_config.output_type.matches(&sig.decl.output)
// in case there is no first arg, since we already have checked the number of arguments
// it's should be always true
&& first_arg_ty_opt.map_or(true, |first_arg_ty| method_config
&& first_arg_ty_opt.is_none_or(|first_arg_ty| method_config
.self_kind.matches(cx, self_ty, first_arg_ty)
)
&& fn_header_equals(method_config.fn_header, sig.header)

View file

@ -23,7 +23,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>, a
if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind {
let body = cx.tcx.hir().body(body);
let arg_id = body.params[0].pat.hir_id;
let mutates_arg = mutated_variables(body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
let mutates_arg = mutated_variables(body.value, cx).is_none_or(|used_mutably| used_mutably.contains(&arg_id));
let (clone_or_copy_needed, _) = clone_or_copy_needed(cx, body.params[0].pat, body.value);
let (mut found_mapping, mut found_filtering) = check_expression(cx, arg_id, body.value);

View file

@ -351,7 +351,7 @@ fn used_underscore_binding<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
/// `unused_variables`'s idea
/// of what it means for an expression to be "used".
fn is_used(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
get_parent_expr(cx, expr).map_or(true, |parent| match parent.kind {
get_parent_expr(cx, expr).is_none_or(|parent| match parent.kind {
ExprKind::Assign(_, rhs, _) | ExprKind::AssignOp(_, _, rhs) => SpanlessEq::new(cx).eq_expr(rhs, expr),
_ => is_used(cx, parent),
})

View file

@ -362,7 +362,7 @@ fn referent_used_exactly_once<'tcx>(
let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id);
if possible_borrowers
.last()
.map_or(true, |&(local_def_id, _)| local_def_id != body_owner_local_def_id)
.is_none_or(|&(local_def_id, _)| local_def_id != body_owner_local_def_id)
{
possible_borrowers.push((body_owner_local_def_id, PossibleBorrowerMap::new(cx, mir)));
}

View file

@ -238,7 +238,7 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
ExprKind::Struct(_, fields, ref base) => {
!has_drop(cx, cx.typeck_results().expr_ty(expr))
&& fields.iter().all(|field| has_no_effect(cx, field.expr))
&& base.as_ref().map_or(true, |base| has_no_effect(cx, base))
&& base.as_ref().is_none_or(|base| has_no_effect(cx, base))
},
ExprKind::Call(callee, args) => {
if let ExprKind::Path(ref qpath) = callee.kind {

View file

@ -335,7 +335,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
// i.e. having an enum doesn't necessary mean a type has a frozen variant.
// And, implementing it isn't a trivial task; it'll probably end up
// re-implementing the trait predicate evaluation specific to `Freeze`.
&& body_id_opt.map_or(true, |body_id| Self::is_value_unfrozen_poly(cx, body_id, normalized))
&& body_id_opt.is_none_or(|body_id| Self::is_value_unfrozen_poly(cx, body_id, normalized))
{
lint(cx, Source::Assoc { item: trait_item.span });
}

View file

@ -200,7 +200,7 @@ impl Params {
if self
.get_by_fn(param.fn_id, usage.idx)
// If the parameter can't be found, then it's used for more than just recursion.
.map_or(true, |p| self.try_disable_lint_for_param(p, eval_stack))
.is_none_or(|p| self.try_disable_lint_for_param(p, eval_stack))
{
param.apply_lint.set(false);
eval_stack.pop();

View file

@ -27,7 +27,7 @@ pub(super) fn check<'tcx>(
if let Some((_, lang_item)) = binop_traits(op.node)
&& let Some(trait_id) = cx.tcx.lang_items().get(lang_item)
&& let parent_fn = cx.tcx.hir().get_parent_item(e.hir_id).def_id
&& trait_ref_of_method(cx, parent_fn).map_or(true, |t| t.path.res.def_id() != trait_id)
&& trait_ref_of_method(cx, parent_fn).is_none_or(|t| t.path.res.def_id() != trait_id)
&& implements_trait(cx, ty, trait_id, &[rty.into()])
{
// Primitive types execute assign-ops right-to-left. Every other type is left-to-right.

View file

@ -541,9 +541,7 @@ fn check_mut_from_ref<'tcx>(cx: &LateContext<'tcx>, sig: &FnSig<'_>, body: Optio
.collect();
if let Some(args) = args
&& !args.is_empty()
&& body.map_or(true, |body| {
sig.header.safety == Safety::Unsafe || contains_unsafe_block(cx, body.value)
})
&& body.is_none_or(|body| sig.header.safety == Safety::Unsafe || contains_unsafe_block(cx, body.value))
{
span_lint_and_then(
cx,

View file

@ -88,7 +88,7 @@ fn desugar_async_block<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Op
cx.typeck_results()
.closure_min_captures
.get(def_id)
.map_or(true, |m| {
.is_none_or(|m| {
m.values().all(|places| {
places
.iter()

View file

@ -93,7 +93,7 @@ impl SingleCallFn {
.tcx
.hir()
.maybe_body_owned_by(fn_def_id)
.map_or(true, |body| is_in_test_function(cx.tcx, body.value.hir_id))
.is_none_or(|body| is_in_test_function(cx.tcx, body.value.hir_id))
|| match cx.tcx.hir_node(fn_hir_id) {
Node::Item(item) => is_from_proc_macro(cx, item),
Node::ImplItem(item) => is_from_proc_macro(cx, item),

View file

@ -337,7 +337,7 @@ fn is_unsafe_from_proc_macro(cx: &LateContext<'_>, span: Span) -> bool {
.src
.as_deref()
.and_then(|src| src.get(file_pos.pos.to_usize()..))
.map_or(true, |src| !src.starts_with("unsafe"))
.is_none_or(|src| !src.starts_with("unsafe"))
}
// Checks if any parent {expression, statement, block, local, const, static}

View file

@ -145,7 +145,7 @@ fn expr_needs_inferred_result<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -
}
while let Some(id) = locals_to_check.pop() {
if let Node::LetStmt(l) = cx.tcx.parent_hir_node(id) {
if !l.ty.map_or(true, |ty| matches!(ty.kind, TyKind::Infer)) {
if !l.ty.is_none_or(|ty| matches!(ty.kind, TyKind::Infer)) {
return false;
}
if let Some(e) = l.init {

View file

@ -93,7 +93,7 @@ fn check_ident(cx: &LateContext<'_>, ident: &Ident, hir_id: HirId, be_aggressive
while let Some(c) = s.next() {
r.push(
if replace(&mut prev_upper, c.is_ascii_uppercase())
&& s.clone().next().map_or(true, |c| c.is_ascii_uppercase())
&& s.clone().next().is_none_or(|c| c.is_ascii_uppercase())
{
c.to_ascii_lowercase()
} else {

View file

@ -94,7 +94,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
&& let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args
&& parameters
.as_ref()
.map_or(true, |params| params.parenthesized == GenericArgsParentheses::No)
.is_none_or(|params| params.parenthesized == GenericArgsParentheses::No)
&& !item.span.from_expansion()
&& !is_from_proc_macro(cx, item)
// expensive, should be last check

View file

@ -226,7 +226,7 @@ impl Constant<'_> {
.zip(r)
.zip(tys)
.map(|((li, ri), cmp_type)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
.find(|r| r.is_none_or(|o| o != Ordering::Equal))
.unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
_ => None,
},
@ -236,7 +236,7 @@ impl Constant<'_> {
};
iter::zip(l, r)
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
.find(|r| r.is_none_or(|o| o != Ordering::Equal))
.unwrap_or_else(|| Some(l.len().cmp(&r.len())))
},
(Self::Repeat(lv, ls), Self::Repeat(rv, rs)) => {

View file

@ -1570,7 +1570,7 @@ pub fn is_else_clause_in_let_else(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
pub fn is_range_full(cx: &LateContext<'_>, expr: &Expr<'_>, container_path: Option<&Path<'_>>) -> bool {
let ty = cx.typeck_results().expr_ty(expr);
if let Some(Range { start, end, limits }) = Range::hir(expr) {
let start_is_none_or_min = start.map_or(true, |start| {
let start_is_none_or_min = start.is_none_or(|start| {
if let rustc_ty::Adt(_, subst) = ty.kind()
&& let bnd_ty = subst.type_at(0)
&& let Some(min_val) = bnd_ty.numeric_min_val(cx.tcx)
@ -1582,7 +1582,7 @@ pub fn is_range_full(cx: &LateContext<'_>, expr: &Expr<'_>, container_path: Opti
false
}
});
let end_is_none_or_max = end.map_or(true, |end| match limits {
let end_is_none_or_max = end.is_none_or(|end| match limits {
RangeLimits::Closed => {
if let rustc_ty::Adt(_, subst) = ty.kind()
&& let bnd_ty = subst.type_at(0)

View file

@ -93,7 +93,7 @@ pub fn expn_is_local(expn: ExpnId) -> bool {
std::iter::once((expn, data))
.chain(backtrace)
.find_map(|(_, data)| data.macro_def_id)
.map_or(true, DefId::is_local)
.is_none_or(DefId::is_local)
}
/// Returns an iterator of macro expansions that created the given span.

View file

@ -121,7 +121,7 @@ impl Msrv {
}
pub fn meets(&self, required: RustcVersion) -> bool {
self.current().map_or(true, |msrv| msrv >= required)
self.current().is_none_or(|msrv| msrv >= required)
}
fn parse_attr(sess: &Session, attrs: &[Attribute]) -> Option<RustcVersion> {

View file

@ -27,7 +27,7 @@ pub fn mutated_variables<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) ->
}
pub fn is_potentially_mutated<'tcx>(variable: HirId, expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> bool {
mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&variable))
mutated_variables(expr, cx).is_none_or(|mutated| mutated.contains(&variable))
}
pub fn is_potentially_local_place(local_id: HirId, place: &Place<'_>) -> bool {