errors and warnings / types / W2061
W2061 warning
a and nil or b (or false in the middle) is always b: the falsy middle value makes the or take over (any mode)
- area
- types (x2xxx)
- fails the build
- with --deny-warnings
a and nil or b (or false in the middle) is always b: when a is truthy,
a and nil is nil, so the or takes over and gives b anyway. The idiom
can't give a falsy value. Write not a and b or nil when b is never falsy,
or an if.
Example
local function gapFor(subtitle: string?): number? return subtitle and nil or 8endprint(gapFor("x"))local function gapFor(subtitle: string?): number? return not subtitle and 8 or nilendprint(gapFor("x"))The compiler reports it like this:
warning[W2061]: `a and nil or b` is always `b`: the `nil` makes the `or` take over (write `not a and b or nil`, or an `if`) ┌─ src/main.wowlua:2:12 │ 2 │ return subtitle and nil or 8 │ ^^^^^^^^^^^^^^^^ here
Offline
$ wowlua explain W2061 W2061 (warning): a and nil or b (or false in the middle) is always b: the falsy middle value makes the or take over (any mode)