@@ -2,6 +2,7 @@ use stdx::{format_to, to_lower_snake_case};
22use syntax:: ast:: { self , AstNode , NameOwner , VisibilityOwner } ;
33
44use crate :: {
5+ utils:: useless_type_special_case,
56 utils:: { find_impl_block_end, find_struct_impl, generate_impl_text} ,
67 AssistContext , AssistId , AssistKind , Assists , GroupLabel ,
78} ;
@@ -99,7 +100,7 @@ pub(crate) fn generate_getter_impl(
99100 let ( ty, body) = if mutable {
100101 ( format ! ( "&mut {}" , field_ty) , format ! ( "&mut self.{}" , field_name) )
101102 } else {
102- useless_type_special_case ( & field_name. to_string ( ) , & field_ty)
103+ useless_type_special_case ( & field_name. to_string ( ) , & field_ty. to_string ( ) )
103104 . unwrap_or_else ( || ( format ! ( "&{}" , field_ty) , format ! ( "&self.{}" , field_name) ) )
104105 } ;
105106
@@ -136,29 +137,6 @@ pub(crate) fn generate_getter_impl(
136137 )
137138}
138139
139- fn useless_type_special_case ( field_name : & str , field_ty : & ast:: Type ) -> Option < ( String , String ) > {
140- if field_ty. to_string ( ) == "String" {
141- cov_mark:: hit!( useless_type_special_case) ;
142- return Some ( ( "&str" . to_string ( ) , format ! ( "self.{}.as_str()" , field_name) ) ) ;
143- }
144- if let Some ( arg) = ty_ctor ( field_ty, "Vec" ) {
145- return Some ( ( format ! ( "&[{}]" , arg) , format ! ( "self.{}.as_slice()" , field_name) ) ) ;
146- }
147- if let Some ( arg) = ty_ctor ( field_ty, "Box" ) {
148- return Some ( ( format ! ( "&{}" , arg) , format ! ( "self.{}.as_ref()" , field_name) ) ) ;
149- }
150- if let Some ( arg) = ty_ctor ( field_ty, "Option" ) {
151- return Some ( ( format ! ( "Option<&{}>" , arg) , format ! ( "self.{}.as_ref()" , field_name) ) ) ;
152- }
153- None
154- }
155-
156- // FIXME: This should rely on semantic info.
157- fn ty_ctor ( ty : & ast:: Type , ctor : & str ) -> Option < String > {
158- let res = ty. to_string ( ) . strip_prefix ( ctor) ?. strip_prefix ( '<' ) ?. strip_suffix ( '>' ) ?. to_string ( ) ;
159- Some ( res)
160- }
161-
162140#[ cfg( test) ]
163141mod tests {
164142 use crate :: tests:: { check_assist, check_assist_not_applicable} ;
0 commit comments