v0.13.7
errors and warnings / names

errors and warnings / names / W1004

W1004 warning

Local shadows another local, or a local function is named like a local of the same block; also a parameter named self inside a class's method or constructor, which hides the instance (a plain function M:m()'s self may be shadowed, as frame handlers do)

area
names (x1xxx)
fails the build
with --deny-warnings

A local hides another one: it has the name of a local of an enclosing scope, or a local function is named like a local declared before it in the same block (from there on, the earlier one is out of reach, even inside the new function). A parameter named self in a class's method or constructor also gets this warning, because it hides the instance. Hidden names are a common source of "it reads the wrong value" bugs; rename one of them. Names starting with _ are exempt.

Example

× failswowlua
local count = 1do    local count = 2    print(count)endprint(count)
✓ compileswowlua
local count = 1do    local bonus = 2    print(bonus)endprint(count)

The compiler reports it like this:

warning[W1004]: `count` shadows a local of an enclosing scope
  ┌─ src/main.wowlua:3:11
  │
1 │ local count = 1
  │       ----- shadowed local
2 │ do
3 │     local count = 2
  │           ^^^^^ declared here
codemessage
E1004A parameter's default reads that parameter or a later one, which is not set yet (in Lua every parameter is in scope there, so the name is the parameter, not an outer one)

Offline

terminal
$ wowlua explain W1004
W1004 (warning): Local shadows another local, or a local function is named like a local of the same block; also a parameter named self inside a class's method or constructor, which hides the instance (a plain function M:m()'s self may be shadowed, as frame handlers do)

source on GitHub ↗