Skip to content

Commit 982c4a9

Browse files
authored
Rollup merge of #76835 - matthiaskrgr:replace_prefix, r=lcnr
make replace_prefix only take &str as arguments included the clippy::manual strip commit to not run into merge conflicts later. r? @lcnr
2 parents ae4b677 + 026922a commit 982c4a9

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

compiler/rustc_typeck/src/check/demand.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
362362
false
363363
}
364364

365-
fn replace_prefix<A, B, C>(&self, s: A, old: B, new: C) -> Option<String>
366-
where
367-
A: AsRef<str>,
368-
B: AsRef<str>,
369-
C: AsRef<str>,
370-
{
371-
let s = s.as_ref();
372-
let old = old.as_ref();
365+
fn replace_prefix(&self, s: &str, old: &str, new: &str) -> Option<String> {
373366
if let Some(stripped) = s.strip_prefix(old) {
374-
Some(new.as_ref().to_owned() + stripped)
367+
Some(new.to_string() + stripped)
375368
} else {
376369
None
377370
}
@@ -422,7 +415,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
422415
(&ty::Str, &ty::Array(arr, _) | &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
423416
if let hir::ExprKind::Lit(_) = expr.kind {
424417
if let Ok(src) = sm.span_to_snippet(sp) {
425-
if let Some(src) = self.replace_prefix(src, "b\"", "\"") {
418+
if let Some(src) = self.replace_prefix(&src, "b\"", "\"") {
426419
return Some((
427420
sp,
428421
"consider removing the leading `b`",
@@ -436,7 +429,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
436429
(&ty::Array(arr, _) | &ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
437430
if let hir::ExprKind::Lit(_) = expr.kind {
438431
if let Ok(src) = sm.span_to_snippet(sp) {
439-
if let Some(src) = self.replace_prefix(src, "\"", "b\"") {
432+
if let Some(src) = self.replace_prefix(&src, "\"", "b\"") {
440433
return Some((
441434
sp,
442435
"consider adding a leading `b`",
@@ -561,7 +554,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
561554
// we may want to suggest removing a `&`.
562555
if sm.is_imported(expr.span) {
563556
if let Ok(src) = sm.span_to_snippet(sp) {
564-
if let Some(src) = self.replace_prefix(src, "&", "") {
557+
if let Some(src) = self.replace_prefix(&src, "&", "") {
565558
return Some((
566559
sp,
567560
"consider removing the borrow",
@@ -598,7 +591,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
598591
match mutbl_a {
599592
hir::Mutability::Mut => {
600593
if let Some(s) =
601-
self.replace_prefix(src, "&mut ", new_prefix)
594+
self.replace_prefix(&src, "&mut ", &new_prefix)
602595
{
603596
Some((s, Applicability::MachineApplicable))
604597
} else {
@@ -607,7 +600,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
607600
}
608601
hir::Mutability::Not => {
609602
if let Some(s) =
610-
self.replace_prefix(src, "&", new_prefix)
603+
self.replace_prefix(&src, "&", &new_prefix)
611604
{
612605
Some((s, Applicability::Unspecified))
613606
} else {
@@ -621,7 +614,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
621614
match mutbl_a {
622615
hir::Mutability::Mut => {
623616
if let Some(s) =
624-
self.replace_prefix(src, "&mut ", new_prefix)
617+
self.replace_prefix(&src, "&mut ", &new_prefix)
625618
{
626619
Some((s, Applicability::MachineApplicable))
627620
} else {
@@ -630,7 +623,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
630623
}
631624
hir::Mutability::Not => {
632625
if let Some(s) =
633-
self.replace_prefix(src, "&", new_prefix)
626+
self.replace_prefix(&src, "&", &new_prefix)
634627
{
635628
Some((s, Applicability::MachineApplicable))
636629
} else {

0 commit comments

Comments
 (0)