diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-01-26 12:12:45 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-01-26 12:12:45 +0000 |
commit | 3a5cd6bbc60673aaaa62a94839062f870a62777a (patch) | |
tree | 8d055c5eaf79e281b139ffb1a1ab9f0ebb144cca /crates/assists/src/handlers | |
parent | 21660f1d979be43af579277ef487118d68533d41 (diff) | |
parent | 5728d7186e64966a20ece3dc479153219a47ba17 (diff) |
Merge #7406
7406: if_let_match: don't assume happy path r=matklad a=bugadani
Closes #7392
Co-authored-by: Dániel Buga <[email protected]>
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r-- | crates/assists/src/handlers/fill_match_arms.rs | 21 | ||||
-rw-r--r-- | crates/assists/src/handlers/replace_if_let_with_match.rs | 73 |
2 files changed, 72 insertions, 22 deletions
diff --git a/crates/assists/src/handlers/fill_match_arms.rs b/crates/assists/src/handlers/fill_match_arms.rs index da47187e4..7663d211d 100644 --- a/crates/assists/src/handlers/fill_match_arms.rs +++ b/crates/assists/src/handlers/fill_match_arms.rs | |||
@@ -8,7 +8,7 @@ use syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat}; | |||
8 | use test_utils::mark; | 8 | use test_utils::mark; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | utils::{render_snippet, Cursor}, | 11 | utils::{does_pat_match_variant, render_snippet, Cursor}, |
12 | AssistContext, AssistId, AssistKind, Assists, | 12 | AssistContext, AssistId, AssistKind, Assists, |
13 | }; | 13 | }; |
14 | 14 | ||
@@ -147,25 +147,6 @@ fn is_variant_missing(existing_arms: &mut Vec<MatchArm>, var: &Pat) -> bool { | |||
147 | }) | 147 | }) |
148 | } | 148 | } |
149 | 149 | ||
150 | fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool { | ||
151 | let first_node_text = |pat: &Pat| pat.syntax().first_child().map(|node| node.text()); | ||
152 | |||
153 | let pat_head = match pat { | ||
154 | Pat::IdentPat(bind_pat) => { | ||
155 | if let Some(p) = bind_pat.pat() { | ||
156 | first_node_text(&p) | ||
157 | } else { | ||
158 | return false; | ||
159 | } | ||
160 | } | ||
161 | pat => first_node_text(pat), | ||
162 | }; | ||
163 | |||
164 | let var_head = first_node_text(var); | ||
165 | |||
166 | pat_head == var_head | ||
167 | } | ||
168 | |||
169 | fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> { | 150 | fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> { |
170 | sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() { | 151 | sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() { |
171 | Some(Adt::Enum(e)) => Some(e), | 152 | Some(Adt::Enum(e)) => Some(e), |
diff --git a/crates/assists/src/handlers/replace_if_let_with_match.rs b/crates/assists/src/handlers/replace_if_let_with_match.rs index aee3397ab..aee880625 100644 --- a/crates/assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/assists/src/handlers/replace_if_let_with_match.rs | |||
@@ -10,7 +10,10 @@ use syntax::{ | |||
10 | AstNode, | 10 | AstNode, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists}; | 13 | use crate::{ |
14 | utils::{does_pat_match_variant, unwrap_trivial_block}, | ||
15 | AssistContext, AssistId, AssistKind, Assists, | ||
16 | }; | ||
14 | 17 | ||
15 | // Assist: replace_if_let_with_match | 18 | // Assist: replace_if_let_with_match |
16 | // | 19 | // |
@@ -66,7 +69,13 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
66 | .sema | 69 | .sema |
67 | .type_of_pat(&pat) | 70 | .type_of_pat(&pat) |
68 | .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)) | 71 | .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)) |
69 | .map(|it| it.sad_pattern()) | 72 | .map(|it| { |
73 | if does_pat_match_variant(&pat, &it.sad_pattern()) { | ||
74 | it.happy_pattern() | ||
75 | } else { | ||
76 | it.sad_pattern() | ||
77 | } | ||
78 | }) | ||
70 | .unwrap_or_else(|| make::wildcard_pat().into()); | 79 | .unwrap_or_else(|| make::wildcard_pat().into()); |
71 | let else_expr = unwrap_trivial_block(else_block); | 80 | let else_expr = unwrap_trivial_block(else_block); |
72 | make::match_arm(vec![pattern], else_expr) | 81 | make::match_arm(vec![pattern], else_expr) |
@@ -279,6 +288,36 @@ fn foo(x: Option<i32>) { | |||
279 | } | 288 | } |
280 | 289 | ||
281 | #[test] | 290 | #[test] |
291 | fn special_case_inverted_option() { | ||
292 | check_assist( | ||
293 | replace_if_let_with_match, | ||
294 | r#" | ||
295 | enum Option<T> { Some(T), None } | ||
296 | use Option::*; | ||
297 | |||
298 | fn foo(x: Option<i32>) { | ||
299 | $0if let None = x { | ||
300 | println!("none") | ||
301 | } else { | ||
302 | println!("some") | ||
303 | } | ||
304 | } | ||
305 | "#, | ||
306 | r#" | ||
307 | enum Option<T> { Some(T), None } | ||
308 | use Option::*; | ||
309 | |||
310 | fn foo(x: Option<i32>) { | ||
311 | match x { | ||
312 | None => println!("none"), | ||
313 | Some(_) => println!("some"), | ||
314 | } | ||
315 | } | ||
316 | "#, | ||
317 | ); | ||
318 | } | ||
319 | |||
320 | #[test] | ||
282 | fn special_case_result() { | 321 | fn special_case_result() { |
283 | check_assist( | 322 | check_assist( |
284 | replace_if_let_with_match, | 323 | replace_if_let_with_match, |
@@ -309,6 +348,36 @@ fn foo(x: Result<i32, ()>) { | |||
309 | } | 348 | } |
310 | 349 | ||
311 | #[test] | 350 | #[test] |
351 | fn special_case_inverted_result() { | ||
352 | check_assist( | ||
353 | replace_if_let_with_match, | ||
354 | r#" | ||
355 | enum Result<T, E> { Ok(T), Err(E) } | ||
356 | use Result::*; | ||
357 | |||
358 | fn foo(x: Result<i32, ()>) { | ||
359 | $0if let Err(x) = x { | ||
360 | println!("{}", x) | ||
361 | } else { | ||
362 | println!("ok") | ||
363 | } | ||
364 | } | ||
365 | "#, | ||
366 | r#" | ||
367 | enum Result<T, E> { Ok(T), Err(E) } | ||
368 | use Result::*; | ||
369 | |||
370 | fn foo(x: Result<i32, ()>) { | ||
371 | match x { | ||
372 | Err(x) => println!("{}", x), | ||
373 | Ok(_) => println!("ok"), | ||
374 | } | ||
375 | } | ||
376 | "#, | ||
377 | ); | ||
378 | } | ||
379 | |||
380 | #[test] | ||
312 | fn nested_indent() { | 381 | fn nested_indent() { |
313 | check_assist( | 382 | check_assist( |
314 | replace_if_let_with_match, | 383 | replace_if_let_with_match, |