Skip to content

Commit

Permalink
Exempt _ENV from 214 warning, tweak error (#64)
Browse files Browse the repository at this point in the history
* test: Add test to exempt builtin underscore prefixes causing warning
* fix: Exempt special builtin _ENV from 214 warning
* chore: Tweak warning message for 214 to be more explicit
  • Loading branch information
alerque committed Apr 23, 2022
2 parents 62f1869 + 6421156 commit 70dfac6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions spec/unused_locals_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ end
]])
end)

it("exempt _ENV from warning on usage with underscore prefix", function()
assert_warnings({}, [[ return function(_ENV) return type(_ENV) end ]])
end)

it("detects unused implicit self", function()
assert_warnings({
{code = "212", name = "self", self = true, line = 2, column = 11, end_column = 11}
Expand Down
4 changes: 2 additions & 2 deletions src/luacheck/stages/detect_unused_locals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ stage.warnings = {
fields = {"name", "func", "secondary", "useless", "recursive", "mutually_recursive"}},
["212"] = {message_format = unused_arg_message_format, fields = {"name", "self"}},
["213"] = {message_format = "unused loop variable {name!}", fields = {"name"}},
["214"] = {message_format = "used variable {name!}", fields = {"name"}},
["214"] = {message_format = "used variable {name!} with unused hint", fields = {"name"}},
["221"] = {message_format = "variable {name!} is never set", fields = {"name", "secondary"}},
["231"] = {message_format = "variable {name!} is never accessed", fields = {"name", "secondary"}},
["232"] = {message_format = "argument {name!} is never accessed", fields = {"name"}},
Expand Down Expand Up @@ -152,7 +152,7 @@ local function detect_unused_local(chstate, var)
elseif #var.values == 1 then
local value = var.values[1]

if var.hint_unused then
if var.hint_unused and var.name ~= "_ENV" then
if value.used then
chstate:warn_var("214", var)
end
Expand Down

0 comments on commit 70dfac6

Please sign in to comment.