fix: avoid arithmetic error (subtracting 1 from nil) #396
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why this change?
I'm writing my own neotest adapter and I'm sending in error messages along with line numbers:
Full source here.
Sometimes, and only when a line number (an integer) is sent in, I see this crash in Neovim, and neotest breaks:
Turns out that the function call (found on line diagnostic.lua:103) can return either
integer
ornil
, and it's not a good idea to subtract 1 from its return value without first checking it for beingnil
:neotest/lua/neotest/consumers/diagnostic.lua
Line 103 in a11ceb2
Function signature:
What was done in this PR?
Instead of attempting to subtract 1 from potentially
nil
, I first check if the returned value is nil or not.This completely remedies the crash and the error is now shown as expected in the buffer, which is very nice.
Notes
0
is a good value here to use forcol
, in case the returned value from the pattern matching isnil
.{error = { message = line.Output, line = line_number - 1 }
when sending in the line number into neovim, which might help alleviate this problem.