Skip to content

Commit 307799a

Browse files
committed
Use is_some_and/is_ok_and in less obvious spots
1 parent fb0f74a commit 307799a

File tree

19 files changed

+53
-88
lines changed

19 files changed

+53
-88
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'a> AstValidator<'a> {
348348
let source_map = self.session.source_map();
349349
let end = source_map.end_point(sp);
350350

351-
if source_map.span_to_snippet(end).map(|s| s == ";").unwrap_or(false) {
351+
if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
352352
end
353353
} else {
354354
sp.shrink_to_hi()

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
224224
if info.tail_result_is_ignored {
225225
// #85581: If the first mutable borrow's scope contains
226226
// the second borrow, this suggestion isn't helpful.
227-
if !multiple_borrow_span
228-
.map(|(old, new)| {
229-
old.to(info.span.shrink_to_hi()).contains(new)
230-
})
231-
.unwrap_or(false)
232-
{
227+
if !multiple_borrow_span.is_some_and(|(old, new)| {
228+
old.to(info.span.shrink_to_hi()).contains(new)
229+
}) {
233230
err.span_suggestion_verbose(
234231
info.span.shrink_to_hi(),
235232
"consider adding semicolon after the expression so its \

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
289289
.body
290290
.local_decls
291291
.get(local)
292-
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local]))
293-
.unwrap_or(false) =>
292+
.is_some_and(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])) =>
294293
{
295294
let decl = &self.body.local_decls[local];
296295
err.span_label(span, format!("cannot {act}"));

compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ impl OutlivesSuggestionBuilder {
125125
|(r, _)| {
126126
self.constraints_to_add
127127
.get(r)
128-
.map(|r_outlived| r_outlived.as_slice().contains(fr))
129-
.unwrap_or(false)
128+
.is_some_and(|r_outlived| r_outlived.as_slice().contains(fr))
130129
},
131130
);
132131

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
432432
let is_cold = if fn_sig.abi() == Abi::RustCold {
433433
true
434434
} else {
435-
instance
436-
.map(|inst| {
437-
fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD)
438-
})
439-
.unwrap_or(false)
435+
instance.is_some_and(|inst| {
436+
fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD)
437+
})
440438
};
441439
if is_cold {
442440
fx.bcx.set_cold_block(fx.bcx.current_block().unwrap());
@@ -470,7 +468,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
470468
};
471469

472470
// Pass the caller location for `#[track_caller]`.
473-
if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) {
471+
if instance.is_some_and(|inst| inst.def.requires_caller_location(fx.tcx)) {
474472
let caller_location = fx.get_caller_location(source_info);
475473
args.push(CallArgument { value: caller_location, is_owned: false });
476474
}

compiler/rustc_codegen_cranelift/src/base.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,11 @@ fn codegen_stmt<'tcx>(
630630
let to_ty = fx.monomorphize(to_ty);
631631

632632
fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
633-
ty.builtin_deref(true)
634-
.map(|ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| {
633+
ty.builtin_deref(true).is_some_and(
634+
|ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| {
635635
has_ptr_meta(fx.tcx, pointee_ty)
636-
})
637-
.unwrap_or(false)
636+
},
637+
)
638638
}
639639

