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 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'crates/ra_assists/src/handlers') 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; +} +", + ) + } } -- cgit v1.2.3