Skip to content

Commit d422ce2

Browse files
committed
Do not consider traits as ownable in suggestion
1 parent f9357f6 commit d422ce2

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
@@ -2906,15 +2906,63 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
29062906
owned_sugg = true;
29072907
}
29082908
if let Some(ty) = lt_finder.found {
2909-
if let TyKind::Path(None, Path { segments, .. }) = &ty.kind
2909+
if let TyKind::Path(None, path @ Path { segments, .. }) = &ty.kind
29102910
&& segments.len() == 1
2911-
&& segments[0].ident.name == sym::str
29122911
{
2913-
// Don't suggest `-> str`, suggest `-> String`.
2914-
sugg = vec![
2915-
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
2916-
];
2917-
} else if let TyKind::Slice(inner_ty) = &ty.kind {
2912+
if segments[0].ident.name == sym::str {
2913+
// Don't suggest `-> str`, suggest `-> String`.
2914+
sugg = vec![
2915+
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
2916+
];
2917+
} else {
2918+
// Check if the path being borrowed is likely to be owned.
2919+
let path: Vec<_> = Segment::from_path(path);
2920+
match self.resolve_path(&path, Some(TypeNS), None) {
2921+
PathResult::Module(
2922+
ModuleOrUniformRoot::Module(module),
2923+
) => {
2924+
match module.res() {
2925+
Some(Res::PrimTy(..)) => {}
2926+
Some(Res::Def(
2927+
DefKind::Struct
2928+
| DefKind::Union
2929+
| DefKind::Enum
2930+
| DefKind::ForeignTy
2931+
| DefKind::AssocTy
2932+
| DefKind::OpaqueTy
2933+
| DefKind::TyParam,
2934+
_,
2935+
)) => {}
2936+
_ => { // Do not suggest in all other cases.
2937+
owned_sugg = false;
2938+
}
2939+
}
2940+
}
2941+
PathResult::NonModule(res) => {
2942+
match res.base_res() {
2943+
Res::PrimTy(..) => {}
2944+
Res::Def(
2945+
DefKind::Struct
2946+
| DefKind::Union
2947+
| DefKind::Enum
2948+
| DefKind::ForeignTy
2949+
| DefKind::AssocTy
2950+
| DefKind::OpaqueTy
2951+
| DefKind::TyParam,
2952+
_,
2953+
) => {}
2954+
_ => { // Do not suggest in all other cases.
2955+
owned_sugg = false;
2956+
}
2957+
}
2958+
}
2959+
_ => { // Do not suggest in all other cases.
2960+
owned_sugg = false;
2961+
}
2962+
}
2963+
}
2964+
}
2965+
if let TyKind::Slice(inner_ty) = &ty.kind {
29182966
// Don't suggest `-> [T]`, suggest `-> Vec<T>`.
29192967
sugg = vec![
29202968
(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)