Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] add a warning for goto's that jump across local variable definitions #98

Open
dljsjr opened this issue Jun 27, 2023 · 1 comment

Comments

@dljsjr
Copy link

dljsjr commented Jun 27, 2023

I frequently use goto continue with a ::continue:: label as a proxy for Lua's lack of a first-class continue keyword and I often find myself getting bit by jumping over a local variable declaration that doesn't go out of scope; easy enough to fix by introducing do ... end blocks.

Could be a nice warning/lint/error to be able to see via Luacheck.

Here's an extremely simple self contained example:

🐧 ❯ cat init.lua
function bad_goto_loop()
  while true do
    goto continue
    local some_var = "foo"
    ::continue::
    print("loop end, starting again")
  end
end

🐧 ❯ lua init.lua
lua: init.lua:6: <goto continue> at line 3 jumps into the scope of local 'some_var'
@alerque
Copy link
Member

alerque commented Jun 28, 2023

I'm happy to consider a PR for this.

In the mean time just a couple comments:

  • The Lua interpreter seems to find this error even without executing it.
  • Luacheck already finds and throws two errors related to this code. The problem would be much more interesting if you had an MWE that showed off this problem without also triggering other lint issues.
  • The lua-language-server also spots the unused local.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants