Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d0b3ebe

Browse files
authored
Rollup merge of rust-lang#111912 - WaffleLapkin:is_some_and_in_the_compiler, r=petrochenkov
Use `Option::is_some_and` and `Result::is_ok_and` in the compiler `.is_some_and(..)`/`.is_ok_and(..)` replace `.map_or(false, ..)` and `.map(..).unwrap_or(false)`, making the code more readable. This PR is a sibling of rust-lang#111873 (comment)
2 parents 33ded73 + 307799a commit d0b3ebe

File tree

97 files changed

+201
-246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+201
-246
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,10 +2391,10 @@ pub struct FnDecl {
23912391

23922392
impl FnDecl {
23932393
pub fn has_self(&self) -> bool {
2394-
self.inputs.get(0).map_or(false, Param::is_self)
2394+
self.inputs.get(0).is_some_and(Param::is_self)
23952395
}
23962396
pub fn c_variadic(&self) -> bool {
2397-
self.inputs.last().map_or(false, |arg| matches!(arg.ty.kind, TyKind::CVarArgs))
2397+
self.inputs.last().is_some_and(|arg| matches!(arg.ty.kind, TyKind::CVarArgs))
23982398
}
23992399
}
24002400

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl Attribute {
149149
}
150150

151151
pub fn may_have_doc_links(&self) -> bool {
152-
self.doc_str().map_or(false, |s| comments::may_have_doc_links(s.as_str()))
152+
self.doc_str().is_some_and(|s| comments::may_have_doc_links(s.as_str()))
153153
}
154154

155155
pub fn is_proc_macro_attr(&self) -> bool {
@@ -441,12 +441,12 @@ impl NestedMetaItem {
441441

442442
/// Returns `true` if this list item is a MetaItem with a name of `name`.
443443
pub fn has_name(&self, name: Symbol) -> bool {
444-
self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
444+
self.meta_item().is_some_and(|meta_item| meta_item.has_name(name))
445445
}
446446

447447
/// Returns `true` if `self` is a `MetaItem` and the meta item is a word.
448448
pub fn is_word(&self) -> bool {
449-
self.meta_item().map_or(false, |meta_item| meta_item.is_word())
449+
self.meta_item().is_some_and(|meta_item| meta_item.is_word())
450450
}
451451

452452
/// Gets a list of inner meta items from a list `MetaItem` type.

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl Token {
607607
/// Returns `true` if the token is an identifier whose name is the given
608608
/// string slice.
609609
pub fn is_ident_named(&self, name: Symbol) -> bool {
610-
self.ident().map_or(false, |(ident, _)| ident.name == name)
610+
self.ident().is_some_and(|(ident, _)| ident.name == name)
611611
}
612612

613613
/// Returns `true` if the token is an interpolated path.

compiler/rustc_ast/src/util/literal.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitErr
392392
// Small bases are lexed as if they were base 10, e.g, the string
393393
// might be `0b10201`. This will cause the conversion above to fail,
394394
// but these kinds of errors are already reported by the lexer.
395-
let from_lexer =
396-
base < 10 && s.chars().any(|c| c.to_digit(10).map_or(false, |d| d >= base));
395+
let from_lexer = base < 10 && s.chars().any(|c| c.to_digit(10).is_some_and(|d| d >= base));
397396
if from_lexer { LitError::LexerError } else { LitError::IntTooLarge(base) }
398397
})
399398
}

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
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_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
317317
match i.kind {
318318
ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
319319
let link_name = attr::first_attr_value_str_by_name(&i.attrs, sym::link_name);
320-
let links_to_llvm =
321-
link_name.map_or(false, |val| val.as_str().starts_with("llvm."));
320+
let links_to_llvm = link_name.is_some_and(|val| val.as_str().starts_with("llvm."));
322321
if links_to_llvm {
323322
gate_feature_post!(
324323
&self,

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
118118
let path_span = path_span.unwrap();
119119
// path_span is only present in the case of closure capture
120120
assert!(matches!(later_use_kind, LaterUseKind::ClosureCapture));
121-
if !borrow_span.map_or(false, |sp| sp.overlaps(var_or_use_span)) {
121+
if !borrow_span.is_some_and(|sp| sp.overlaps(var_or_use_span)) {
122122
let path_label = "used here by closure";
123123
let capture_kind_label = message;
124124
err.span_label(
@@ -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/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11561156
ty::Adt(def, ..) => Some(def.did()),
11571157
_ => None,
11581158
});
1159-
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
1159+
let is_option_or_result = parent_self_ty.is_some_and(|def_id| {
11601160
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
11611161
});
11621162
if is_option_or_result && maybe_reinitialized_locations_is_empty {

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 2 additions & 3 deletions
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}"));
@@ -443,7 +442,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
443442
.sess
444443
.source_map()
445444
.span_to_snippet(span)
446-
.map_or(false, |snippet| snippet.starts_with("&mut ")) =>
445+
.is_ok_and(|snippet| snippet.starts_with("&mut ")) =>
447446
{
448447
err.span_label(span, format!("cannot {act}"));
449448
err.span_suggestion(

compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs

Lines changed: 1 addition & 2 deletions
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

0 commit comments

Comments
 (0)