@@ -60,6 +60,21 @@ const USELESS_METHODS: &[&str] = &[
60
60
"into_future" ,
61
61
] ;
62
62
63
+ /// Suggest a name for given type.
64
+ ///
65
+ /// The function will strip references first, and suggest name from the inner type.
66
+ ///
67
+ /// - If `ty` is an ADT, it will suggest the name of the ADT.
68
+ /// + If `ty` is wrapped in `Box`, `Option` or `Result`, it will suggest the name from the inner type.
69
+ /// - If `ty` is a trait, it will suggest the name of the trait.
70
+ /// - If `ty` is an `impl Trait`, it will suggest the name of the first trait.
71
+ ///
72
+ /// If the suggested name conflicts with reserved keywords, it will return `None`.
73
+ pub fn for_type ( ty : & hir:: Type , db : & RootDatabase , edition : Edition ) -> Option < String > {
74
+ let ty = ty. strip_references ( ) ;
75
+ name_of_type ( & ty, db, edition)
76
+ }
77
+
63
78
/// Suggest a unique name for generic parameter.
64
79
///
65
80
/// `existing_params` is used to check if the name conflicts with existing
@@ -269,10 +284,9 @@ fn var_name_from_pat(pat: &ast::Pat) -> Option<ast::Name> {
269
284
270
285
fn from_type ( expr : & ast:: Expr , sema : & Semantics < ' _ , RootDatabase > ) -> Option < String > {
271
286
let ty = sema. type_of_expr ( expr) ?. adjusted ( ) ;
272
- let ty = ty. remove_ref ( ) . unwrap_or ( ty) ;
273
287
let edition = sema. scope ( expr. syntax ( ) ) ?. krate ( ) . edition ( sema. db ) ;
274
288
275
- name_of_type ( & ty, sema. db , edition)
289
+ for_type ( & ty, sema. db , edition)
276
290
}
277
291
278
292
fn name_of_type ( ty : & hir:: Type , db : & RootDatabase , edition : Edition ) -> Option < String > {
0 commit comments