Skip to content

Commit 5e6e1e3

Browse files
committed
Auto merge of #79817 - LingMan:if_map, r=lcnr
Replace simple `if let` constructs with Option::map Replaces a few constructs of the form ``` if let Some(x) = var { Some(...) } else { None } ``` with calls to `Option::map`. `@rustbot` modify labels +C-cleanup +T-compiler
2 parents 5019791 + af9402a commit 5e6e1e3

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

compiler/rustc_typeck/src/check/demand.rs

+13-41
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
361361
}
362362

363363
fn replace_prefix(&self, s: &str, old: &str, new: &str) -> Option<String> {
364-
if let Some(stripped) = s.strip_prefix(old) {
365-
Some(new.to_string() + stripped)
366-
} else {
367-
None
368-
}
364+
s.strip_prefix(old).map(|stripped| new.to_string() + stripped)
369365
}
370366

371367
/// This function is used to determine potential "simple" improvements or users' errors and
@@ -587,47 +583,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
587583
hir::Mutability::Mut => {
588584
let new_prefix = "&mut ".to_owned() + derefs;
589585
match mutbl_a {
590-
hir::Mutability::Mut => {
591-
if let Some(s) =
592-
self.replace_prefix(&src, "&mut ", &new_prefix)
593-
{
594-
Some((s, Applicability::MachineApplicable))
595-
} else {
596-
None
597-
}
598-
}
599-
hir::Mutability::Not => {
600-
if let Some(s) =
601-
self.replace_prefix(&src, "&", &new_prefix)
602-
{
603-
Some((s, Applicability::Unspecified))
604-
} else {
605-
None
606-
}
607-
}
586+
hir::Mutability::Mut => self
587+
.replace_prefix(&src, "&mut ", &new_prefix)
588+
.map(|s| (s, Applicability::MachineApplicable)),
589+
hir::Mutability::Not => self
590+
.replace_prefix(&src, "&", &new_prefix)
591+
.map(|s| (s, Applicability::Unspecified)),
608592
}
609593
}
610594
hir::Mutability::Not => {
611595
let new_prefix = "&".to_owned() + derefs;
612596
match mutbl_a {
613-
hir::Mutability::Mut => {
614-
if let Some(s) =
615-
self.replace_prefix(&src, "&mut ", &new_prefix)
616-
{
617-
Some((s, Applicability::MachineApplicable))
618-
} else {
619-
None
620-
}
621-
}
622-
hir::Mutability::Not => {
623-
if let Some(s) =
624-
self.replace_prefix(&src, "&", &new_prefix)
625-
{
626-
Some((s, Applicability::MachineApplicable))
627-
} else {
628-
None
629-
}
630-
}
597+
hir::Mutability::Mut => self
598+
.replace_prefix(&src, "&mut ", &new_prefix)
599+
.map(|s| (s, Applicability::MachineApplicable)),
600+
hir::Mutability::Not => self
601+
.replace_prefix(&src, "&", &new_prefix)
602+
.map(|s| (s, Applicability::MachineApplicable)),
631603
}
632604
}
633605
} {

0 commit comments

Comments
 (0)