Skip to content

Commit

Permalink
Merge branch 'topic/fix_use_if_expr' into 'master'
Browse files Browse the repository at this point in the history
Fix the "Use_If_Expressions" rule

Closes #406

See merge request eng/libadalang/langkit-query-language!347
  • Loading branch information
HugoGGuerrier committed Dec 9, 2024
2 parents 53f10c4 + eba7f15 commit e8e05bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lkql_checker/share/lkql/use_if_expressions.lkql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fun simple_return(l) = l.children_count == 1 and l[1] is ReturnStmt
fun simple_return(l) = l != null and l.children_count == 1 and l[1] is ReturnStmt
fun simple_assignment(l) =
l.children_count == 1 and l[1] is AssignStmt(f_dest: Name)
l != null and l.children_count == 1 and l[1] is AssignStmt(f_dest: Name)

@check(message="IF statement may be replaced by an IF expression",
category="Style", subcategory="Programming Practice")
Expand Down Expand Up @@ -52,11 +52,11 @@ fun use_if_expressions(node) =
|" end if;
node is IfStmt
when (simple_return(node.f_then_stmts) and
simple_return(node.f_else_part.f_stmts) and
simple_return(node.f_else_part?.f_stmts) and
not [s for s in node.f_alternatives.children
if not simple_return(s.f_stmts)])
or (simple_assignment(node.f_then_stmts) and
simple_assignment(node.f_else_part.f_stmts) and
simple_assignment(node.f_else_part?.f_stmts) and
not [s for s in node.f_alternatives.children
if not simple_assignment(s.f_stmts)] and {
val stmts = from node select AssignStmt;
Expand Down
4 changes: 4 additions & 0 deletions testsuite/tests/checks/use_if_expressions/stmt.adb
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ begin
X := 0;
end if;

if X > 0 then -- NOFLAG
return 2;
end if:

return X;
end Stmt;

0 comments on commit e8e05bb

Please sign in to comment.