Fix the remove unnecessary else action to preserve block tail expr
This commit is contained in:
parent
21f4ff0351
commit
8f6e2127c3
@ -41,9 +41,11 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse) -> Option<Vec<
|
|||||||
indent = indent + 1;
|
indent = indent + 1;
|
||||||
}
|
}
|
||||||
let else_replacement = match if_expr.else_branch()? {
|
let else_replacement = match if_expr.else_branch()? {
|
||||||
ast::ElseBranch::Block(ref block) => {
|
ast::ElseBranch::Block(ref block) => block
|
||||||
block.statements().map(|stmt| format!("\n{indent}{stmt}")).join("")
|
.statements()
|
||||||
}
|
.map(|stmt| format!("\n{indent}{stmt}"))
|
||||||
|
.chain(block.tail_expr().map(|tail| format!("\n{indent}{tail}")))
|
||||||
|
.join(""),
|
||||||
ast::ElseBranch::IfExpr(ref nested_if_expr) => {
|
ast::ElseBranch::IfExpr(ref nested_if_expr) => {
|
||||||
format!("\n{indent}{nested_if_expr}")
|
format!("\n{indent}{nested_if_expr}")
|
||||||
}
|
}
|
||||||
@ -171,6 +173,41 @@ fn test() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remove_unnecessary_else_for_return3() {
|
||||||
|
check_diagnostics_with_needless_return_disabled(
|
||||||
|
r#"
|
||||||
|
fn test(a: bool) -> i32 {
|
||||||
|
if a {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
//^^^^ 💡 weak: remove unnecessary else block
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
check_fix(
|
||||||
|
r#"
|
||||||
|
fn test(a: bool) -> i32 {
|
||||||
|
if a {
|
||||||
|
return 1;
|
||||||
|
} else$0 {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn test(a: bool) -> i32 {
|
||||||
|
if a {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn remove_unnecessary_else_for_return_in_child_if_expr() {
|
fn remove_unnecessary_else_for_return_in_child_if_expr() {
|
||||||
check_diagnostics_with_needless_return_disabled(
|
check_diagnostics_with_needless_return_disabled(
|
||||||
|
Loading…
Reference in New Issue
Block a user