Skip to content

Commit f711f3f

Browse files
Rollup merge of #80299 - LingMan:helper, r=lcnr
Turn helper method into a closure `replace_prefix` is currently implemented as a method but has no real relation to the struct it is implemented on. Turn it into a closure and move it into the only method from which it is called. `@rustbot` modify labels +C-cleanup +T-compiler r? `@lcnr`
2 parents 5af144e + ef75761 commit f711f3f

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

compiler/rustc_typeck/src/check/demand.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
360360
false
361361
}
362362

363-
fn replace_prefix(&self, s: &str, old: &str, new: &str) -> Option<String> {
364-
s.strip_prefix(old).map(|stripped| new.to_string() + stripped)
365-
}
366-
367363
/// This function is used to determine potential "simple" improvements or users' errors and
368364
/// provide them useful help. For example:
369365
///
@@ -394,6 +390,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
394390
return None;
395391
}
396392

393+
let replace_prefix = |s: &str, old: &str, new: &str| {
394+
s.strip_prefix(old).map(|stripped| new.to_string() + stripped)
395+
};
396+
397397
let is_struct_pat_shorthand_field =
398398
self.is_hir_id_from_struct_pattern_shorthand_field(expr.hir_id, sp);
399399

@@ -409,7 +409,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
409409
(&ty::Str, &ty::Array(arr, _) | &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
410410
if let hir::ExprKind::Lit(_) = expr.kind {
411411
if let Ok(src) = sm.span_to_snippet(sp) {
412-
if let Some(src) = self.replace_prefix(&src, "b\"", "\"") {
412+
if let Some(src) = replace_prefix(&src, "b\"", "\"") {
413413
return Some((
414414
sp,
415415
"consider removing the leading `b`",
@@ -423,7 +423,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
423423
(&ty::Array(arr, _) | &ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
424424
if let hir::ExprKind::Lit(_) = expr.kind {
425425
if let Ok(src) = sm.span_to_snippet(sp) {
426-
if let Some(src) = self.replace_prefix(&src, "\"", "b\"") {
426+
if let Some(src) = replace_prefix(&src, "\"", "b\"") {
427427
return Some((
428428
sp,
429429
"consider adding a leading `b`",
@@ -583,23 +583,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
583583
hir::Mutability::Mut => {
584584
let new_prefix = "&mut ".to_owned() + derefs;
585585
match mutbl_a {
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)),
586+
hir::Mutability::Mut => {
587+
replace_prefix(&src, "&mut ", &new_prefix)
588+
.map(|s| (s, Applicability::MachineApplicable))
589+
}
590+
hir::Mutability::Not => {
591+
replace_prefix(&src, "&", &new_prefix)
592+
.map(|s| (s, Applicability::Unspecified))
593+
}
592594
}
593595
}
594596
hir::Mutability::Not => {
595597
let new_prefix = "&".to_owned() + derefs;
596598
match mutbl_a {
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)),
599+
hir::Mutability::Mut => {
600+
replace_prefix(&src, "&mut ", &new_prefix)
601+
.map(|s| (s, Applicability::MachineApplicable))
602+
}
603+
hir::Mutability::Not => {
604+
replace_prefix(&src, "&", &new_prefix)
605+
.map(|s| (s, Applicability::MachineApplicable))
606+
}
603607
}
604608
}
605609
} {

0 commit comments

Comments
 (0)