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: negated_is_nil when nested in guards #1096

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/credo/check/refactor/negated_is_nil.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule Credo.Check.Refactor.NegatedIsNil do
{_, 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}
{ast, (first_op_issues ++ (second_op_issues -- issues))}
end

defp traverse(ast, issues, _), do: {ast, issues}
Expand Down
31 changes: 31 additions & 0 deletions test/credo/check/refactor/negated_is_nil_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ defmodule Credo.Check.Refactor.NegatedIsNilTest do
|> assert_issue()
end

test "it should report multiple violations with guards - `when not is_nil and guard not is nil`" do
"""
defmodule CredoSampleModule do
defguard my_guard(value) when not is_nil(value)

def some_function(parameter1, parameter2, parameter3) when not is_nil(parameter1) and my_guard(parameter2) and my_guard(parameter3) do
something
end
end
"""
|> to_source_file()
|> run_check(@described_check)
|> assert_issues(fn issues ->
assert Enum.count(issues) == 2
end)
end

test "it should report one violation - `when not is_nil and not is nil`" do
# This is due to the way the check works, and is probably fine since it will fail if the first one is removed
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2, parameter3) when not is_nil(parameter2) and not is_nil(parameter3) and is_nil(parameter3) do
something
end
end
"""
|> to_source_file()
|> run_check(@described_check)
|> assert_issue()
end

test "it should report a violation - `when not is_nil is part of a multi clause guard`" do
"""
defmodule CredoSampleModule do
Expand Down