1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::functions::emit_warning::emit_warning;
use crate::records::lint_global_local::LintGlobalLocal;
use luaur_config::enums::code::Code;
impl LintGlobalLocal {
pub fn report(&mut self) {
let context = self.context;
let placeholder = unsafe { (*context).placeholder };
for i in 0..self.global_refs.len() {
let gv = self.global_refs[i];
let g = unsafe { self.globals.find(&(*gv).name) };
match g {
None => unsafe {
emit_warning(
&mut *context,
Code::Code_UnknownGlobal,
(*gv).base.base.location,
format_args!(
"Unknown global '{}'; consider assigning to it first",
name_str((*gv).name.value)
),
);
},
Some(g) if !g.assigned && !g.builtin => unsafe {
emit_warning(
&mut *context,
Code::Code_UnknownGlobal,
(*gv).base.base.location,
format_args!(
"Unknown global '{}'; consider assigning to it first",
name_str((*gv).name.value)
),
);
},
Some(g) => {
if let Some(replacement) = g.deprecated {
unsafe {
if !replacement.is_null()
&& !core::ffi::CStr::from_ptr(replacement).to_bytes().is_empty()
{
emit_warning(
&mut *context,
Code::Code_DeprecatedGlobal,
(*gv).base.base.location,
format_args!(
"Global '{}' is deprecated, use '{}' instead",
name_str((*gv).name.value),
name_str(replacement)
),
);
} else {
emit_warning(
&mut *context,
Code::Code_DeprecatedGlobal,
(*gv).base.base.location,
format_args!(
"Global '{}' is deprecated",
name_str((*gv).name.value)
),
);
}
}
}
}
}
}
for (_name, g) in self.globals.iter() {
if !g.functionRef.is_empty()
&& g.assigned
&& unsafe { (*g.firstRef).name } != placeholder
{
let top = *g.functionRef.last().unwrap();
unsafe {
if !(*top).debugname.value.is_null() {
emit_warning(
&mut *context,
Code::Code_GlobalUsedAsLocal,
(*g.firstRef).base.base.location,
format_args!(
"Global '{}' is only used in the enclosing function '{}'; consider changing it to local",
name_str((*g.firstRef).name.value),
name_str((*top).debugname.value)
),
);
} else {
emit_warning(
&mut *context,
Code::Code_GlobalUsedAsLocal,
(*g.firstRef).base.base.location,
format_args!(
"Global '{}' is only used in the enclosing function defined at line {}; consider changing it to local",
name_str((*g.firstRef).name.value),
(*top).base.base.location.begin.line + 1
),
);
}
}
} else if g.assigned
&& !g.readBeforeWritten
&& !g.definedInModuleScope
&& unsafe { (*g.firstRef).name } != placeholder
{
unsafe {
emit_warning(
&mut *context,
Code::Code_GlobalUsedAsLocal,
(*g.firstRef).base.base.location,
format_args!(
"Global '{}' is never read before being written. Consider changing it to local",
name_str((*g.firstRef).name.value)
),
);
}
}
}
}
}
fn name_str(value: *const core::ffi::c_char) -> alloc::borrow::Cow<'static, str> {
if value.is_null() {
alloc::borrow::Cow::Borrowed("")
} else {
unsafe { core::ffi::CStr::from_ptr(value).to_string_lossy() }
}
}