Skip to content

Need check nil: I checked the code for nil and the LSP is still complaining, what am I missing? #3254

Answered by tomlau10
andradei asked this question in Q&A
Discussion options

You must be logged in to vote

The warning is saying that you didn't check nil for rectangle_region when you call rectangle_region:gsub(...) 😅

  • you first assign it on the line local rectangle_region, e = proc(slurp_command)
    => rectangle_region maybe nil here I believe
  • in your code pattern, you maybe having proc() to return a pair, i.e.
    • (string, nil) or (nil, error)
    • however LuaLS doesn't support mutually-exclusive multi-return yet
      (related discussion: #3102)
    • it will infer each return value individually => so rectangle_region: string|nil and e: error|nil
  • on your gsub() line, you are doing <string|nil>:gsub(...) in the viewpoint of LuaLS
    => missing nil check

In the meantime, if you don't want to change the code struc…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@tomlau10
Comment options

Answer selected by andradei
@andradei
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants