Skip to content

Commit d861c99

Browse files
committed
fix array_index
1 parent 67217db commit d861c99

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#[cfg(test)]
2+
mod test {
3+
use std::{ops::Deref, sync::Arc};
4+
5+
use crate::{DiagnosticCode, VirtualWorkspace};
6+
7+
#[test]
8+
fn test_array_index() {
9+
let mut ws = VirtualWorkspace::new();
10+
let mut emmyrc = ws.analysis.get_emmyrc().deref().clone();
11+
emmyrc.strict.array_index = false;
12+
ws.analysis.update_config(Arc::new(emmyrc));
13+
ws.def(
14+
r#"
15+
---@class Test.Add
16+
---@field a string
17+
18+
---@type int
19+
index = 1
20+
---@type Test.Add[]
21+
items = {}
22+
"#,
23+
);
24+
25+
assert!(ws.check_code_for(
26+
DiagnosticCode::NeedCheckNil,
27+
r#"
28+
local a = items[index]
29+
local b = a.a
30+
"#,
31+
));
32+
}
33+
}

crates/emmylua_code_analysis/src/compilation/test/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod and_or_test;
22
mod annotation_test;
3+
mod array_test;
34
mod closure_generic;
45
mod closure_param_infer_test;
56
mod closure_return_test;

crates/emmylua_code_analysis/src/semantic/infer/infer_index.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,13 @@ fn infer_array_member(
243243

244244
let result_type = match &base_type {
245245
LuaType::Any | LuaType::Unknown => base_type.clone(),
246-
_ => TypeOps::Union.apply(db, base_type, &LuaType::Nil),
246+
_ => {
247+
if db.get_emmyrc().strict.array_index {
248+
TypeOps::Union.apply(db, base_type, &LuaType::Nil)
249+
} else {
250+
base_type.clone()
251+
}
252+
}
247253
};
248254

249255
Ok(result_type)

0 commit comments

Comments
 (0)