640640
if is_fat_ptr(fx, from_ty) {

compiler/rustc_codegen_llvm/src/mono_item.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ impl CodegenCx<'_, '_> {
125125

126126
// Thread-local variables generally don't support copy relocations.
127127
let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
128-
.map(|v| llvm::LLVMIsThreadLocal(v) == llvm::True)
129-
.unwrap_or(false);
128+
.is_some_and(|v| llvm::LLVMIsThreadLocal(v) == llvm::True);
130129
if is_thread_local_var {
131130
return false;
132131
}

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
944944
tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == gate)
945945
};
946946
let feature_gate_declared = gate_declared(gate);
947-
let implied_gate_declared = implied_by.map(gate_declared).unwrap_or(false);
947+
let implied_gate_declared = implied_by.is_some_and(gate_declared);
948948
if !feature_gate_declared && !implied_gate_declared {
949949
self.check_op(ops::FnCallUnstable(callee, Some(gate)));
950950
return;

compiler/rustc_errors/src/emitter.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,11 @@ pub trait Emitter: Translate {
285285
format!(
286286
"help: {}{}: `{}`",
287287
&msg,
288-
if self
289-
.source_map()
290-
.map(|sm| is_case_difference(
291-
sm,
292-
substitution,
293-
sugg.substitutions[0].parts[0].span,
294-
))
295-
.unwrap_or(false)
296-
{
288+
if self.source_map().is_some_and(|sm| is_case_difference(
289+
sm,
290+
substitution,
291+
sugg.substitutions[0].parts[0].span,
292+
)) {
297293
" (notice the capitalization)"
298294
} else {
299295
""

compiler/rustc_feature/src/lib.rs

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

compiler/rustc_hir_typeck/src/demand.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1748,8 +1748,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17481748
|err: &mut Diagnostic,
17491749
found_to_exp_is_fallible: bool,
17501750
exp_to_found_is_fallible: bool| {
1751-
let exp_is_lhs =
1752-
expected_ty_expr.map(|e| self.tcx.hir().is_lhs(e.hir_id)).unwrap_or(false);
1751+
let exp_is_lhs = expected_ty_expr.is_some_and(|e| self.tcx.hir().is_lhs(e.hir_id));
17531752

17541753
if exp_is_lhs {
17551754
return;

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1017,23 +1017,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10171017
.typeck_results
10181018
.borrow()
10191019
.expr_ty_adjusted_opt(rcvr)
1020-
.and_then(|ty| expected.map(|expected_ty| expected_ty.peel_refs() == ty.peel_refs()))
1021-
.unwrap_or(false);
1020+
.zip(expected)
1021+
.is_some_and(|(ty, expected_ty)| expected_ty.peel_refs() == ty.peel_refs());
10221022

10231023
let prev_call_mutates_and_returns_unit = || {
10241024
self.typeck_results
10251025
.borrow()
10261026
.type_dependent_def_id(expr.hir_id)
10271027
.map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder())
10281028
.and_then(|sig| sig.inputs_and_output.split_last())
1029-
.map(|(output, inputs)| {
1029+
.is_some_and(|(output, inputs)| {
10301030
output.is_unit()
10311031
&& inputs
10321032
.get(0)
10331033
.and_then(|self_ty| self_ty.ref_mutability())
10341034
.is_some_and(rustc_ast::Mutability::is_mut)
10351035
})
1036-
.unwrap_or(false)
10371036
};
10381037

10391038
if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) {
@@ -1200,10 +1199,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12001199
}
12011200
}
12021201

1203-
let has_self = path_segs
1204-
.last()
1205-
.map(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self)
1206-
.unwrap_or(false);
1202+
let has_self =
1203+
path_segs.last().is_some_and(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self);
12071204

12081205
let (res, self_ctor_substs) = if let Res::SelfCtor(impl_def_id) = res {
12091206
let ty = self.handle_raw_ty(span, tcx.at(span).type_of(impl_def_id).subst_identity());

compiler/rustc_hir_typeck/src/upvar.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -972,15 +972,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
972972
let mut obligations_should_hold = Vec::new();
973973
// Checks if a root variable implements any of the auto traits
974974
for check_trait in auto_traits_def_id.iter() {
975-
obligations_should_hold.push(
976-
check_trait
977-
.map(|check_trait| {
978-
self.infcx
979-
.type_implements_trait(check_trait, [ty], self.param_env)
980-
.must_apply_modulo_regions()
981-
})
982-
.unwrap_or(false),
983-
);
975+
obligations_should_hold.push(check_trait.is_some_and(|check_trait| {
976+
self.infcx
977+
.type_implements_trait(check_trait, [ty], self.param_env)
978+
.must_apply_modulo_regions()
979+
}));
984980
}
985981

986982
let mut problematic_captures = FxHashMap::default();
@@ -996,15 +992,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
996992
// Checks if a capture implements any of the auto traits
997993
let mut obligations_holds_for_capture = Vec::new();
998994
for check_trait in auto_traits_def_id.iter() {
999-
obligations_holds_for_capture.push(
1000-
check_trait
1001-
.map(|check_trait| {
1002-
self.infcx
1003-
.type_implements_trait(check_trait, [ty], self.param_env)
1004-
.must_apply_modulo_regions()
1005-
})
1006-
.unwrap_or(false),
1007-
);
995+
obligations_holds_for_capture.push(check_trait.is_some_and(|check_trait| {
996+
self.infcx
997+
.type_implements_trait(check_trait, [ty], self.param_env)
998+
.must_apply_modulo_regions()
999+
}));
10081000
}
10091001

10101002
let mut capture_problems = FxHashSet::default();

compiler/rustc_lint/src/internal.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,8 @@ impl LateLintPass<'_> for Diagnostics {
383383
debug!(?span, ?def_id, ?substs);
384384
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs)
385385
.ok()
386-
.and_then(|inst| inst)
387-
.map(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics))
388-
.unwrap_or(false);
386+
.flatten()
387+
.is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics));
389388
if !has_attr {
390389
return;
391390
}

compiler/rustc_parse/src/parser/diagnostics.rs

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

compiler/rustc_passes/src/stability.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -807,15 +807,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
807807
);
808808

809809
let is_allowed_through_unstable_modules = |def_id| {
810-
self.tcx
811-
.lookup_stability(def_id)
812-
.map(|stab| match stab.level {
813-
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
814-
allowed_through_unstable_modules
815-
}
816-
_ => false,
817-
})
818-
.unwrap_or(false)
810+
self.tcx.lookup_stability(def_id).is_some_and(|stab| match stab.level {
811+
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
812+
allowed_through_unstable_modules
813+
}
814+
_ => false,
815+
})
819816
};
820817

