Skip to content

Commit

Permalink
Use is_some_and/is_ok_and in less obvious spots
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed May 24, 2023
1 parent fb0f74a commit 307799a
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 88 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl<'a> AstValidator<'a> {
let source_map = self.session.source_map();
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
} else {
sp.shrink_to_hi()
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
if info.tail_result_is_ignored {
// #85581: If the first mutable borrow's scope contains
// the second borrow, this suggestion isn't helpful.
if !multiple_borrow_span
.map(|(old, new)| {
old.to(info.span.shrink_to_hi()).contains(new)
})
.unwrap_or(false)
{
if !multiple_borrow_span.is_some_and(|(old, new)| {
old.to(info.span.shrink_to_hi()).contains(new)
}) {
err.span_suggestion_verbose(
info.span.shrink_to_hi(),
"consider adding semicolon after the expression so its \
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
.body
.local_decls
.get(local)
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local]))
.unwrap_or(false) =>
.is_some_and(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])) =>
{
let decl = &self.body.local_decls[local];
err.span_label(span, format!("cannot {act}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ impl OutlivesSuggestionBuilder {
|(r, _)| {
self.constraints_to_add
.get(r)
.map(|r_outlived| r_outlived.as_slice().contains(fr))
.unwrap_or(false)
.is_some_and(|r_outlived| r_outlived.as_slice().contains(fr))
},
);

Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_codegen_cranelift/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
let is_cold = if fn_sig.abi() == Abi::RustCold {
true
} else {
instance
.map(|inst| {
fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD)
})
.unwrap_or(false)
instance.is_some_and(|inst| {
fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD)
})
};
if is_cold {
fx.bcx.set_cold_block(fx.bcx.current_block().unwrap());
Expand Down Expand Up @@ -470,7 +468,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
};

// 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);
args.push(CallArgument { value: caller_location, is_owned: false });
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,11 @@ fn codegen_stmt<'tcx>(
let to_ty = fx.monomorphize(to_ty);

fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.builtin_deref(true)
.map(|ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| {
ty.builtin_deref(true).is_some_and(
|ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| {
has_ptr_meta(fx.tcx, pointee_ty)
})
.unwrap_or(false)
},
)
}

