Skip to content

Commit 8ac58b3

Browse files
committed
Rewrite 'use_if_expressions' auto-fix function
1 parent a08e270 commit 8ac58b3

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

lkql_checker/share/lkql/use_if_expressions.lkql

+21-21
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ fun replace_by_expr(if_stmt, ctx) =
1010
{
1111
val is_return = simple_return(if_stmt.f_then_stmts);
1212
val then_stmt = if_stmt.f_then_stmts[1];
13-
val else_stmt = if_stmt.f_else_part.f_stmts[1];
14-
val if_expr = new ParenExpr(new IfExpr(
15-
f_cond_expr = if_stmt.f_cond_expr,
16-
f_then_expr = if is_return then then_stmt.f_return_expr else then_stmt.f_expr,
17-
f_else_expr = if is_return then else_stmt.f_return_expr else else_stmt.f_expr,
18-
f_alternatives=new ElsifExprPartList([
19-
{
20-
val stmt = part.f_stmts[1];
21-
new ElsifExprPart(
22-
f_cond_expr = part.f_cond_expr,
23-
f_then_expr = if is_return then stmt.f_return_expr else stmt.f_expr
24-
)
25-
}
26-
for part in if_stmt.f_alternatives.children
27-
].to_list)
28-
));
13+
val expr_index = if is_return then 1 else 2;
14+
val template_and_rule = (
15+
if is_return
16+
then ("return (if {} then {} {} else {});", "return_stmt_rule")
17+
else (then_stmt.f_dest.text & " := (if {} then {} {} else {});", "assignment_stmt_rule")
18+
);
2919
ctx.replace(
3020
if_stmt,
31-
if is_return
32-
then new ReturnStmt(if_expr)
33-
else new AssignStmt(
34-
f_dest = then_stmt.f_dest,
35-
f_expr = if_expr
21+
ctx.create_from_template(
22+
template_and_rule[1],
23+
template_and_rule[2],
24+
[
25+
if_stmt.f_cond_expr,
26+
then_stmt[expr_index],
27+
new ElsifExprPartList([
28+
new ElsifExprPart(
29+
f_cond_expr = part.f_cond_expr,
30+
f_then_expr = part.f_stmts[1][expr_index]
31+
)
32+
for part in if_stmt.f_alternatives.children
33+
].to_list),
34+
if_stmt.f_else_part.f_stmts[1][expr_index]
35+
]
3636
)
3737
)
3838
}

testsuite/tests/checks/use_if_expressions/test.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ Patched "stmt.adb":
3131
function Stmt (X : in out Integer) return Integer is
3232
Y : Integer;
3333
begin
34-
return(if X = 1 then 1 else 2);if X = 1 then -- NOFLAG
34+
return(if X=1 then 1 else 2);if X = 1 then -- NOFLAG
3535
null;
3636
else
3737
null;
3838
end if;
3939

40-
return(if X = 1 then 1 elsif X = 2 then 2 else 3);if X = 1 then -- NOFLAG
40+
return(if X=1 then 1 elsif X=2 then 2 else 3);if X = 1 then -- NOFLAG
4141
return 1;
4242
elsif X = 2 then
4343
X := 2;
@@ -53,7 +53,7 @@ begin
5353
X := 0;
5454
end if;
5555

56-
X :=(if X >= 2 then X + 1 elsif X <= 0 then X - 1 else 0);if X > 0 then -- NOFLAG
56+
X:=(if X>=2 then X+1 elsif X<=0 then X-1 else 0);if X > 0 then -- NOFLAG
5757
return 2;
5858
end if;
5959

0 commit comments

Comments
 (0)