File tree Expand file tree Collapse file tree 3 files changed +64
-1
lines changed
crates/emmylua_code_analysis/src Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,39 @@ use crate::{
1111pub fn bind_type (
1212 db : & mut DbIndex ,
1313 type_owner : LuaTypeOwner ,
14- type_cache : LuaTypeCache ,
14+ mut type_cache : LuaTypeCache ,
1515) -> Option < ( ) > {
1616 let decl_type_cache = db. get_type_index ( ) . get_type_cache ( & type_owner) ;
1717
1818 if decl_type_cache. is_none ( ) {
19+ // type backward
20+ if type_cache. is_infer ( ) {
21+ if let LuaTypeOwner :: Decl ( decl_id) = & type_owner {
22+ if let Some ( refs) = db
23+ . get_reference_index ( )
24+ . get_decl_references ( & decl_id. file_id , decl_id)
25+ {
26+ if refs. iter ( ) . any ( |it| it. is_write ) {
27+ match & type_cache. as_type ( ) {
28+ LuaType :: IntegerConst ( _) => {
29+ type_cache = LuaTypeCache :: InferType ( LuaType :: Integer )
30+ }
31+ LuaType :: StringConst ( _) => {
32+ type_cache = LuaTypeCache :: InferType ( LuaType :: String )
33+ }
34+ LuaType :: BooleanConst ( _) => {
35+ type_cache = LuaTypeCache :: InferType ( LuaType :: Boolean )
36+ }
37+ LuaType :: FloatConst ( _) => {
38+ type_cache = LuaTypeCache :: InferType ( LuaType :: Number )
39+ }
40+ _ => { }
41+ }
42+ }
43+ }
44+ }
45+ }
46+
1947 db. get_type_index_mut ( )
2048 . bind_type ( type_owner. clone ( ) , type_cache) ;
2149 migrate_global_members_when_type_resolve ( db, type_owner) ;
Original file line number Diff line number Diff line change 716716 "# ,
717717 ) ) ;
718718 }
719+
720+ #[ test]
721+ fn test_issue_472 ( ) {
722+ let mut ws = VirtualWorkspace :: new ( ) ;
723+
724+ assert ! ( ws. check_code_for(
725+ DiagnosticCode :: UnnecessaryIf ,
726+ r#"
727+ worldLightLevel = 0
728+ worldLightColor = 0
729+ Gmae = {}
730+ ---@param color integer
731+ ---@param level integer
732+ function Game.setWorldLight(color, level)
733+ local previousColor = worldLightColor
734+ local previousLevel = worldLightLevel
735+
736+ worldLightColor = color
737+ worldLightLevel = level
738+
739+ if worldLightColor ~= previousColor or worldLightLevel ~= previousLevel then
740+ -- Do something...
741+ end
742+ end
743+ "#
744+ ) )
745+ }
719746}
Original file line number Diff line number Diff line change @@ -61,6 +61,14 @@ impl LuaTypeCache {
6161 LuaTypeCache :: InferType ( ty) => ty,
6262 }
6363 }
64+
65+ pub fn is_infer ( & self ) -> bool {
66+ matches ! ( self , LuaTypeCache :: InferType ( _) )
67+ }
68+
69+ pub fn is_doc ( & self ) -> bool {
70+ matches ! ( self , LuaTypeCache :: DocType ( _) )
71+ }
6472}
6573
6674impl std:: ops:: Deref for LuaTypeCache {
You can’t perform that action at this time.
0 commit comments