From 4fe51dfdae84f139bb92f8517aff914c4adb052c Mon Sep 17 00:00:00 2001 From: glepnir Date: Wed, 23 Jul 2025 08:40:24 +0800 Subject: [PATCH] fix(iter): ArrayIter:last returns nil when filtered to empty #34697 Problem: After filtering out all elements, ArrayIter:last still returns a stale element. Solution: Add check for self._head == self._tail and return nil early. Fix #34696 --- runtime/lua/vim/iter.lua | 3 +++ test/functional/lua/iter_spec.lua | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/runtime/lua/vim/iter.lua b/runtime/lua/vim/iter.lua index c1dd20f745..f3062564ce 100644 --- a/runtime/lua/vim/iter.lua +++ b/runtime/lua/vim/iter.lua @@ -1010,6 +1010,9 @@ end ---@private function ArrayIter:last() + if self._head >= self._tail then + return nil + end local inc = self._head < self._tail and 1 or -1 local v = self._table[self._tail - inc] self._head = self._tail diff --git a/test/functional/lua/iter_spec.lua b/test/functional/lua/iter_spec.lua index 64f8d5df79..1e4012521e 100644 --- a/test/functional/lua/iter_spec.lua +++ b/test/functional/lua/iter_spec.lua @@ -405,6 +405,15 @@ describe('vim.iter', function() local s = 'abcdefghijklmnopqrstuvwxyz' eq('z', vim.iter(vim.split(s, '')):last()) eq('z', vim.iter(vim.gsplit(s, '')):last()) + eq( + nil, + vim + .iter({ 1, 2, 3, 4, 5 }) + :filter(function() + return false + end) + :last() + ) end) it('enumerate()', function()