if is_fat_ptr(fx, from_ty) {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/mono_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ impl CodegenCx<'_, '_> {

// Thread-local variables generally don't support copy relocations.
let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
.map(|v| llvm::LLVMIsThreadLocal(v) == llvm::True)
.unwrap_or(false);
.is_some_and(|v| llvm::LLVMIsThreadLocal(v) == llvm::True);
if is_thread_local_var {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == 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 {
self.check_op(ops::FnCallUnstable(callee, Some(gate)));
return;
Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,11 @@ pub trait Emitter: Translate {
format!(
"help: {}{}: `{}`",
&msg,
if self
.source_map()
.map(|sm| is_case_difference(
sm,
substitution,
sugg.substitutions[0].parts[0].span,
))
.unwrap_or(false)
{
if self.source_map().is_some_and(|sm| is_case_difference(
sm,
substitution,
sugg.substitutions[0].parts[0].span,
)) {
" (notice the capitalization)"
} else {
""
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl UnstableFeatures {
pub fn from_environment(krate: Option<&str>) -> Self {
// `true` if this is a feature-staged build, i.e., on the beta or stable channel.
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
let is_unstable_crate =
|var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,8 +1748,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|err: &mut Diagnostic,
found_to_exp_is_fallible: bool,
exp_to_found_is_fallible: bool| {
let exp_is_lhs =
expected_ty_expr.map(|e| self.tcx.hir().is_lhs(e.hir_id)).unwrap_or(false);
let exp_is_lhs = expected_ty_expr.is_some_and(|e| self.tcx.hir().is_lhs(e.hir_id));

if exp_is_lhs {
return;
Expand Down
13 changes: 5 additions & 8 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,23 +1017,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.typeck_results
.borrow()
.expr_ty_adjusted_opt(rcvr)
.and_then(|ty| expected.map(|expected_ty| expected_ty.peel_refs() == ty.peel_refs()))
.unwrap_or(false);
.zip(expected)
.is_some_and(|(ty, expected_ty)| expected_ty.peel_refs() == ty.peel_refs());

let prev_call_mutates_and_returns_unit = || {
self.typeck_results
.borrow()
.type_dependent_def_id(expr.hir_id)
.map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder())
.and_then(|sig| sig.inputs_and_output.split_last())
.map(|(output, inputs)| {
.is_some_and(|(output, inputs)| {
output.is_unit()
&& inputs
.get(0)
.and_then(|self_ty| self_ty.ref_mutability())
.is_some_and(rustc_ast::Mutability::is_mut)
})
.unwrap_or(false)
};

if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) {
Expand Down Expand Up @@ -1200,10 +1199,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let has_self = path_segs
.last()
.map(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self)
.unwrap_or(false);
let has_self =
path_segs.last().is_some_and(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self);

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());
Expand Down
28 changes: 10 additions & 18 deletions compiler/rustc_hir_typeck/src/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,15 +972,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut obligations_should_hold = Vec::new();
// Checks if a root variable implements any of the auto traits
for check_trait in auto_traits_def_id.iter() {
obligations_should_hold.push(
check_trait
.map(|check_trait| {
self.infcx
.type_implements_trait(check_trait, [ty], self.param_env)
.must_apply_modulo_regions()
})
.unwrap_or(false),
);
obligations_should_hold.push(check_trait.is_some_and(|check_trait| {
self.infcx
.type_implements_trait(check_trait, [ty], self.param_env)
.must_apply_modulo_regions()
}));
}

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

let mut capture_problems = FxHashSet::default();
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,8 @@ impl LateLintPass<'_> for Diagnostics {
debug!(?span, ?def_id, ?substs);
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs)
.ok()
.and_then(|inst| inst)
.map(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics))
.unwrap_or(false);
.flatten()
.is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics));
if !has_attr {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2610,7 +2610,7 @@ impl<'a> Parser<'a> {
let TyKind::Path(qself, path) = &ty.kind else { return Ok(()) };
let qself_position = qself.as_ref().map(|qself| qself.position);
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;
}
if let [a, b] = segments {
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,15 +807,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
);

let is_allowed_through_unstable_modules = |def_id| {
self.tcx
.lookup_stability(def_id)
.map(|stab| match stab.level {
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
allowed_through_unstable_modules
}
_ => false,
})
.unwrap_or(false)
self.tcx.lookup_stability(def_id).is_some_and(|stab| match stab.level {
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
allowed_through_unstable_modules
}
_ => false,
})
};

if item_is_allowed && !is_allowed_through_unstable_modules(def_id) {
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,11 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
match item.kind {
ast::UseTreeKind::Simple(Some(ident)) => {
if ident.name == kw::Underscore
&& !self
.r
.import_res_map
.get(&id)
.map(|per_ns| {
per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
})
&& !self.r.import_res_map.get(&id).is_some_and(|per_ns| {
per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
})
.unwrap_or(false)
})
{
self.unused_import(self.base_id).add(id);
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
.sess
.source_map()
.span_to_snippet(span)
.map(|snippet| snippet.ends_with(')'))
.unwrap_or(false)
.is_ok_and(|snippet| snippet.ends_with(')'))
}
Res::Def(
DefKind::Ctor(..) | DefKind::AssocFn | DefKind::Const | DefKind::AssocConst,
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let is_allowed = |feature| {
self.active_features.contains(&feature) || span.allows_unstable(feature)
};
let allowed_by_implication =
implied_by.map(|feature| is_allowed(feature)).unwrap_or(false);
let allowed_by_implication = implied_by.is_some_and(|feature| is_allowed(feature));
if !is_allowed(feature) && !allowed_by_implication {
let lint_buffer = &mut self.lint_buffer;
let soft_handler =
Expand Down

0 comments on commit 307799a

Please sign in to comment.