Skip to content

Commit 6516718

Browse files
committed
fix #396
fix #397
1 parent f58bf63 commit 6516718

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,40 @@ mod test {
6666
assert_eq!(c_ty, LuaType::String);
6767
assert_eq!(d_ty, LuaType::Integer);
6868
}
69+
70+
#[test]
71+
fn test_issue_397() {
72+
let mut ws = VirtualWorkspace::new();
73+
74+
ws.def(
75+
r#"
76+
--- @class A
77+
--- @field field? integer
78+
79+
--- @class B : A
80+
--- @field field integer
81+
82+
--- @type B
83+
local b = { field = 1 }
84+
85+
local key1 --- @type 'field'
86+
local key2 = 'field'
87+
88+
a = b.field -- type is integer - correct
89+
d = b['field'] -- type is integer - correct
90+
e = b[key1] -- type is integer? - wrong
91+
f = b[key2] -- type is integer? - wrong
92+
"#,
93+
);
94+
95+
let a_ty = ws.expr_ty("a");
96+
let d_ty = ws.expr_ty("d");
97+
let e_ty = ws.expr_ty("e");
98+
let f_ty = ws.expr_ty("f");
99+
100+
assert_eq!(a_ty, LuaType::Integer);
101+
assert_eq!(d_ty, LuaType::Integer);
102+
assert_eq!(e_ty, LuaType::Integer);
103+
assert_eq!(f_ty, LuaType::Integer);
104+
}
69105
}

crates/emmylua_code_analysis/src/diagnostic/test/unnecessary_if_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ mod test {
1414
"#
1515
));
1616
}
17+
18+
#[test]
19+
fn test_issue_396() {
20+
let mut ws = VirtualWorkspace::new();
21+
assert!(ws.check_code_for(DiagnosticCode::UnnecessaryIf,
22+
r#"
23+
local a = false ---@type 'a'|'b'
24+
if a ~= 'a' then -- Unnecessary `if` statement: this condition is always truthy [unnecessary-if]
25+
end
26+
"#
27+
));
28+
}
1729
}

0 commit comments

Comments
 (0)