LuaLS / LuaLS/lua-language-server

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

Open
#2,609 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

### 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:
```lua
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:
```lua
---@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
```
2. Observe the lack of diagnostic warnings on each line inside the function

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Lua snippet in a Lua file through the Visual Studio Code extension, then trace the type-checking and diagnostic entry points that handle conditionals and boolean expressions. Done means the requested cases produce appropriate warnings; the separate no-unparameterized-tables request also needs its scope decided.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.