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 )))
4629fun 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
0 commit comments