LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.36k stars 319 forks source link

Unused Label but it's used #2247

Closed Mikilio closed 1 year ago

Mikilio commented 1 year ago

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

 local lproc = io.popen(lcmd)
    if not (lproc) or (lproc == '') then
      ::view_err::
      err_msg("Could not launch viewer.")
      mark_viewer_closed()
      return
    end

    -- try to read pid
    local vpid = lproc:read()
    lproc:close()
    -- if couldn't read pid then it was a failure
    if not (vpid) or (vpid == '') then
      goto view_err
    end

Should pass

Actual Behaviour

Ich get two warnings:

Reproduction steps

Have the above code snippet inside a function

Additional Notes

No response

Log File

No response

Bilal2453 commented 1 year ago

There is nothing wrong here. That is just how Lua visibility rules for labels work. image

Trying to run your code, indeed returns a syntax error.

Mikilio commented 1 year ago

Sorry for my ignorance but why is that mark not visible?

Bilal2453 commented 1 year ago

I am using the latest lsp with VSCode, I do get an error on the line goto view_err that says "No visible label 'view_err' ".

Try updating to the latest lsp, or reloading the client.

Bilal2453 commented 1 year ago

The reason for why that label is not visible is that it is in a scope unreachable by the if statement scope, effectively you are doing:

do
  ::view_err:: -- valid everywhere inside of this block
  -- etc
end

do -- a new block
  -- etc
  goto view_err
end

Instead of trying to use goto, simply define a function instead:

local function view_err()
  err_msg("Could not launch viewer.")
  -- etc
end

if not lproc or lproc == "" then
  return view_err()
end

-- etc

if not vpis or vpid == "" then
  return view_err()
end
Mikilio commented 1 year ago

Thank you! I was confused because my LSP only found the error upon saving and opening again. But that solves it for me.