Skip to content

Commit e8e05bb

Browse files
committed
Merge branch 'topic/fix_use_if_expr' into 'master'
Fix the "Use_If_Expressions" rule Closes #406 See merge request eng/libadalang/langkit-query-language!347
2 parents 53f10c4 + eba7f15 commit e8e05bb

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lkql_checker/share/lkql/use_if_expressions.lkql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
fun simple_return(l) = l.children_count == 1 and l[1] is ReturnStmt
1+
fun simple_return(l) = l != null and l.children_count == 1 and l[1] is ReturnStmt
22
fun simple_assignment(l) =
3-
l.children_count == 1 and l[1] is AssignStmt(f_dest: Name)
3+
l != null and l.children_count == 1 and l[1] is AssignStmt(f_dest: Name)
44

55
@check(message="IF statement may be replaced by an IF expression",
66
category="Style", subcategory="Programming Practice")
@@ -52,11 +52,11 @@ fun use_if_expressions(node) =
5252
|" end if;
5353
node is IfStmt
5454
when (simple_return(node.f_then_stmts) and
55-
simple_return(node.f_else_part.f_stmts) and
55+
simple_return(node.f_else_part?.f_stmts) and
5656
not [s for s in node.f_alternatives.children
5757
if not simple_return(s.f_stmts)])
5858
or (simple_assignment(node.f_then_stmts) and
59-
simple_assignment(node.f_else_part.f_stmts) and
59+
simple_assignment(node.f_else_part?.f_stmts) and
6060
not [s for s in node.f_alternatives.children
6161
if not simple_assignment(s.f_stmts)] and {
6262
val stmts = from node select AssignStmt;

testsuite/tests/checks/use_if_expressions/stmt.adb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ begin
4545
X := 0;
4646
end if;
4747

48+
if X > 0 then -- NOFLAG
49+
return 2;
50+
end if:
51+
4852
return X;
4953
end Stmt;

0 commit comments

Comments
 (0)