From 1cca6b2a3d4e1981577616d854bca498f4891289 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 3 Mar 2020 16:56:42 +0100 Subject: Fix applicability of inline local --- .../src/handlers/inline_local_variable.rs | 30 +++++++++++++++++++--- crates/ra_assists/src/lib.rs | 12 ++++----- crates/ra_assists/src/marks.rs | 2 ++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index 53a72309b..98e5112df 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs @@ -2,9 +2,9 @@ use ra_syntax::{ ast::{self, AstNode, AstToken}, TextRange, }; +use test_utils::tested_by; -use crate::assist_ctx::ActionBuilder; -use crate::{Assist, AssistCtx, AssistId}; +use crate::{assist_ctx::ActionBuilder, Assist, AssistCtx, AssistId}; // Assist: inline_local_variable // @@ -29,6 +29,11 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { _ => return None, }; if bind_pat.is_mutable() { + tested_by!(test_not_inline_mut_variable); + return None; + } + if !bind_pat.syntax().text_range().contains_inclusive(ctx.frange.range.start()) { + tested_by!(not_applicable_outside_of_bind_pat); return None; } let initializer_expr = let_stmt.initializer()?; @@ -111,6 +116,8 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { + use test_utils::covers; + use crate::helpers::{check_assist, check_assist_not_applicable}; use super::*; @@ -317,9 +324,10 @@ fn foo() { #[test] fn test_not_inline_mut_variable() { + covers!(test_not_inline_mut_variable); check_assist_not_applicable( inline_local_variable, - " + r" fn foo() { let mut a<|> = 1 + 1; a + 1; @@ -651,11 +659,25 @@ fn foo() { fn test_not_applicable_if_variable_unused() { check_assist_not_applicable( inline_local_variable, - " + r" fn foo() { let <|>a = 0; } ", ) } + + #[test] + fn not_applicable_outside_of_bind_pat() { + covers!(not_applicable_outside_of_bind_pat); + check_assist_not_applicable( + inline_local_variable, + r" +fn main() { + let x = <|>1 + 2; + x * 4; +} +", + ) + } } diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index deeada2de..6d81a6396 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -178,19 +178,19 @@ mod helpers { (db, file_id) } - pub(crate) fn check_assist(assist: AssistHandler, before: &str, after: &str) { - check(assist, before, ExpectedResult::After(after)); + pub(crate) fn check_assist(assist: AssistHandler, ra_fixture: &str, after: &str) { + check(assist, ra_fixture, ExpectedResult::After(after)); } // FIXME: instead of having a separate function here, maybe use // `extract_ranges` and mark the target as ` ` in the // fixuture? - pub(crate) fn check_assist_target(assist: AssistHandler, before: &str, target: &str) { - check(assist, before, ExpectedResult::Target(target)); + pub(crate) fn check_assist_target(assist: AssistHandler, ra_fixture: &str, target: &str) { + check(assist, ra_fixture, ExpectedResult::Target(target)); } - pub(crate) fn check_assist_not_applicable(assist: AssistHandler, before: &str) { - check(assist, before, ExpectedResult::NotApplicable); + pub(crate) fn check_assist_not_applicable(assist: AssistHandler, ra_fixture: &str) { + check(assist, ra_fixture, ExpectedResult::NotApplicable); } enum ExpectedResult<'a> { diff --git a/crates/ra_assists/src/marks.rs b/crates/ra_assists/src/marks.rs index c20e4db9e..cef3df4e5 100644 --- a/crates/ra_assists/src/marks.rs +++ b/crates/ra_assists/src/marks.rs @@ -4,4 +4,6 @@ test_utils::marks!( introduce_var_in_comment_is_not_applicable test_introduce_var_expr_stmt test_introduce_var_last_expr + not_applicable_outside_of_bind_pat + test_not_inline_mut_variable ); -- cgit v1.2.3