Rely in resolve and not on path name for &str -> String suggestion

This commit is contained in:
Esteban Küber 2023-11-15 20:22:54 +00:00
parent bb9d720a16
commit 02bea16c08

View File

@ -3105,59 +3105,64 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
owned_sugg = true; owned_sugg = true;
} }
if let Some(ty) = lt_finder.found { if let Some(ty) = lt_finder.found {
if let TyKind::Path(None, path @ Path { segments, .. }) = &ty.kind if let TyKind::Path(None, path) = &ty.kind {
&& segments.len() == 1 // Check if the path being borrowed is likely to be owned.
{ let path: Vec<_> = Segment::from_path(path);
if segments[0].ident.name == sym::str { match self.resolve_path(&path, Some(TypeNS), None) {
// Don't suggest `-> str`, suggest `-> String`. PathResult::Module(
sugg = vec![ ModuleOrUniformRoot::Module(module),
(lt.span.with_hi(ty.span.hi()), "String".to_string()), ) => {
]; match module.res() {
} else { Some(Res::PrimTy(PrimTy::Str)) => {
// Check if the path being borrowed is likely to be owned. // Don't suggest `-> str`, suggest `-> String`.
let path: Vec<_> = Segment::from_path(path); sugg = vec![(
match self.resolve_path(&path, Some(TypeNS), None) { lt.span.with_hi(ty.span.hi()),
PathResult::Module( "String".to_string(),
ModuleOrUniformRoot::Module(module), )];
) => { }
match module.res() { Some(Res::PrimTy(..)) => {}
Some(Res::PrimTy(..)) => {} Some(Res::Def(
Some(Res::Def( DefKind::Struct
DefKind::Struct | DefKind::Union
| DefKind::Union | DefKind::Enum
| DefKind::Enum | DefKind::ForeignTy
| DefKind::ForeignTy | DefKind::AssocTy
| DefKind::AssocTy | DefKind::OpaqueTy
| DefKind::OpaqueTy | DefKind::TyParam,
| DefKind::TyParam, _,
_, )) => {}
)) => {} _ => { // Do not suggest in all other cases.
_ => { // Do not suggest in all other cases. owned_sugg = false;
owned_sugg = false;
}
} }
} }
PathResult::NonModule(res) => { }
match res.base_res() { PathResult::NonModule(res) => {
Res::PrimTy(..) => {} match res.base_res() {
Res::Def( Res::PrimTy(PrimTy::Str) => {
DefKind::Struct // Don't suggest `-> str`, suggest `-> String`.
| DefKind::Union sugg = vec![(
| DefKind::Enum lt.span.with_hi(ty.span.hi()),
| DefKind::ForeignTy "String".to_string(),
| DefKind::AssocTy )];
| DefKind::OpaqueTy }
| DefKind::TyParam, Res::PrimTy(..) => {}
_, Res::Def(
) => {} DefKind::Struct
_ => { // Do not suggest in all other cases. | DefKind::Union
owned_sugg = false; | DefKind::Enum
} | DefKind::ForeignTy
| DefKind::AssocTy
| DefKind::OpaqueTy
| DefKind::TyParam,
_,
) => {}
_ => { // Do not suggest in all other cases.
owned_sugg = false;
} }
} }
_ => { // Do not suggest in all other cases. }
owned_sugg = false; _ => { // Do not suggest in all other cases.
} owned_sugg = false;
} }
} }
} }