Fix applicability of inline local

This commit is contained in:
Aleksey Kladov 2020-03-03 16:56:42 +01:00
parent 8f3677a94a
commit 1cca6b2a3d
3 changed files with 34 additions and 10 deletions

View file

@ -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<Assist> {
_ => 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<Assist> {
#[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;
}
",
)
}
}

View file

@ -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 `<target> </target>` 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> {

View file

@ -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
);