|
| 1 | +import stdlib |
| 2 | + |
1 | 3 | fun simple_return(l) = l != null and l.children_count == 1 and l[1] is ReturnStmt |
2 | 4 | fun simple_assignment(l) = |
3 | 5 | l != null and l.children_count == 1 and l[1] is AssignStmt(f_dest: Name) |
4 | 6 |
|
| 7 | +fun replace_by_expr(if_stmt, ctx) = |
| 8 | + |" Replace ``if_stmt`` node by a semantically equivalent node which use an |
| 9 | + |" IfExpr to perform the conditional branching. |
| 10 | + { |
| 11 | + val is_return = simple_return(if_stmt.f_then_stmts); |
| 12 | + 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 | + )); |
| 29 | + ctx.replace( |
| 30 | + 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 |
| 36 | + ) |
| 37 | + ) |
| 38 | + } |
| 39 | + |
5 | 40 | @check(message="IF statement may be replaced by an IF expression", |
6 | | - category="Style", subcategory="Programming Practice") |
| 41 | + category="Style", subcategory="Programming Practice", |
| 42 | + auto_fix=replace_by_expr) |
7 | 43 | fun use_if_expressions(node) = |
8 | 44 | |" Flag ``if`` statements which could be replaced by an ``if`` expression. |
9 | 45 | |" This rule detects the following code patterns: |
@@ -53,12 +89,14 @@ fun use_if_expressions(node) = |
53 | 89 | node is IfStmt |
54 | 90 | when (simple_return(node.f_then_stmts) and |
55 | 91 | simple_return(node.f_else_part?.f_stmts) and |
56 | | - not [s for s in node.f_alternatives.children |
57 | | - if not simple_return(s.f_stmts)]) |
| 92 | + stdlib.all([simple_return(s.f_stmts) |
| 93 | + for s in node.f_alternatives.children])) |
58 | 94 | or (simple_assignment(node.f_then_stmts) and |
59 | 95 | simple_assignment(node.f_else_part?.f_stmts) and |
60 | | - not [s for s in node.f_alternatives.children |
61 | | - if not simple_assignment(s.f_stmts)] and { |
62 | | - val stmts = from node select AssignStmt; |
63 | | - val lhs = stmts[1].f_dest; |
64 | | - not [s for s in stmts if not s.f_dest.p_name_matches(lhs)]}) |
| 96 | + stdlib.all([simple_assignment(s.f_stmts) |
| 97 | + for s in node.f_alternatives.children]) and |
| 98 | + { |
| 99 | + val stmts = from node select AssignStmt; |
| 100 | + val lhs = stmts[1].f_dest; |
| 101 | + stdlib.all([s.f_dest.p_name_matches(lhs) for s in stmts]) |
| 102 | + }) |
0 commit comments