Skip to content

Commit 872d7b8

Browse files
committed
Add suggestion for Option<&Vec<T>> -> Option<&[T]
1 parent d9170dc commit 872d7b8

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+34-15
Original file line numberDiff line numberDiff line change
@@ -466,21 +466,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
466466
borrow_removal_span,
467467
});
468468
return true;
469-
} else if let Some((deref_ty, _)) =
470-
self.autoderef(expr.span, found_ty_inner).silence_errors().nth(1)
471-
&& self.can_eq(self.param_env, deref_ty, peeled)
472-
&& error_tys_equate_as_ref
473-
{
474-
let sugg = prefix_wrap(".as_deref()");
475-
err.subdiagnostic(errors::SuggestConvertViaMethod {
476-
span: expr.span.shrink_to_hi(),
477-
sugg,
478-
expected,
479-
found,
480-
borrow_removal_span,
481-
});
482-
return true;
483-
} else if let ty::Adt(adt, _) = found_ty_inner.peel_refs().kind()
469+
} else if let ty::Ref(_, peeled_found_ty, _) = found_ty_inner.kind()
470+
&& let ty::Adt(adt, _) = peeled_found_ty.peel_refs().kind()
484471
&& self.tcx.is_lang_item(adt.did(), LangItem::String)
485472
&& peeled.is_str()
486473
// `Result::map`, conversely, does not take ref of the error type.
@@ -496,6 +483,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
496483
Applicability::MachineApplicable,
497484
);
498485
return true;
486+
} else {
487+
if !error_tys_equate_as_ref {
488+
return false;
489+
}
490+
let mut steps = self.autoderef(expr.span, found_ty_inner).silence_errors();
491+
if let Some((deref_ty, _)) = steps.nth(1)
492+
&& self.can_eq(self.param_env, deref_ty, peeled)
493+
{
494+
let sugg = prefix_wrap(".as_deref()");
495+
err.subdiagnostic(errors::SuggestConvertViaMethod {
496+
span: expr.span.shrink_to_hi(),
497+
sugg,
498+
expected,
499+
found,
500+
borrow_removal_span,
501+
});
502+
return true;
503+
}
504+
for (deref_ty, n_step) in steps {
505+
if self.can_eq(self.param_env, deref_ty, peeled) {
506+
let explicit_deref = "*".repeat(n_step);
507+
let sugg = prefix_wrap(&format!(".map(|v| &{explicit_deref}v)"));
508+
err.subdiagnostic(errors::SuggestConvertViaMethod {
509+
span: expr.span.shrink_to_hi(),
510+
sugg,
511+
expected,
512+
found,
513+
borrow_removal_span,
514+
});
515+
return true;
516+
}
517+
}
499518
}
500519
}
501520

tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | arg
88
|
99
= note: expected enum `Option<&[i32]>`
1010
found enum `Option<&Vec<i32>>`
11+
help: try using `.map(|v| &**v)` to convert `Option<&Vec<i32>>` to `Option<&[i32]>`
12+
|
13+
LL | arg.map(|v| &**v)
14+
| ++++++++++++++
1115

1216
error: aborting due to 1 previous error
1317

0 commit comments

Comments
 (0)