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

Needs a "don't use truthy types in a conditional/boolean context" diagnostic #2609

Open
mycroftjr opened this issue Apr 13, 2024 · 1 comment

Comments

@mycroftjr
Copy link

mycroftjr commented Apr 13, 2024

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Type Checking, Diagnostics/Syntax Checking

Expected Behaviour

When variable a has a truthy type (e.g. any type that does not contain boolean or nil), then the following lines should all give warnings, since (assuming the type annotations are correct and Weak Nil Check is off) a cannot be nil or false, and thus can never resolve to false in a boolean context, and so the operations are pointless:

if a then print("1") end  -- will always print 1
if not a then print("2") end  -- will never print 2
local b = a or "3"  -- equivalent to b = a
local c = a and "4" or "5"  -- equivalent to c = "4"

Actual Behaviour

None of those lines warn you about anything under the current diagnostics

Reproduction steps

  1. Type the following into a lua file:
---@param a number (or string, table, etc.)
function A(a)
    if a then print("1") end
    if not a then print("2") end
    local b = a or "3"
    local c = a and "4" or "5"
end
  1. Observe the lack of diagnostic warnings on each line inside the function
@mycroftjr
Copy link
Author

mycroftjr commented Apr 13, 2024

(I would also like a "no-unparameterized-tables" diagnostic where plain table without any <> does not count as a valid type)

@mycroftjr mycroftjr changed the title Needs a "don't use truthy types in a conditional/boolean context" check Needs a "don't use truthy types in a conditional/boolean context" diagnostic Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant