Skip to content

Commit

Permalink
Merge pull request #1094 from msutkowski/ms/fix-negatedisnil
Browse files Browse the repository at this point in the history
fix: NegatedIsNil memory leak
  • Loading branch information
rrrene authored Nov 19, 2023
2 parents 6e72bec + eaea607 commit 3ef7c5a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/credo/check/refactor/negated_is_nil.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ defmodule Credo.Check.Refactor.NegatedIsNil do
end

defp traverse({:when, meta, [fun, {_, _, [first_op | second_op]}]} = ast, issues, issue_meta) do
{_, first_op_issues} = traverse({:when, meta, [fun, first_op]}, issues, issue_meta)
{_, second_op_issues} = traverse({:when, meta, [fun, second_op]}, issues, issue_meta)
{_, first_op_issues} = traverse({:when, meta, [fun, first_op]}, [], issue_meta)
{_, second_op_issues} = traverse({:when, meta, [fun, second_op]}, [], issue_meta)

{ast, first_op_issues ++ second_op_issues ++ issues}
end
Expand Down
28 changes: 28 additions & 0 deletions test/credo/check/refactor/negated_is_nil_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,32 @@ defmodule Credo.Check.Refactor.NegatedIsNilTest do
|> run_check(@described_check)
|> refute_issues()
end

test "it should report only one violation in a module with multiple functions when only one is problematic" do
"""
defmodule CredoSampleModule do
def hello(a) when not is_nil(a), do: a
def foo1(x) when is_integer(x), do: x
def foo2(x) when is_integer(x), do: x
end
"""
|> to_source_file()
|> run_check(@described_check)
|> assert_issue()
end

test "it should report two violations in a module with multiple functions when two are problematic" do
"""
defmodule CredoSampleModule do
def hello(a) when not is_nil(a), do: a
def foo1(x) when is_integer(x), do: x
def foo2(x) when is_integer(x), do: x
def hello2(a) when not is_nil(a), do: a
def foo3(x) when is_integer(x), do: x
end
"""
|> to_source_file()
|> run_check(@described_check)
|> assert_issues(fn issues -> assert length(issues) == 2 end)
end
end

0 comments on commit 3ef7c5a

Please sign in to comment.