Skip to main content

luaur_analysis/records/
contains_function_call.rs

1use luaur_ast::records::ast_visitor::AstVisitor;
2
3#[derive(Debug, Clone)]
4pub struct ContainsFunctionCall {
5    pub(crate) also_return: bool,
6    pub(crate) result: bool,
7}
8
9impl ContainsFunctionCall {
10    pub fn new(also_return: bool) -> Self {
11        Self {
12            also_return,
13            result: false,
14        }
15    }
16}
17
18impl Default for ContainsFunctionCall {
19    fn default() -> Self {
20        Self {
21            also_return: false,
22            result: false,
23        }
24    }
25}
26
27impl AstVisitor for ContainsFunctionCall {
28    fn visit_expr(&mut self, _node: *mut core::ffi::c_void) -> bool {
29        // short circuit if result is true
30        !self.result
31    }
32
33    fn visit_expr_call(&mut self, _node: *mut core::ffi::c_void) -> bool {
34        self.result = true;
35        false
36    }
37
38    fn visit_stat_for_in(&mut self, _node: *mut core::ffi::c_void) -> bool {
39        // for in loops perform an implicit function call as part of the iterator protocol
40        self.result = true;
41        false
42    }
43
44    fn visit_stat_return(&mut self, node: *mut core::ffi::c_void) -> bool {
45        if self.also_return {
46            self.result = true;
47            false
48        } else {
49            self.visit_stat(node)
50        }
51    }
52
53    fn visit_expr_function(&mut self, _node: *mut core::ffi::c_void) -> bool {
54        false
55    }
56
57    fn visit_stat_function(&mut self, _node: *mut core::ffi::c_void) -> bool {
58        false
59    }
60
61    fn visit_stat_local_function(&mut self, _node: *mut core::ffi::c_void) -> bool {
62        false
63    }
64
65    fn visit_type(&mut self, _node: *mut core::ffi::c_void) -> bool {
66        true
67    }
68}