Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit872d7b8

Browse files
committed
Add suggestion forOption<&Vec<T>> -> Option<&[T]
1 parentd9170dc commit872d7b8

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‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -466,21 +466,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
466466
borrow_removal_span,
467467
});
468468
returntrue;
469-
}elseifletSome((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-
returntrue;
483-
}elseiflet ty::Adt(adt, _) = found_ty_inner.peel_refs().kind()
469+
}elseiflet 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
returntrue;
486+
}else{
487+
if !error_tys_equate_as_ref{
488+
returnfalse;
489+
}
490+
letmut steps =self.autoderef(expr.span, found_ty_inner).silence_errors();
491+
ifletSome((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+
returntrue;
503+
}
504+
for(deref_ty, n_step)in steps{
505+
ifself.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+
returntrue;
516+
}
517+
}
499518
}
500519
}
501520

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

Lines changed: 4 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp