diff options
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/handlers/inline_local_variable.rs | 29 | ||||
-rw-r--r-- | crates/ra_assists/src/marks.rs | 5 |
2 files changed, 21 insertions, 13 deletions
diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index eb5112343..3bfcba8ff 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs | |||
@@ -1,3 +1,4 @@ | |||
1 | use ra_ide_db::defs::Definition; | ||
1 | use ra_syntax::{ | 2 | use ra_syntax::{ |
2 | ast::{self, AstNode, AstToken}, | 3 | ast::{self, AstNode, AstToken}, |
3 | TextRange, | 4 | TextRange, |
@@ -37,6 +38,15 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { | |||
37 | return None; | 38 | return None; |
38 | } | 39 | } |
39 | let initializer_expr = let_stmt.initializer()?; | 40 | let initializer_expr = let_stmt.initializer()?; |
41 | |||
42 | let def = ctx.sema.to_def(&bind_pat)?; | ||
43 | let def = Definition::Local(def); | ||
44 | let refs = def.find_usages(ctx.db, None); | ||
45 | if refs.is_empty() { | ||
46 | tested_by!(test_not_applicable_if_variable_unused); | ||
47 | return None; | ||
48 | }; | ||
49 | |||
40 | let delete_range = if let Some(whitespace) = let_stmt | 50 | let delete_range = if let Some(whitespace) = let_stmt |
41 | .syntax() | 51 | .syntax() |
42 | .next_sibling_or_token() | 52 | .next_sibling_or_token() |
@@ -49,16 +59,14 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { | |||
49 | } else { | 59 | } else { |
50 | let_stmt.syntax().text_range() | 60 | let_stmt.syntax().text_range() |
51 | }; | 61 | }; |
52 | let refs = ctx.sema.find_all_refs(&bind_pat); | ||
53 | if refs.is_empty() { | ||
54 | return None; | ||
55 | }; | ||
56 | 62 | ||
57 | let mut wrap_in_parens = vec![true; refs.len()]; | 63 | let mut wrap_in_parens = vec![true; refs.len()]; |
58 | 64 | ||
59 | for (i, desc) in refs.iter().enumerate() { | 65 | for (i, desc) in refs.iter().enumerate() { |
60 | let usage_node = | 66 | let usage_node = ctx |
61 | ctx.covering_node_for_range(desc.range).ancestors().find_map(ast::PathExpr::cast)?; | 67 | .covering_node_for_range(desc.file_range.range) |
68 | .ancestors() | ||
69 | .find_map(ast::PathExpr::cast)?; | ||
62 | let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast); | 70 | let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast); |
63 | let usage_parent = match usage_parent_option { | 71 | let usage_parent = match usage_parent_option { |
64 | Some(u) => u, | 72 | Some(u) => u, |
@@ -103,11 +111,9 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { | |||
103 | move |edit: &mut ActionBuilder| { | 111 | move |edit: &mut ActionBuilder| { |
104 | edit.delete(delete_range); | 112 | edit.delete(delete_range); |
105 | for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { | 113 | for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { |
106 | if should_wrap { | 114 | let replacement = |
107 | edit.replace(desc.range, init_in_paren.clone()) | 115 | if should_wrap { init_in_paren.clone() } else { init_str.clone() }; |
108 | } else { | 116 | edit.replace(desc.file_range.range, replacement) |
109 | edit.replace(desc.range, init_str.clone()) | ||
110 | } | ||
111 | } | 117 | } |
112 | edit.set_cursor(delete_range.start()) | 118 | edit.set_cursor(delete_range.start()) |
113 | }, | 119 | }, |
@@ -657,6 +663,7 @@ fn foo() { | |||
657 | 663 | ||
658 | #[test] | 664 | #[test] |
659 | fn test_not_applicable_if_variable_unused() { | 665 | fn test_not_applicable_if_variable_unused() { |
666 | covers!(test_not_applicable_if_variable_unused); | ||
660 | check_assist_not_applicable( | 667 | check_assist_not_applicable( |
661 | inline_local_variable, | 668 | inline_local_variable, |
662 | r" | 669 | r" |
diff --git a/crates/ra_assists/src/marks.rs b/crates/ra_assists/src/marks.rs index cef3df4e5..22404ee80 100644 --- a/crates/ra_assists/src/marks.rs +++ b/crates/ra_assists/src/marks.rs | |||
@@ -1,9 +1,10 @@ | |||
1 | //! See test_utils/src/marks.rs | 1 | //! See test_utils/src/marks.rs |
2 | 2 | ||
3 | test_utils::marks!( | 3 | test_utils::marks![ |
4 | introduce_var_in_comment_is_not_applicable | 4 | introduce_var_in_comment_is_not_applicable |
5 | test_introduce_var_expr_stmt | 5 | test_introduce_var_expr_stmt |
6 | test_introduce_var_last_expr | 6 | test_introduce_var_last_expr |
7 | not_applicable_outside_of_bind_pat | 7 | not_applicable_outside_of_bind_pat |
8 | test_not_inline_mut_variable | 8 | test_not_inline_mut_variable |
9 | ); | 9 | test_not_applicable_if_variable_unused |
10 | ]; | ||