diff options
author | hi-rustin <[email protected]> | 2021-03-23 15:23:01 +0000 |
---|---|---|
committer | hi-rustin <[email protected]> | 2021-03-24 03:52:44 +0000 |
commit | e992acf0780ef591c9cb95b385bb693d397bc5ea (patch) | |
tree | f5712030baf9e1a72d4d198c18778db799226606 /crates/ide_assists/src/handlers | |
parent | 258afb8fb8331e43a75e4f19df255d85d2430be7 (diff) |
correct `convert to guard return` let_stmt
fix
fix
add check
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/early_return.rs | 77 |
1 files changed, 70 insertions, 7 deletions
diff --git a/crates/ide_assists/src/handlers/early_return.rs b/crates/ide_assists/src/handlers/early_return.rs index 9e0918477..2148df7c8 100644 --- a/crates/ide_assists/src/handlers/early_return.rs +++ b/crates/ide_assists/src/handlers/early_return.rs | |||
@@ -56,7 +56,11 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) | |||
56 | match path.qualifier() { | 56 | match path.qualifier() { |
57 | None => { | 57 | None => { |
58 | let bound_ident = pat.fields().next().unwrap(); | 58 | let bound_ident = pat.fields().next().unwrap(); |
59 | Some((path, bound_ident)) | 59 | if ast::IdentPat::can_cast(bound_ident.syntax().kind()) { |
60 | Some((path, bound_ident)) | ||
61 | } else { | ||
62 | return None; | ||
63 | } | ||
60 | } | 64 | } |
61 | Some(_) => return None, | 65 | Some(_) => return None, |
62 | } | 66 | } |
@@ -143,10 +147,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) | |||
143 | make::expr_match(cond_expr, make::match_arm_list(vec![happy_arm, sad_arm])) | 147 | make::expr_match(cond_expr, make::match_arm_list(vec![happy_arm, sad_arm])) |
144 | }; | 148 | }; |
145 | 149 | ||
146 | let let_stmt = make::let_stmt( | 150 | let let_stmt = make::let_stmt(bound_ident, Some(match_expr)); |
147 | make::ident_pat(make::name(&bound_ident.syntax().to_string())).into(), | ||
148 | Some(match_expr), | ||
149 | ); | ||
150 | let let_stmt = let_stmt.indent(if_indent_level); | 151 | let let_stmt = let_stmt.indent(if_indent_level); |
151 | replace(let_stmt.syntax(), &then_block, &parent_block, &if_expr) | 152 | replace(let_stmt.syntax(), &then_block, &parent_block, &if_expr) |
152 | } | 153 | } |
@@ -284,7 +285,7 @@ mod tests { | |||
284 | r#" | 285 | r#" |
285 | fn main(n: Option<String>) { | 286 | fn main(n: Option<String>) { |
286 | bar(); | 287 | bar(); |
287 | if$0 let Ok(n) = n { | 288 | if$0 let Some(n) = n { |
288 | foo(n); | 289 | foo(n); |
289 | 290 | ||
290 | //comment | 291 | //comment |
@@ -296,7 +297,69 @@ mod tests { | |||
296 | fn main(n: Option<String>) { | 297 | fn main(n: Option<String>) { |
297 | bar(); | 298 | bar(); |
298 | let n = match n { | 299 | let n = match n { |
299 | Ok(it) => it, | 300 | Some(it) => it, |
301 | _ => return, | ||
302 | }; | ||
303 | foo(n); | ||
304 | |||
305 | //comment | ||
306 | bar(); | ||
307 | } | ||
308 | "#, | ||
309 | ); | ||
310 | } | ||
311 | |||
312 | #[test] | ||
313 | fn convert_let_mut_ok_inside_fn() { | ||
314 | check_assist( | ||
315 | convert_to_guarded_return, | ||
316 | r#" | ||
317 | fn main(n: Option<String>) { | ||
318 | bar(); | ||
319 | if$0 let Some(mut n) = n { | ||
320 | foo(n); | ||
321 | |||
322 | //comment | ||
323 | bar(); | ||
324 | } | ||
325 | } | ||
326 | "#, | ||
327 | r#" | ||
328 | fn main(n: Option<String>) { | ||
329 | bar(); | ||
330 | let mut n = match n { | ||
331 | Some(it) => it, | ||
332 | _ => return, | ||
333 | }; | ||
334 | foo(n); | ||
335 | |||
336 | //comment | ||
337 | bar(); | ||
338 | } | ||
339 | "#, | ||
340 | ); | ||
341 | } | ||
342 | |||
343 | #[test] | ||
344 | fn convert_let_ref_ok_inside_fn() { | ||
345 | check_assist( | ||
346 | convert_to_guarded_return, | ||
347 | r#" | ||
348 | fn main(n: Option<&str>) { | ||
349 | bar(); | ||
350 | if$0 let Some(ref n) = n { | ||
351 | foo(n); | ||
352 | |||
353 | //comment | ||
354 | bar(); | ||
355 | } | ||
356 | } | ||
357 | "#, | ||
358 | r#" | ||
359 | fn main(n: Option<&str>) { | ||
360 | bar(); | ||
361 | let ref n = match n { | ||
362 | Some(it) => it, | ||
300 | _ => return, | 363 | _ => return, |
301 | }; | 364 | }; |
302 | foo(n); | 365 | foo(n); |