1
Fork 0

Use Option::map_or instead of .map(..).unwrap_or(..)

This commit is contained in:
LingMan 2021-01-11 20:45:33 +01:00
parent d03fe84169
commit a56bffb4f9
50 changed files with 67 additions and 79 deletions

View file

@ -1259,8 +1259,8 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: &'tcx ty
let layout = tcx.layout_of(param_env.and(ty));
// We are currently checking the type this field came from, so it must be local
let span = tcx.hir().span_if_local(field.did).unwrap();
let zst = layout.map(|layout| layout.is_zst()).unwrap_or(false);
let align1 = layout.map(|layout| layout.align.abi.bytes() == 1).unwrap_or(false);
let zst = layout.map_or(false, |layout| layout.is_zst());
let align1 = layout.map_or(false, |layout| layout.align.abi.bytes() == 1);
(span, zst, align1)
});

View file

@ -364,13 +364,14 @@ fn check_region_bounds_on_impl_item<'tcx>(
if trait_params != impl_params {
let item_kind = assoc_item_kind_str(impl_m);
let def_span = tcx.sess.source_map().guess_head_span(span);
let span = tcx.hir().get_generics(impl_m.def_id).map(|g| g.span).unwrap_or(def_span);
let span = tcx.hir().get_generics(impl_m.def_id).map_or(def_span, |g| g.span);
let generics_span = if let Some(sp) = tcx.hir().span_if_local(trait_m.def_id) {
let def_sp = tcx.sess.source_map().guess_head_span(sp);
Some(tcx.hir().get_generics(trait_m.def_id).map(|g| g.span).unwrap_or(def_sp))
Some(tcx.hir().get_generics(trait_m.def_id).map_or(def_sp, |g| g.span))
} else {
None
};
tcx.sess.emit_err(LifetimesOrBoundsMismatchOnTrait {
span,
item_kind,

View file

@ -904,8 +904,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
// Return directly on cache hit. This is useful to avoid doubly reporting
// errors with default match binding modes. See #44614.
let def =
cached_result.map(|(kind, def_id)| Res::Def(kind, def_id)).unwrap_or(Res::Err);
let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id));
return (def, Some(ty), slice::from_ref(&**item_segment));
}
let item_name = item_segment.ident;
@ -932,7 +931,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Write back the new resolution.
self.write_resolution(hir_id, result);
(
result.map(|(kind, def_id)| Res::Def(kind, def_id)).unwrap_or(Res::Err),
result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
Some(ty),
slice::from_ref(&**item_segment),
)

View file

@ -816,7 +816,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Some(match &elem.kind {
// Point at the tail expression when possible.
hir::ExprKind::Block(block, _) => {
block.expr.as_ref().map(|e| e.span).unwrap_or(block.span)
block.expr.as_ref().map_or(block.span, |e| e.span)
}
_ => elem.span,
})
@ -888,7 +888,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Write back the new resolution.
self.write_resolution(hir_id, result);
(result.map(|(kind, def_id)| Res::Def(kind, def_id)).unwrap_or(Res::Err), ty)
(result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)), ty)
}
QPath::LangItem(lang_item, span) => {
self.resolve_lang_item_path(lang_item, span, hir_id)

View file

@ -1193,7 +1193,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.any(|imp_did| {
let imp = self.tcx.impl_trait_ref(imp_did).unwrap();
let imp_simp = simplify_type(self.tcx, imp.self_ty(), true);
imp_simp.map(|s| s == simp_rcvr_ty).unwrap_or(false)
imp_simp.map_or(false, |s| s == simp_rcvr_ty)
})
{
explicitly_negative.push(candidate);
@ -1270,11 +1270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match ty.kind() {
ty::Adt(def, _) => def.did.is_local(),
ty::Foreign(did) => did.is_local(),
ty::Dynamic(ref tr, ..) => {
tr.principal().map(|d| d.def_id().is_local()).unwrap_or(false)
}
ty::Dynamic(ref tr, ..) => tr.principal().map_or(false, |d| d.def_id().is_local()),
ty::Param(_) => true,
// Everything else (primitive types, etc.) is effectively

View file

@ -108,7 +108,7 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
.impl_trait_ref(tcx.hir().local_def_id(item.hir_id))
.map_or(false, |trait_ref| tcx.trait_is_auto(trait_ref.def_id));
if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
let sp = impl_.of_trait.as_ref().map(|t| t.path.span).unwrap_or(item.span);
let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
let mut err =
tcx.sess.struct_span_err(sp, "impls of auto traits cannot be default");
err.span_labels(impl_.defaultness_span, "default because of this");

View file

@ -1656,7 +1656,7 @@ fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
..
}) => {
if is_rustc_reservation {
let span = span.to(of_trait.as_ref().map(|t| t.path.span).unwrap_or(*span));
let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
tcx.sess.span_err(span, "reservation impls can't be negative");
}
ty::ImplPolarity::Negative

View file

@ -654,9 +654,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
// Then we see that to get the same result, we must start with
// `deref { deref { place_foo }}` instead of `place_foo` since the pattern is now `Some(x,)`
// and not `&&Some(x,)`, even though its assigned type is that of `&&Some(x,)`.
for _ in
0..self.typeck_results.pat_adjustments().get(pat.hir_id).map(|v| v.len()).unwrap_or(0)
{
for _ in 0..self.typeck_results.pat_adjustments().get(pat.hir_id).map_or(0, |v| v.len()) {
debug!("cat_pattern: applying adjustment to place_with_id={:?}", place_with_id);
place_with_id = self.cat_deref(pat, place_with_id)?;
}

View file

@ -99,7 +99,7 @@ impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> {
// we walk the crates again and re-calculate predicates for all
// items.
let item_predicates_len: usize =
self.global_inferred_outlives.get(&item_did.to_def_id()).map(|p| p.len()).unwrap_or(0);
self.global_inferred_outlives.get(&item_did.to_def_id()).map_or(0, |p| p.len());
if item_required_predicates.len() > item_predicates_len {
*self.predicates_added = true;
self.global_inferred_outlives.insert(item_did.to_def_id(), item_required_predicates);