Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NegatedIsNil memory leak #1094

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading