aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists/src')
-rw-r--r--crates/ide_assists/src/handlers/add_lifetime_to_type.rs70
1 files changed, 36 insertions, 34 deletions
diff --git a/crates/ide_assists/src/handlers/add_lifetime_to_type.rs b/crates/ide_assists/src/handlers/add_lifetime_to_type.rs
index 844928754..7030d0f97 100644
--- a/crates/ide_assists/src/handlers/add_lifetime_to_type.rs
+++ b/crates/ide_assists/src/handlers/add_lifetime_to_type.rs
@@ -1,5 +1,4 @@
1use ast::FieldList; 1use syntax::ast::{self, AstNode, GenericParamsOwner, NameOwner};
2use syntax::ast::{self, AstNode, GenericParamsOwner, NameOwner, RefType, Type};
3 2
4use crate::{AssistContext, AssistId, AssistKind, Assists}; 3use crate::{AssistContext, AssistId, AssistKind, Assists};
5 4
@@ -65,8 +64,8 @@ pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext) -> Op
65 ) 64 )
66} 65}
67 66
68fn fetch_borrowed_types(node: &ast::Adt) -> Option<Vec<RefType>> { 67fn fetch_borrowed_types(node: &ast::Adt) -> Option<Vec<ast::RefType>> {
69 let ref_types: Vec<RefType> = match node { 68 let ref_types: Vec<ast::RefType> = match node {
70 ast::Adt::Enum(enum_) => { 69 ast::Adt::Enum(enum_) => {
71 let variant_list = enum_.variant_list()?; 70 let variant_list = enum_.variant_list()?;
72 variant_list 71 variant_list
@@ -88,7 +87,7 @@ fn fetch_borrowed_types(node: &ast::Adt) -> Option<Vec<RefType>> {
88 record_field_list 87 record_field_list
89 .fields() 88 .fields()
90 .filter_map(|r_field| { 89 .filter_map(|r_field| {
91 if let Type::RefType(ref_type) = r_field.ty()? { 90 if let ast::Type::RefType(ref_type) = r_field.ty()? {
92 if ref_type.lifetime().is_none() { 91 if ref_type.lifetime().is_none() {
93 return Some(ref_type); 92 return Some(ref_type);
94 } 93 }
@@ -107,12 +106,12 @@ fn fetch_borrowed_types(node: &ast::Adt) -> Option<Vec<RefType>> {
107 } 106 }
108} 107}
109 108
110fn find_ref_types_from_field_list(field_list: &FieldList) -> Option<Vec<RefType>> { 109fn find_ref_types_from_field_list(field_list: &ast::FieldList) -> Option<Vec<ast::RefType>> {
111 let ref_types: Vec<RefType> = match field_list { 110 let ref_types: Vec<ast::RefType> = match field_list {
112 ast::FieldList::RecordFieldList(record_list) => record_list 111 ast::FieldList::RecordFieldList(record_list) => record_list
113 .fields() 112 .fields()
114 .filter_map(|f| { 113 .filter_map(|f| {
115 if let Type::RefType(ref_type) = f.ty()? { 114 if let ast::Type::RefType(ref_type) = f.ty()? {
116 if ref_type.lifetime().is_none() { 115 if ref_type.lifetime().is_none() {
117 return Some(ref_type); 116 return Some(ref_type);
118 } 117 }
@@ -124,7 +123,7 @@ fn find_ref_types_from_field_list(field_list: &FieldList) -> Option<Vec<RefType>
124 ast::FieldList::TupleFieldList(tuple_field_list) => tuple_field_list 123 ast::FieldList::TupleFieldList(tuple_field_list) => tuple_field_list
125 .fields() 124 .fields()
126 .filter_map(|f| { 125 .filter_map(|f| {
127 if let Type::RefType(ref_type) = f.ty()? { 126 if let ast::Type::RefType(ref_type) = f.ty()? {
128 if ref_type.lifetime().is_none() { 127 if ref_type.lifetime().is_none() {
129 return Some(ref_type); 128 return Some(ref_type);
130 } 129 }
@@ -152,76 +151,79 @@ mod tests {
152 fn add_lifetime_to_struct() { 151 fn add_lifetime_to_struct() {
153 check_assist( 152 check_assist(
154 add_lifetime_to_type, 153 add_lifetime_to_type,
155 "struct Foo { a: &$0i32 }", 154 r#"struct Foo { a: &$0i32 }"#,
156 "struct Foo<'a> { a: &'a i32 }", 155 r#"struct Foo<'a> { a: &'a i32 }"#,
157 ); 156 );
158 157
159 check_assist( 158 check_assist(
160 add_lifetime_to_type, 159 add_lifetime_to_type,
161 "struct Foo { a: &$0i32, b: &usize }", 160 r#"struct Foo { a: &$0i32, b: &usize }"#,
162 "struct Foo<'a> { a: &'a i32, b: &'a usize }", 161 r#"struct Foo<'a> { a: &'a i32, b: &'a usize }"#,
163 ); 162 );
164 163
165 check_assist( 164 check_assist(
166 add_lifetime_to_type, 165 add_lifetime_to_type,
167 "struct Foo { a: &$0i32, b: usize }", 166 r#"struct Foo { a: &$0i32, b: usize }"#,
168 "struct Foo<'a> { a: &'a i32, b: usize }", 167 r#"struct Foo<'a> { a: &'a i32, b: usize }"#,
169 ); 168 );
170 169
171 check_assist( 170 check_assist(
172 add_lifetime_to_type, 171 add_lifetime_to_type,
173 "struct Foo<T> { a: &$0T, b: usize }", 172 r#"struct Foo<T> { a: &$0T, b: usize }"#,
174 "struct Foo<'a, T> { a: &'a T, b: usize }", 173 r#"struct Foo<'a, T> { a: &'a T, b: usize }"#,
175 ); 174 );
176 175
177 check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &$0'a i32 }"); 176 check_assist_not_applicable(add_lifetime_to_type, r#"struct Foo<'a> { a: &$0'a i32 }"#);
178 check_assist_not_applicable(add_lifetime_to_type, "struct Foo { a: &'a$0 i32 }"); 177 check_assist_not_applicable(add_lifetime_to_type, r#"struct Foo { a: &'a$0 i32 }"#);
179 } 178 }
180 179
181 #[test] 180 #[test]
182 fn add_lifetime_to_enum() { 181 fn add_lifetime_to_enum() {
183 check_assist( 182 check_assist(
184 add_lifetime_to_type, 183 add_lifetime_to_type,
185 "enum Foo { Bar { a: i32 }, Other, Tuple(u32, &$0u32)}", 184 r#"enum Foo { Bar { a: i32 }, Other, Tuple(u32, &$0u32)}"#,
186 "enum Foo<'a> { Bar { a: i32 }, Other, Tuple(u32, &'a u32)}", 185 r#"enum Foo<'a> { Bar { a: i32 }, Other, Tuple(u32, &'a u32)}"#,
187 ); 186 );
188 187
189 check_assist( 188 check_assist(
190 add_lifetime_to_type, 189 add_lifetime_to_type,
191 "enum Foo { Bar { a: &$0i32 }}", 190 r#"enum Foo { Bar { a: &$0i32 }}"#,
192 "enum Foo<'a> { Bar { a: &'a i32 }}", 191 r#"enum Foo<'a> { Bar { a: &'a i32 }}"#,
193 ); 192 );
194 193
195 check_assist( 194 check_assist(
196 add_lifetime_to_type, 195 add_lifetime_to_type,
197 "enum Foo<T> { Bar { a: &$0i32, b: &T }}", 196 r#"enum Foo<T> { Bar { a: &$0i32, b: &T }}"#,
198 "enum Foo<'a, T> { Bar { a: &'a i32, b: &'a T }}", 197 r#"enum Foo<'a, T> { Bar { a: &'a i32, b: &'a T }}"#,
199 ); 198 );
200 199
201 check_assist_not_applicable(add_lifetime_to_type, "enum Foo<'a> { Bar { a: &$0'a i32 }}"); 200 check_assist_not_applicable(
202 check_assist_not_applicable(add_lifetime_to_type, "enum Foo { Bar, $0Misc }"); 201 add_lifetime_to_type,
202 r#"enum Foo<'a> { Bar { a: &$0'a i32 }}"#,
203 );
204 check_assist_not_applicable(add_lifetime_to_type, r#"enum Foo { Bar, $0Misc }"#);
203 } 205 }
204 206
205 #[test] 207 #[test]
206 fn add_lifetime_to_union() { 208 fn add_lifetime_to_union() {
207 check_assist( 209 check_assist(
208 add_lifetime_to_type, 210 add_lifetime_to_type,
209 "union Foo { a: &$0i32 }", 211 r#"union Foo { a: &$0i32 }"#,
210 "union Foo<'a> { a: &'a i32 }", 212 r#"union Foo<'a> { a: &'a i32 }"#,
211 ); 213 );
212 214
213 check_assist( 215 check_assist(
214 add_lifetime_to_type, 216 add_lifetime_to_type,
215 "union Foo { a: &$0i32, b: &usize }", 217 r#"union Foo { a: &$0i32, b: &usize }"#,
216 "union Foo<'a> { a: &'a i32, b: &'a usize }", 218 r#"union Foo<'a> { a: &'a i32, b: &'a usize }"#,
217 ); 219 );
218 220
219 check_assist( 221 check_assist(
220 add_lifetime_to_type, 222 add_lifetime_to_type,
221 "union Foo<T> { a: &$0T, b: usize }", 223 r#"union Foo<T> { a: &$0T, b: usize }"#,
222 "union Foo<'a, T> { a: &'a T, b: usize }", 224 r#"union Foo<'a, T> { a: &'a T, b: usize }"#,
223 ); 225 );
224 226
225 check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &'a $0i32 }"); 227 check_assist_not_applicable(add_lifetime_to_type, r#"struct Foo<'a> { a: &'a $0i32 }"#);
226 } 228 }
227} 229}