Minor cleanup

This commit is contained in:
Aleksey Kladov 2019-11-13 10:27:21 +03:00
parent 70dd70b1fc
commit 018255efe3
2 changed files with 21 additions and 20 deletions

View file

@ -38,27 +38,27 @@ use crate::{
// ``` // ```
pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; let if_expr: ast::IfExpr = ctx.find_node_at_offset()?;
if if_expr.else_branch().is_some() {
return None;
}
let cond = if_expr.condition()?; let cond = if_expr.condition()?;
let mut if_let_ident: Option<String> = None;
// Check if there is an IfLet that we can handle. // Check if there is an IfLet that we can handle.
match cond.pat() { let if_let_ident = match cond.pat() {
None => {} // No IfLet, supported. None => None, // No IfLet, supported.
Some(TupleStructPat(ref pat)) if pat.args().count() == 1usize => match &pat.path() { Some(TupleStructPat(pat)) if pat.args().count() == 1 => {
Some(p) => match p.qualifier() { let path = pat.path()?;
None => if_let_ident = Some(p.syntax().text().to_string()), match path.qualifier() {
_ => return None, None => Some(path.syntax().to_string()),
}, Some(_) => return None,
_ => return None, }
}, }
_ => return None, // Unsupported IfLet. Some(_) => return None, // Unsupported IfLet.
}; };
let expr = cond.expr()?; let expr = cond.expr()?;
let then_block = if_expr.then_branch()?.block()?; let then_block = if_expr.then_branch()?.block()?;
if if_expr.else_branch().is_some() {
return None;
}
let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::Block::cast)?; let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::Block::cast)?;
@ -100,7 +100,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt
let early_expression = &(early_expression.to_owned() + ";"); let early_expression = &(early_expression.to_owned() + ";");
let new_expr = let new_expr =
if_indent_level.increase_indent(make::if_expression(&expr, early_expression)); if_indent_level.increase_indent(make::if_expression(&expr, early_expression));
replace(new_expr, &then_block, &parent_block, &if_expr) replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)
} }
Some(if_let_ident) => { Some(if_let_ident) => {
// If-let. // If-let.
@ -109,7 +109,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt
&if_let_ident, &if_let_ident,
early_expression, early_expression,
)); ));
replace(new_expr, &then_block, &parent_block, &if_expr) replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)
} }
}; };
edit.target(if_expr.syntax().text_range()); edit.target(if_expr.syntax().text_range());
@ -117,7 +117,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt
edit.set_cursor(cursor_position); edit.set_cursor(cursor_position);
fn replace( fn replace(
new_expr: impl AstNode, new_expr: &SyntaxNode,
then_block: &Block, then_block: &Block,
parent_block: &Block, parent_block: &Block,
if_expr: &ast::IfExpr, if_expr: &ast::IfExpr,
@ -130,7 +130,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt
} else { } else {
end_of_then end_of_then
}; };
let mut then_statements = new_expr.syntax().children_with_tokens().chain( let mut then_statements = new_expr.children_with_tokens().chain(
then_block_items then_block_items
.syntax() .syntax()
.children_with_tokens() .children_with_tokens()
@ -151,9 +151,10 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use crate::helpers::{check_assist, check_assist_not_applicable}; use crate::helpers::{check_assist, check_assist_not_applicable};
use super::*;
#[test] #[test]
fn convert_inside_fn() { fn convert_inside_fn() {
check_assist( check_assist(

View file

@ -19,7 +19,7 @@ use xtask::{
}; };
// Latest stable, feel free to send a PR if this lags behind. // Latest stable, feel free to send a PR if this lags behind.
const REQUIRED_RUST_VERSION: u32 = 38; const REQUIRED_RUST_VERSION: u32 = 39;
struct InstallOpt { struct InstallOpt {
client: Option<ClientOpt>, client: Option<ClientOpt>,