Skip to content

Commit 3bd865c

Browse files
committed
Use OptionExt::is_none_or in the compiler
1 parent ddc72fe commit 3bd865c

File tree

36 files changed

+78
-42
lines changed

36 files changed

+78
-42
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_ast as ast;
22
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
33
use rustc_ast::{attr, AssocConstraint, AssocConstraintKind, NodeId};
44
use rustc_ast::{PatKind, RangeEnd};
5+
use rustc_data_structures::OptionExt as _;
56
use rustc_errors::{Applicability, StashKey};
67
use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP};
78
use rustc_session::parse::{feature_err, feature_err_issue, feature_warn};
@@ -101,7 +102,7 @@ impl<'a> PostExpansionVisitor<'a> {
101102
.emit();
102103
}
103104
Err(abi::AbiDisabled::Unrecognized) => {
104-
if self.sess.opts.pretty.map_or(true, |ppm| ppm.needs_hir()) {
105+
if self.sess.opts.pretty.is_none_or(|ppm| ppm.needs_hir()) {
105106
self.sess.parse_sess.span_diagnostic.delay_span_bug(
106107
span,
107108
format!(

compiler/rustc_attr/src/builtin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use rustc_ast::{self as ast, attr};
44
use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem, NodeId};
55
use rustc_ast_pretty::pprust;
6+
use rustc_data_structures::OptionExt as _;
67
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
78
use rustc_macros::HashStable_Generic;
89
use rustc_session::config::ExpectedValues;
@@ -1096,7 +1097,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10961097
// the `check_mod_attrs` pass, but this pass doesn't always run
10971098
// (e.g. if we only pretty-print the source), so we have to gate
10981099
// the `delay_span_bug` call as follows:
1099-
if sess.opts.pretty.map_or(true, |pp| pp.needs_analysis()) {
1100+
if sess.opts.pretty.is_none_or(|pp| pp.needs_analysis()) {
11001101
diagnostic.delay_span_bug(item.span(), "unrecognized representation hint");
11011102
}
11021103
}

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::iter;
33
use either::Either;
44
use rustc_data_structures::captures::Captures;
55
use rustc_data_structures::fx::FxIndexSet;
6+
use rustc_data_structures::OptionExt as _;
67
use rustc_errors::{
78
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
89
};
@@ -1328,7 +1329,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13281329
&& ex.span.contains(self.borrow_span)
13291330
// To support cases like `|| { v.call(|this| v.get()) }`
13301331
// FIXME: actually support such cases (need to figure out how to move from the capture place to original local)
1331-
&& self.res.as_ref().map_or(true, |(prev_res, _)| prev_res.span.contains(ex.span))
1332+
&& self.res.as_ref().is_none_or(|(prev_res, _)| prev_res.span.contains(ex.span))
13321333
{
13331334
self.res = Some((ex, closure));
13341335
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use either::Either;
1010
use hir::OpaqueTyOrigin;
1111
use rustc_data_structures::frozen::Frozen;
1212
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
13+
use rustc_data_structures::OptionExt as _;
1314
use rustc_hir as hir;
1415
use rustc_hir::def::DefKind;
1516
use rustc_hir::def_id::LocalDefId;
@@ -1812,7 +1813,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18121813
// than 1.
18131814
// If the length is larger than 1, the repeat expression will need to copy the
18141815
// element, so we require the `Copy` trait.
1815-
if len.try_eval_target_usize(tcx, self.param_env).map_or(true, |len| len > 1) {
1816+
if len.try_eval_target_usize(tcx, self.param_env).is_none_or(|len| len > 1) {
18161817
match operand {
18171818
Operand::Copy(..) | Operand::Constant(..) => {
18181819
// These are always okay: direct use of a const, or a value that can evidently be copied.

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use super::validity::RefTracking;
1818
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
19+
use rustc_data_structures::OptionExt as _;
1920
use rustc_errors::ErrorGuaranteed;
2021
use rustc_hir as hir;
2122
use rustc_middle::mir::interpret::InterpResult;
@@ -114,7 +115,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval:
114115
if let InternMode::Static(mutability) = mode {
115116
// For this, we need to take into account `UnsafeCell`. When `ty` is `None`, we assume
116117
// no interior mutability.
117-
let frozen = ty.map_or(true, |ty| ty.is_freeze(*ecx.tcx, ecx.param_env));
118+
let frozen = ty.is_none_or(|ty| ty.is_freeze(*ecx.tcx, ecx.param_env));
118119
// For statics, allocation mutability is the combination of place mutability and
119120
// type mutability.
120121
// The entire allocation needs to be mutable if it contains an `UnsafeCell` anywhere.

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::ptr;
1414

1515
use rustc_ast::Mutability;
1616
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
17+
use rustc_data_structures::OptionExt as _;
1718
use rustc_middle::mir::display_allocation;
1819
use rustc_middle::ty::{self, Instance, ParamEnv, Ty, TyCtxt};
1920
use rustc_target::abi::{Align, HasDataLayout, Size};
@@ -422,7 +423,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
422423
let (alloc_size, alloc_align, ret_val) = alloc_size(alloc_id, offset, prov)?;
423424
// Test bounds. This also ensures non-null.
424425
// It is sufficient to check this for the end pointer. Also check for overflow!
425-
if offset.checked_add(size, &self.tcx).map_or(true, |end| end > alloc_size) {
426+
if offset.checked_add(size, &self.tcx).is_none_or(|end| end > alloc_size) {
426427
throw_ub!(PointerOutOfBounds {
427428
alloc_id,
428429
alloc_size,

compiler/rustc_const_eval/src/interpret/projection.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
use either::{Left, Right};
1111

12+
use rustc_data_structures::OptionExt as _;
1213
use rustc_middle::mir;
1314
use rustc_middle::ty;
1415
use rustc_middle::ty::layout::LayoutOf;
@@ -294,7 +295,7 @@ where
294295
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
295296
let len = base.len(self)?; // also asserts that we have a type where this makes sense
296297
let actual_to = if from_end {
297-
if from.checked_add(to).map_or(true, |to| to > len) {
298+
if from.checked_add(to).is_none_or(|to| to > len) {
298299
// This can only be reached in ConstProp and non-rustc-MIR.
299300
throw_ub!(BoundsCheckFailed { len: len, index: from.saturating_add(to) });
300301
}

compiler/rustc_data_structures/src/option_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
pub trait OptionExt {
33
type T;
44

5+
/// Returns `true` if `self` is `None` or the value inside the `Some` matches a predicate `f`.
56
fn is_none_or(self, f: impl FnOnce(Self::T) -> bool) -> bool;
67
}
78

89
impl<T> OptionExt for Option<T> {
910
type T = T;
1011

11-
/// Returns `true` is `self` is `None` or the value inside matches a predicate `f`.
1212
fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool {
1313
match self {
1414
None => true,

compiler/rustc_expand/src/config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_ast::{self as ast, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem
1414
use rustc_attr as attr;
1515
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1616
use rustc_data_structures::fx::FxHashMap;
17+
use rustc_data_structures::OptionExt as _;
1718
use rustc_feature::{Feature, Features, State as FeatureState};
1819
use rustc_feature::{
1920
ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES, STABLE_REMOVED_FEATURES,
@@ -427,15 +428,15 @@ impl<'a> StripUnconfigured<'a> {
427428
return true;
428429
}
429430
};
430-
parse_cfg(&meta_item, &self.sess).map_or(true, |meta_item| {
431+
parse_cfg(&meta_item, &self.sess).is_none_or(|meta_item| {
431432
attr::cfg_matches(&meta_item, &self.sess.parse_sess, self.lint_node_id, self.features)
432433
})
433434
}
434435

435436
/// If attributes are not allowed on expressions, emit an error for `attr`
436437
#[instrument(level = "trace", skip(self))]
437438
pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
438-
if !self.features.map_or(true, |features| features.stmt_expr_attributes) {
439+
if !self.features.is_none_or(|features| features.stmt_expr_attributes) {
439440
let mut err = feature_err(
440441
&self.sess.parse_sess,
441442
sym::stmt_expr_attributes,

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::mbe::{
77
use rustc_ast::token::{self, Token, TokenKind};
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_ast_pretty::pprust;
10+
use rustc_data_structures::OptionExt as _;
1011
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, DiagnosticMessage};
1112
use rustc_parse::parser::{Parser, Recovery};
1213
use rustc_span::source_map::SourceMap;
@@ -155,7 +156,7 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx,
155156
if self
156157
.best_failure
157158
.as_ref()
158-
.map_or(true, |failure| failure.is_better_position(*approx_position))
159+
.is_none_or(|failure| failure.is_better_position(*approx_position))
159160
{
160161
self.best_failure = Some(BestFailure {
161162
token: token.clone(),

0 commit comments

Comments
 (0)