Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,21 @@ mod test {
"#
));
}

#[test]
fn test_table_generic_index_not_unnecessary() {
let mut ws = VirtualWorkspace::new();
assert!(ws.check_code_for(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The test name test_table_generic_index_not_unnecessary suggests that this test should pass if the UnnecessaryIf diagnostic is not found. Based on other tests in this file, check_code_for seems to return true when a diagnostic is found. If that's the case, this assertion should be inverted to assert!(!ws.check_code_for(...)) to correctly verify the fix.

Suggested change
assert!(ws.check_code_for(
assert!(!ws.check_code_for(

DiagnosticCode::UnnecessaryIf,
r#"
---@type table<string, number>
local t = {}
local k = "hello"
local v = t[k]
if v then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue this is unnecessary. If you want it not to be then the type of t should be table<string,number?>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats what I did, quite counterintuitive to me, but u r right

print(v)
end
"#
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,8 @@ fn infer_member_by_index_table_generic(
let index_key = index_expr.get_index_key().ok_or(InferFailReason::None)?;
let key_type = &table_params[0];
let value_type = &table_params[1];
infer_index_metamethod(db, cache, &index_key, key_type, value_type)
let result = infer_index_metamethod(db, cache, &index_key, key_type, value_type)?;
Ok(TypeOps::Union.apply(db, &result, &LuaType::Nil))
}

fn infer_global_field_member(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn infer_table_generic_raw_member_type(
LuaMemberKey::None => return Err(InferFailReason::FieldNotFound),
};
if check_type_compact(db, key_type, &access_key_type).is_ok() {
return Ok(value_type.clone());
return Ok(TypeOps::Union.apply(db, value_type, &LuaType::Nil));
}

Err(InferFailReason::FieldNotFound)
Expand Down
4 changes: 2 additions & 2 deletions crates/emmylua_ls/src/handlers/test/hover_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ mod tests {
end
"#,
VirtualHoverResult {
value: "```lua\n(field) _cfg: number\n```".to_string(),
value: "```lua\n(field) _cfg: number?\n```".to_string(),
},
));

Expand All @@ -293,7 +293,7 @@ mod tests {
end
"#,
VirtualHoverResult {
value: "```lua\n(field) _cfg: number\n```".to_string(),
value: "```lua\n(field) _cfg: number?\n```".to_string(),
},
));
Ok(())
Expand Down