Skip to content

Commit 02bea16

Browse files
committed
Rely in resolve and not on path name for &str -> String suggestion
1 parent bb9d720 commit 02bea16

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+54-49
Original file line numberDiff line numberDiff line change
@@ -3105,60 +3105,65 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
31053105
owned_sugg = true;
31063106
}
31073107
if let Some(ty) = lt_finder.found {
3108-
if let TyKind::Path(None, path @ Path { segments, .. }) = &ty.kind
3109-
&& segments.len() == 1
3110-
{
3111-
if segments[0].ident.name == sym::str {
3112-
// Don't suggest `-> str`, suggest `-> String`.
3113-
sugg = vec![
3114-
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
3115-
];
3116-
} else {
3117-
// Check if the path being borrowed is likely to be owned.
3118-
let path: Vec<_> = Segment::from_path(path);
3119-
match self.resolve_path(&path, Some(TypeNS), None) {
3120-
PathResult::Module(
3121-
ModuleOrUniformRoot::Module(module),
3122-
) => {
3123-
match module.res() {
3124-
Some(Res::PrimTy(..)) => {}
3125-
Some(Res::Def(
3126-
DefKind::Struct
3127-
| DefKind::Union
3128-
| DefKind::Enum
3129-
| DefKind::ForeignTy
3130-
| DefKind::AssocTy
3131-
| DefKind::OpaqueTy
3132-
| DefKind::TyParam,
3133-
_,
3134-
)) => {}
3135-
_ => { // Do not suggest in all other cases.
3136-
owned_sugg = false;
3137-
}
3108+
if let TyKind::Path(None, path) = &ty.kind {
3109+
// Check if the path being borrowed is likely to be owned.
3110+
let path: Vec<_> = Segment::from_path(path);
3111+
match self.resolve_path(&path, Some(TypeNS), None) {
3112+
PathResult::Module(
3113+
ModuleOrUniformRoot::Module(module),
3114+
) => {
3115+
match module.res() {
3116+
Some(Res::PrimTy(PrimTy::Str)) => {
3117+
// Don't suggest `-> str`, suggest `-> String`.
3118+
sugg = vec![(
3119+
lt.span.with_hi(ty.span.hi()),
3120+
"String".to_string(),
3121+
)];
31383122
}
3139-
}
3140-
PathResult::NonModule(res) => {
3141-
match res.base_res() {
3142-
Res::PrimTy(..) => {}
3143-
Res::Def(
3144-
DefKind::Struct
3145-
| DefKind::Union
3146-
| DefKind::Enum
3147-
| DefKind::ForeignTy
3148-
| DefKind::AssocTy
3149-
| DefKind::OpaqueTy
3150-
| DefKind::TyParam,
3151-
_,
3152-
) => {}
3153-
_ => { // Do not suggest in all other cases.
3154-
owned_sugg = false;
3155-
}
3123+
Some(Res::PrimTy(..)) => {}
3124+
Some(Res::Def(
3125+
DefKind::Struct
3126+
| DefKind::Union
3127+
| DefKind::Enum
3128+
| DefKind::ForeignTy
3129+
| DefKind::AssocTy
3130+
| DefKind::OpaqueTy
3131+
| DefKind::TyParam,
3132+
_,
3133+
)) => {}
3134+
_ => { // Do not suggest in all other cases.
3135+
owned_sugg = false;
31563136
}
31573137
}
3158-
_ => { // Do not suggest in all other cases.
3159-
owned_sugg = false;
3138+
}
3139+
PathResult::NonModule(res) => {
3140+
match res.base_res() {
3141+
Res::PrimTy(PrimTy::Str) => {
3142+
// Don't suggest `-> str`, suggest `-> String`.
3143+
sugg = vec![(
3144+
lt.span.with_hi(ty.span.hi()),
3145+
"String".to_string(),
3146+
)];
3147+
}
3148+
Res::PrimTy(..) => {}
3149+
Res::Def(
3150+
DefKind::Struct
3151+
| DefKind::Union
3152+
| DefKind::Enum
3153+
| DefKind::ForeignTy
3154+
| DefKind::AssocTy
3155+
| DefKind::OpaqueTy
3156+
| DefKind::TyParam,
3157+
_,
3158+
) => {}
3159+
_ => { // Do not suggest in all other cases.
3160+
owned_sugg = false;
3161+
}
31603162
}
31613163
}
3164+
_ => { // Do not suggest in all other cases.
3165+
owned_sugg = false;
3166+
}
31623167
}
31633168
}
31643169
if let TyKind::Slice(inner_ty) = &ty.kind {

0 commit comments

Comments
 (0)