From 26c918d03f5b38df900316c0601065ec1ea96264 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 15 Dec 2022 02:27:23 +0100 Subject: fix(lua): always return nil values in vim.tbl_get when no results While `return` and `return nil` are for most intents and purposes identical, there are situations where they're not. For example, calculating the amount of values via the `select()` function will yield varying results: ```lua local function nothing() return end local function null() return nil end select('#', nothing()) -- 0 select('#', null()) -- 1 ``` `vim.tbl_get` currently returns both nil and no results, which makes it unreliable to use in certain situations without manually accounting for these discrepancies. --- runtime/lua/vim/shared.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim/shared.lua') diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 9129500866..5ffd11682c 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -395,15 +395,14 @@ end function vim.tbl_get(o, ...) local keys = { ... } if #keys == 0 then - return + return nil end for i, k in ipairs(keys) do - if type(o[k]) ~= 'table' and next(keys, i) then - return nil - end o = o[k] if o == nil then - return + return nil + elseif type(o) ~= 'table' and next(keys, i) then + return nil end end return o -- cgit