Skip to content

Commit

Permalink
Merge pull request #94 from appgurueu/fix-582-subexps
Browse files Browse the repository at this point in the history
Fix warning 582 (error prone negation) not applying to subexpressions
  • Loading branch information
arichard4 committed Jun 5, 2023
2 parents 606d961 + 14fbbc0 commit c697e32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
32 changes: 27 additions & 5 deletions spec/check_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,34 @@ end
]])
end)

it("warns about error-prone negations", function()
assert.same({
{code = "582", line = 1, column = 13, end_column = 21}
}, check[[
if not 5 > 5 then return end
describe("error-prone negations", function()
it("as sole conditions", function()
assert.same({
{code = "582", line = 1, column = 16, end_column = 24}
}, check[[
if not 5 > 5 then return end
]])
end)

it("as subexpressions", function()
assert.same({
{code = "582", line = 1, column = 25, end_column = 34}
}, check[[
if not 5 or not 5 == 5 then return end
]])
end)

it("doesn't warn if properly parenthesized", function()
assert.same({}, check[[
if (not 5) == 5 then return end
]])
end)

it("doesn't warn for a literal 'not'", function()
assert.same({}, check[[
if 5 == "not" then return end
]])
end)
end)

it("doesn't warn on similarly named variables", function()
Expand Down
13 changes: 6 additions & 7 deletions src/luacheck/stages/unwrap_parens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ local function handle_nodes(chstate, nodes, list_start)
handle_nodes(chstate, node[1])
handle_nodes(chstate, node[2], 1)
else
-- warn that not x == y means (not x) == y
if tag ~= "Paren"
and node[1]
and node[1].tag == "Op"
and relational_operators[node[1][1]]
and node[1][2][1] == "not"
-- warn that not x <relop> y means (not x) <relop> y
if tag == "Op"
and relational_operators[node[1]]
and node[2].tag == "Op"
and node[2][1] == "not"
then
chstate:warn_range("582", node[1])
chstate:warn_range("582", node)
end

handle_nodes(chstate, node)
Expand Down

0 comments on commit c697e32

Please sign in to comment.