1
Fork 0

Use is_some_and/is_ok_and in less obvious spots

This commit is contained in:
Maybe Waffle 2023-05-24 14:33:43 +00:00
parent fb0f74a8c9
commit 307799a711
19 changed files with 53 additions and 88 deletions

View file

@ -348,7 +348,7 @@ impl<'a> AstValidator<'a> {
let source_map = self.session.source_map(); let source_map = self.session.source_map();
let end = source_map.end_point(sp); let end = source_map.end_point(sp);
if source_map.span_to_snippet(end).map(|s| s == ";").unwrap_or(false) { if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
end end
} else { } else {
sp.shrink_to_hi() sp.shrink_to_hi()

View file

@ -224,12 +224,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
if info.tail_result_is_ignored { if info.tail_result_is_ignored {
// #85581: If the first mutable borrow's scope contains // #85581: If the first mutable borrow's scope contains
// the second borrow, this suggestion isn't helpful. // the second borrow, this suggestion isn't helpful.
if !multiple_borrow_span if !multiple_borrow_span.is_some_and(|(old, new)| {
.map(|(old, new)| {
old.to(info.span.shrink_to_hi()).contains(new) old.to(info.span.shrink_to_hi()).contains(new)
}) }) {
.unwrap_or(false)
{
err.span_suggestion_verbose( err.span_suggestion_verbose(
info.span.shrink_to_hi(), info.span.shrink_to_hi(),
"consider adding semicolon after the expression so its \ "consider adding semicolon after the expression so its \

View file

@ -289,8 +289,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
.body .body
.local_decls .local_decls
.get(local) .get(local)
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])) .is_some_and(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])) =>
.unwrap_or(false) =>
{ {
let decl = &self.body.local_decls[local]; let decl = &self.body.local_decls[local];
err.span_label(span, format!("cannot {act}")); err.span_label(span, format!("cannot {act}"));

View file

@ -125,8 +125,7 @@ impl OutlivesSuggestionBuilder {
|(r, _)| { |(r, _)| {
self.constraints_to_add self.constraints_to_add
.get(r) .get(r)
.map(|r_outlived| r_outlived.as_slice().contains(fr)) .is_some_and(|r_outlived| r_outlived.as_slice().contains(fr))
.unwrap_or(false)
}, },
); );

View file

@ -432,11 +432,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
let is_cold = if fn_sig.abi() == Abi::RustCold { let is_cold = if fn_sig.abi() == Abi::RustCold {
true true
} else { } else {
instance instance.is_some_and(|inst| {
.map(|inst| {
fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD) fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD)
}) })
.unwrap_or(false)
}; };
if is_cold { if is_cold {
fx.bcx.set_cold_block(fx.bcx.current_block().unwrap()); fx.bcx.set_cold_block(fx.bcx.current_block().unwrap());
@ -470,7 +468,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
}; };
// Pass the caller location for `#[track_caller]`. // Pass the caller location for `#[track_caller]`.
if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) { if instance.is_some_and(|inst| inst.def.requires_caller_location(fx.tcx)) {
let caller_location = fx.get_caller_location(source_info); let caller_location = fx.get_caller_location(source_info);
args.push(CallArgument { value: caller_location, is_owned: false }); args.push(CallArgument { value: caller_location, is_owned: false });
} }

View file

@ -630,11 +630,11 @@ fn codegen_stmt<'tcx>(
let to_ty = fx.monomorphize(to_ty); let to_ty = fx.monomorphize(to_ty);
fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.builtin_deref(true) ty.builtin_deref(true).is_some_and(
.map(|ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| { |ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| {
has_ptr_meta(fx.tcx, pointee_ty) has_ptr_meta(fx.tcx, pointee_ty)
}) },
.unwrap_or(false) )
} }
if is_fat_ptr(fx, from_ty) { if is_fat_ptr(fx, from_ty) {

View file

@ -125,8 +125,7 @@ impl CodegenCx<'_, '_> {
// Thread-local variables generally don't support copy relocations. // Thread-local variables generally don't support copy relocations.
let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval) let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
.map(|v| llvm::LLVMIsThreadLocal(v) == llvm::True) .is_some_and(|v| llvm::LLVMIsThreadLocal(v) == llvm::True);
.unwrap_or(false);
if is_thread_local_var { if is_thread_local_var {
return false; return false;
} }

View file

@ -944,7 +944,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == gate) tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == gate)
}; };
let feature_gate_declared = gate_declared(gate); let feature_gate_declared = gate_declared(gate);
let implied_gate_declared = implied_by.map(gate_declared).unwrap_or(false); let implied_gate_declared = implied_by.is_some_and(gate_declared);
if !feature_gate_declared && !implied_gate_declared { if !feature_gate_declared && !implied_gate_declared {
self.check_op(ops::FnCallUnstable(callee, Some(gate))); self.check_op(ops::FnCallUnstable(callee, Some(gate)));
return; return;

View file

@ -285,15 +285,11 @@ pub trait Emitter: Translate {
format!( format!(
"help: {}{}: `{}`", "help: {}{}: `{}`",
&msg, &msg,
if self if self.source_map().is_some_and(|sm| is_case_difference(
.source_map()
.map(|sm| is_case_difference(
sm, sm,
substitution, substitution,
sugg.substitutions[0].parts[0].span, sugg.substitutions[0].parts[0].span,
)) )) {
.unwrap_or(false)
{
" (notice the capitalization)" " (notice the capitalization)"
} else { } else {
"" ""

View file

@ -84,7 +84,7 @@ impl UnstableFeatures {
pub fn from_environment(krate: Option<&str>) -> Self { pub fn from_environment(krate: Option<&str>) -> Self {
// `true` if this is a feature-staged build, i.e., on the beta or stable channel. // `true` if this is a feature-staged build, i.e., on the beta or stable channel.
let disable_unstable_features = let disable_unstable_features =
option_env!("CFG_DISABLE_UNSTABLE_FEATURES").map(|s| s != "0").unwrap_or(false); option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some_and(|s| s != "0");
// Returns whether `krate` should be counted as unstable // Returns whether `krate` should be counted as unstable
let is_unstable_crate = let is_unstable_crate =
|var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name)); |var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));

View file

@ -1748,8 +1748,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|err: &mut Diagnostic, |err: &mut Diagnostic,
found_to_exp_is_fallible: bool, found_to_exp_is_fallible: bool,
exp_to_found_is_fallible: bool| { exp_to_found_is_fallible: bool| {
let exp_is_lhs = let exp_is_lhs = expected_ty_expr.is_some_and(|e| self.tcx.hir().is_lhs(e.hir_id));
expected_ty_expr.map(|e| self.tcx.hir().is_lhs(e.hir_id)).unwrap_or(false);
if exp_is_lhs { if exp_is_lhs {
return; return;

View file

@ -1017,8 +1017,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.typeck_results .typeck_results
.borrow() .borrow()
.expr_ty_adjusted_opt(rcvr) .expr_ty_adjusted_opt(rcvr)
.and_then(|ty| expected.map(|expected_ty| expected_ty.peel_refs() == ty.peel_refs())) .zip(expected)
.unwrap_or(false); .is_some_and(|(ty, expected_ty)| expected_ty.peel_refs() == ty.peel_refs());
let prev_call_mutates_and_returns_unit = || { let prev_call_mutates_and_returns_unit = || {
self.typeck_results self.typeck_results
@ -1026,14 +1026,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.type_dependent_def_id(expr.hir_id) .type_dependent_def_id(expr.hir_id)
.map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder()) .map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder())
.and_then(|sig| sig.inputs_and_output.split_last()) .and_then(|sig| sig.inputs_and_output.split_last())
.map(|(output, inputs)| { .is_some_and(|(output, inputs)| {
output.is_unit() output.is_unit()
&& inputs && inputs
.get(0) .get(0)
.and_then(|self_ty| self_ty.ref_mutability()) .and_then(|self_ty| self_ty.ref_mutability())
.is_some_and(rustc_ast::Mutability::is_mut) .is_some_and(rustc_ast::Mutability::is_mut)
}) })
.unwrap_or(false)
}; };
if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) { if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) {
@ -1200,10 +1199,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
} }
let has_self = path_segs let has_self =
.last() path_segs.last().is_some_and(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self);
.map(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self)
.unwrap_or(false);
let (res, self_ctor_substs) = if let Res::SelfCtor(impl_def_id) = res { let (res, self_ctor_substs) = if let Res::SelfCtor(impl_def_id) = res {
let ty = self.handle_raw_ty(span, tcx.at(span).type_of(impl_def_id).subst_identity()); let ty = self.handle_raw_ty(span, tcx.at(span).type_of(impl_def_id).subst_identity());

View file

@ -972,15 +972,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut obligations_should_hold = Vec::new(); let mut obligations_should_hold = Vec::new();
// Checks if a root variable implements any of the auto traits // Checks if a root variable implements any of the auto traits
for check_trait in auto_traits_def_id.iter() { for check_trait in auto_traits_def_id.iter() {
obligations_should_hold.push( obligations_should_hold.push(check_trait.is_some_and(|check_trait| {
check_trait
.map(|check_trait| {
self.infcx self.infcx
.type_implements_trait(check_trait, [ty], self.param_env) .type_implements_trait(check_trait, [ty], self.param_env)
.must_apply_modulo_regions() .must_apply_modulo_regions()
}) }));
.unwrap_or(false),
);
} }
let mut problematic_captures = FxHashMap::default(); let mut problematic_captures = FxHashMap::default();
@ -996,15 +992,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Checks if a capture implements any of the auto traits // Checks if a capture implements any of the auto traits
let mut obligations_holds_for_capture = Vec::new(); let mut obligations_holds_for_capture = Vec::new();
for check_trait in auto_traits_def_id.iter() { for check_trait in auto_traits_def_id.iter() {
obligations_holds_for_capture.push( obligations_holds_for_capture.push(check_trait.is_some_and(|check_trait| {
check_trait
.map(|check_trait| {
self.infcx self.infcx
.type_implements_trait(check_trait, [ty], self.param_env) .type_implements_trait(check_trait, [ty], self.param_env)
.must_apply_modulo_regions() .must_apply_modulo_regions()
}) }));
.unwrap_or(false),
);
} }
let mut capture_problems = FxHashSet::default(); let mut capture_problems = FxHashSet::default();

View file

@ -383,9 +383,8 @@ impl LateLintPass<'_> for Diagnostics {
debug!(?span, ?def_id, ?substs); debug!(?span, ?def_id, ?substs);
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs) let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs)
.ok() .ok()
.and_then(|inst| inst) .flatten()
.map(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics)) .is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics));
.unwrap_or(false);
if !has_attr { if !has_attr {
return; return;
} }

View file

@ -2610,7 +2610,7 @@ impl<'a> Parser<'a> {
let TyKind::Path(qself, path) = &ty.kind else { return Ok(()) }; let TyKind::Path(qself, path) = &ty.kind else { return Ok(()) };
let qself_position = qself.as_ref().map(|qself| qself.position); let qself_position = qself.as_ref().map(|qself| qself.position);
for (i, segments) in path.segments.windows(2).enumerate() { for (i, segments) in path.segments.windows(2).enumerate() {
if qself_position.map(|pos| i < pos).unwrap_or(false) { if qself_position.is_some_and(|pos| i < pos) {
continue; continue;
} }
if let [a, b] = segments { if let [a, b] = segments {

View file

@ -807,15 +807,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
); );
let is_allowed_through_unstable_modules = |def_id| { let is_allowed_through_unstable_modules = |def_id| {
self.tcx self.tcx.lookup_stability(def_id).is_some_and(|stab| match stab.level {
.lookup_stability(def_id)
.map(|stab| match stab.level {
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => { StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
allowed_through_unstable_modules allowed_through_unstable_modules
} }
_ => false, _ => false,
}) })
.unwrap_or(false)
}; };
if item_is_allowed && !is_allowed_through_unstable_modules(def_id) { if item_is_allowed && !is_allowed_through_unstable_modules(def_id) {

View file

@ -117,16 +117,11 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
match item.kind { match item.kind {
ast::UseTreeKind::Simple(Some(ident)) => { ast::UseTreeKind::Simple(Some(ident)) => {
if ident.name == kw::Underscore if ident.name == kw::Underscore
&& !self && !self.r.import_res_map.get(&id).is_some_and(|per_ns| {
.r
.import_res_map
.get(&id)
.map(|per_ns| {
per_ns.iter().filter_map(|res| res.as_ref()).any(|res| { per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
}) })
}) })
.unwrap_or(false)
{ {
self.unused_import(self.base_id).add(id); self.unused_import(self.base_id).add(id);
} }

View file

@ -197,8 +197,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
.sess .sess
.source_map() .source_map()
.span_to_snippet(span) .span_to_snippet(span)
.map(|snippet| snippet.ends_with(')')) .is_ok_and(|snippet| snippet.ends_with(')'))
.unwrap_or(false)
} }
Res::Def( Res::Def(
DefKind::Ctor(..) | DefKind::AssocFn | DefKind::Const | DefKind::AssocConst, DefKind::Ctor(..) | DefKind::AssocFn | DefKind::Const | DefKind::AssocConst,

View file

@ -823,8 +823,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let is_allowed = |feature| { let is_allowed = |feature| {
self.active_features.contains(&feature) || span.allows_unstable(feature) self.active_features.contains(&feature) || span.allows_unstable(feature)
}; };
let allowed_by_implication = let allowed_by_implication = implied_by.is_some_and(|feature| is_allowed(feature));
implied_by.map(|feature| is_allowed(feature)).unwrap_or(false);
if !is_allowed(feature) && !allowed_by_implication { if !is_allowed(feature) && !allowed_by_implication {
let lint_buffer = &mut self.lint_buffer; let lint_buffer = &mut self.lint_buffer;
let soft_handler = let soft_handler =