Skip to content

Commit 9e42bbb

Browse files
ShoyuVanilladavidsemakula
authored andcommitted
Apply indent fix in rust-lang#16575
1 parent 6e9fd75 commit 9e42bbb

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use hir::{db::ExpandDatabase, diagnostics::RemoveUnnecessaryElse, HirFileIdExt};
22
use ide_db::{assists::Assist, source_change::SourceChange};
33
use itertools::Itertools;
44
use syntax::{
5-
ast::{self, edit::IndentLevel},
5+
ast::{
6+
self,
7+
edit::{AstNodeEdit, IndentLevel},
8+
},
69
AstNode, SyntaxToken, TextRange,
710
};
811
use text_edit::TextEdit;
@@ -41,12 +44,15 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse) -> Option<Vec<
4144
indent = indent + 1;
4245
}
4346
let else_replacement = match if_expr.else_branch()? {
44-
ast::ElseBranch::Block(ref block) => block
47+
ast::ElseBranch::Block(block) => block
4548
.statements()
4649
.map(|stmt| format!("\n{indent}{stmt}"))
4750
.chain(block.tail_expr().map(|tail| format!("\n{indent}{tail}")))
4851
.join(""),
49-
ast::ElseBranch::IfExpr(ref nested_if_expr) => {
52+
ast::ElseBranch::IfExpr(mut nested_if_expr) => {
53+
if has_parent_if_expr {
54+
nested_if_expr = nested_if_expr.indent(IndentLevel(1))
55+
}
5056
format!("\n{indent}{nested_if_expr}")
5157
}
5258
};
@@ -251,6 +257,41 @@ fn test() {
251257
);
252258
}
253259

260+
#[test]
261+
fn remove_unnecessary_else_for_return_in_child_if_expr2() {
262+
check_fix(
263+
r#"
264+
fn test() {
265+
if foo {
266+
do_something();
267+
} else if qux {
268+
return bar;
269+
} else$0 if quux {
270+
do_something_else();
271+
} else {
272+
do_something_else2();
273+
}
274+
}
275+
"#,
276+
r#"
277+
fn test() {
278+
if foo {
279+
do_something();
280+
} else {
281+
if qux {
282+
return bar;
283+
}
284+
if quux {
285+
do_something_else();
286+
} else {
287+
do_something_else2();
288+
}
289+
}
290+
}
291+
"#,
292+
);
293+
}
294+
254295
#[test]
255296
fn remove_unnecessary_else_for_break() {
256297
check_diagnostics(

0 commit comments

Comments
 (0)