Skip to main content

luaur_analysis/functions/
does_call_error.rs

1use luaur_ast::records::ast_expr_call::AstExprCall;
2use luaur_ast::records::ast_expr_constant_bool::AstExprConstantBool;
3use luaur_ast::records::ast_expr_global::AstExprGlobal;
4
5#[allow(non_snake_case)]
6pub fn does_call_error(call: &AstExprCall) -> bool {
7    let global = unsafe {
8        luaur_ast::rtti::ast_node_as::<AstExprGlobal>(
9            call.func as *mut luaur_ast::records::ast_node::AstNode,
10        )
11    };
12
13    if global.is_null() {
14        return false;
15    }
16
17    unsafe {
18        let name = (*global).name.value;
19        if name.is_null() {
20            return false;
21        }
22
23        let name_bytes = core::ffi::CStr::from_ptr(name).to_bytes();
24        if name_bytes == b"error" {
25            return true;
26        }
27
28        if name_bytes == b"assert" {
29            // assert() will error because it is missing the first argument
30            let first_arg = match call.args.iter().next() {
31                Some(arg) => *arg,
32                None => return true,
33            };
34
35            if first_arg.is_null() {
36                return false;
37            }
38
39            let expr = luaur_ast::rtti::ast_node_as::<AstExprConstantBool>(
40                first_arg as *mut luaur_ast::records::ast_node::AstNode,
41            );
42
43            if expr.is_null() {
44                return false;
45            }
46
47            if !(*expr).value {
48                return true;
49            }
50        }
51    }
52
53    false
54}