Skip to content

Commit bb9d720

Browse files
committed
Do not consider traits as ownable in suggestion
1 parent 85f26ad commit bb9d720

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+55-7
Original file line numberDiff line numberDiff line change
@@ -3105,15 +3105,63 @@ 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 { segments, .. }) = &ty.kind
3108+
if let TyKind::Path(None, path @ Path { segments, .. }) = &ty.kind
31093109
&& segments.len() == 1
3110-
&& segments[0].ident.name == sym::str
31113110
{
3112-
// Don't suggest `-> str`, suggest `-> String`.
3113-
sugg = vec![
3114-
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
3115-
];
3116-
} else if let TyKind::Slice(inner_ty) = &ty.kind {
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+
}
3138+
}
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+
}
3156+
}
3157+
}
3158+
_ => { // Do not suggest in all other cases.
3159+
owned_sugg = false;
3160+
}
3161+
}
3162+
}
3163+
}
3164+
if let TyKind::Slice(inner_ty) = &ty.kind {
31173165
// Don't suggest `-> [T]`, suggest `-> Vec<T>`.
31183166
sugg = vec![
31193167
(lt.span.with_hi(inner_ty.span.lo()), "Vec<".to_string()),

tests/ui/suggestions/missing-lifetime-specifier.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ help: consider using the `'static` lifetime, but this is uncommon unless you're
117117
|
118118
LL | static f: RefCell<HashMap<i32, Vec<Vec<&'static Tar<'static, i32>>>>> = RefCell::new(HashMap::new());
119119
| +++++++
120-
help: instead, you are more likely to want to return an owned value
121-
|
122-
LL - static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, i32>>>>> = RefCell::new(HashMap::new());
123-
LL + static f: RefCell<HashMap<i32, Vec<Vec<Tar<'static, i32>>>>> = RefCell::new(HashMap::new());
124-
|
125120

126121
error[E0106]: missing lifetime specifier
127122
--> $DIR/missing-lifetime-specifier.rs:47:44

0 commit comments

Comments
 (0)