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) =
8
2
|" This function turns the given "case" statement into a if statement.
9
3
|" Assumes that the "case" has only 2 alternatives.
10
4
{
@@ -16,33 +10,22 @@ fun to_if_stmt(case_stmt) =
16
10
);
17
11
match alts[2].f_choices[1]
18
12
| 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]
24
17
)
25
18
| 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]
40
23
)
41
24
}
42
25
43
26
@check(message="CASE statement can be replaced with IF statement",
44
27
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 )))
46
29
fun binary_case_statements(node, except_enums = false) =
47
30
|" Flag a case statement if this statement has only two alternatives, one
48
31
|" containing exactly one choice, the other containing exactly one choice
0 commit comments