Skip to main content

luaur_analysis/methods/
lint_same_line_statement_visit.rs

1use crate::functions::emit_warning::emit_warning;
2use crate::records::lint_same_line_statement::LintSameLineStatement;
3use core::ffi::c_void;
4use luaur_ast::records::ast_stat_block::AstStatBlock;
5use luaur_ast::records::ast_stat_local::AstStatLocal;
6use luaur_config::enums::code::Code;
7
8impl LintSameLineStatement {
9    pub fn visit_stat_block(&mut self, node: *mut c_void) -> bool {
10        let node = unsafe { &*node.cast::<AstStatBlock>() };
11        let body = &node.body;
12
13        for i in 1..body.size {
14            let last_stmt = unsafe { *body.data.add((i - 1) as usize) };
15            let current_stmt = unsafe { *body.data.add(i as usize) };
16
17            let last = unsafe { (*last_stmt).base.location };
18            let location = unsafe { (*current_stmt).base.location };
19
20            if location.begin.line != last.end.line {
21                continue;
22            }
23
24            if location.begin.line == self.last_line {
25                continue;
26            }
27
28            let last_is_local =
29                unsafe { luaur_ast::rtti::ast_node_is::<AstStatLocal>(&(*last_stmt).base) };
30            let current_is_block =
31                unsafe { luaur_ast::rtti::ast_node_is::<AstStatBlock>(&(*current_stmt).base) };
32
33            if last_is_local && current_is_block {
34                continue;
35            }
36
37            let last_has_semicolon = unsafe { (*last_stmt).has_semicolon };
38            if last_has_semicolon {
39                continue;
40            }
41
42            let context = unsafe { &mut *self.context };
43            emit_warning(
44                context,
45                Code::Code_SameLineStatement,
46                location,
47                format_args!("A new statement is on the same line; add semi-colon on previous statement to silence"),
48            );
49
50            self.last_line = location.begin.line;
51        }
52
53        true
54    }
55}