821818
if item_is_allowed && !is_allowed_through_unstable_modules(def_id) {

compiler/rustc_resolve/src/check_unused.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,11 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
117117
match item.kind {
118118
ast::UseTreeKind::Simple(Some(ident)) => {
119119
if ident.name == kw::Underscore
120-
&& !self
121-
.r
122-
.import_res_map
123-
.get(&id)
124-
.map(|per_ns| {
125-
per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
126-
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
127-
})
120+
&& !self.r.import_res_map.get(&id).is_some_and(|per_ns| {
121+
per_ns.iter().filter_map(|res| res.as_ref()).any(|res| {
122+
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
128123
})
129-
.unwrap_or(false)
124+
})
130125
{
131126
self.unused_import(self.base_id).add(id);
132127
}

compiler/rustc_resolve/src/late/diagnostics.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
197197
.sess
198198
.source_map()
199199
.span_to_snippet(span)
200-
.map(|snippet| snippet.ends_with(')'))
201-
.unwrap_or(false)
200+
.is_ok_and(|snippet| snippet.ends_with(')'))
202201
}
203202
Res::Def(
204203
DefKind::Ctor(..) | DefKind::AssocFn | DefKind::Const | DefKind::AssocConst,

compiler/rustc_resolve/src/macros.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
823823
let is_allowed = |feature| {
824824
self.active_features.contains(&feature) || span.allows_unstable(feature)
825825
};
826-
let allowed_by_implication =
827-
implied_by.map(|feature| is_allowed(feature)).unwrap_or(false);
826+
let allowed_by_implication = implied_by.is_some_and(|feature| is_allowed(feature));
828827
if !is_allowed(feature) && !allowed_by_implication {
829828
let lint_buffer = &mut self.lint_buffer;
830829
let soft_handler =

0 commit comments

Comments
 (0)