From cd3855fb2be78e2dc2d2ca4b8e950d9d9d9081bb Mon Sep 17 00:00:00 2001 From: phanium <91544758+phanen@users.noreply.github.com> Date: Mon, 10 Feb 2025 00:40:43 +0800 Subject: fix(lua): vim.tbl_get({}, nil, 1) should return nil #32218 Problem: `vim.tbl_get(tbl, nil, 1)` returns `tbl` itself. In this case, `keys` is not empty, but `ipairs` skips the iteration: local keys = { nil, 1 } assert(#keys == 2) for i, k in ipairs(keys) do assert(false, 'unreachable') end Solution: Use `select("#", ...)` and `select(i, ...)` to ensure consistency for count and iteration. --- test/functional/lua/vim_spec.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'test/functional') diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 55e5158596..8e22644339 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -970,6 +970,7 @@ describe('lua stdlib', function() ) eq(NIL, exec_lua("return vim.tbl_get({}, 'missing_key')")) eq(NIL, exec_lua('return vim.tbl_get({})')) + eq(NIL, exec_lua("return vim.tbl_get({}, nil, 'key')")) eq(1, exec_lua("return select('#', vim.tbl_get({}))")) eq(1, exec_lua("return select('#', vim.tbl_get({ nested = {} }, 'nested', 'missing_key'))")) end) -- cgit