Skip to content

Commit a08e270

Browse files
committed
Rewrite 'binary_case_statements' auto-fix function
1 parent 42f66c0 commit a08e270

File tree

3 files changed

+12
-37
lines changed

3 files changed

+12
-37
lines changed

lkql_checker/share/lkql/binary_case_statements.lkql

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
# Flag a case statement if this statement has only two alternatives, one
2-
# containing exactly one choice, the other containing exactly one choice
3-
# or the `OTHERS' choice.
4-
# This rule has an optional parameter Except_Enums: exclude case statements on
5-
# enumerated types.
6-
7-
fun to_if_stmt(case_stmt) =
1+
fun to_if_stmt(case_stmt, ctx) =
82
|" This function turns the given "case" statement into a if statement.
93
|" Assumes that the "case" has only 2 alternatives.
104
{
@@ -16,33 +10,22 @@ fun to_if_stmt(case_stmt) =
1610
);
1711
match alts[2].f_choices[1]
1812
| OthersDesignator =>
19-
new IfStmt(
20-
f_cond_expr=cond,
21-
f_then_stmts=alts[1].f_stmts,
22-
f_alternatives=new ElsifStmtPartList(),
23-
f_else_part=new ElsePart(alts[2].f_stmts)
13+
ctx.create_from_template(
14+
"if {} then {} else {} end if;",
15+
"if_stmt_rule",
16+
[cond, alts[1].f_stmts, alts[2].f_stmts]
2417
)
2518
| choice =>
26-
new IfStmt(
27-
f_cond_expr=cond,
28-
f_then_stmts=alts[1].f_stmts,
29-
f_alternatives=new ElsifStmtPartList([
30-
new ElsifStmtPart(
31-
f_cond_expr=new RelationOp(
32-
f_left=case_stmt.f_expr,
33-
f_op=new OpEq(),
34-
f_right=choice
35-
),
36-
f_stmts=alts[2].f_stmts
37-
)
38-
]),
39-
f_else_part=null
19+
ctx.create_from_template(
20+
"if {} then {} elsif {} = {} then {} end if;",
21+
"if_stmt_rule",
22+
[cond, alts[1].f_stmts, case_stmt.f_expr, choice, alts[2].f_stmts]
4023
)
4124
}
4225

4326
@check(message="CASE statement can be replaced with IF statement",
4427
category="Style", subcategory="Programming Practice",
45-
auto_fix=(n, ctx) => ctx.replace(n, to_if_stmt(n)))
28+
auto_fix=(n, ctx) => ctx.replace(n, to_if_stmt(n, ctx)))
4629
fun binary_case_statements(node, except_enums = false) =
4730
|" Flag a case statement if this statement has only two alternatives, one
4831
|" containing exactly one choice, the other containing exactly one choice

testsuite/tests/checks/binary_case_statements/test.out

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ Patched "binary_case_statements.adb":
3131
procedure Main is
3232
procedure Test (I : Integer);
3333
begin
34-
if 1 =1 then null;
35-
else null;
36-
end if;if 1 =1 then Test (2);
37-
elsif 1 =2 then Test (1);
38-
end if;if True =True then null;
39-
elsif True =False then null;
40-
end if;case 1 is -- NOFLAG
34+
if 1=1 then null;else null;end if;if 1=1 then Test(2);elsif 1=2 then Test(1);end if;if True=True then null;elsif True=False then null;end if;case 1 is -- NOFLAG
4135
when 1 | 2 => null;
4236
when others => null;
4337
end case;

testsuite/tests/checks/binary_case_statements_enums/test.out

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@ begin
2424
when others => null;
2525
end case;
2626

27-
if 1 =1 then null;
28-
else null;
29-
end if;end Paths;
27+
if 1=1 then null;else null;end if;end Paths;
3028

0 commit comments

Comments
 (0)