diff --git a/lib/credo/check/refactor/negated_conditions_with_else.ex b/lib/credo/check/refactor/negated_conditions_with_else.ex index 82b538b74..282fd72ee 100644 --- a/lib/credo/check/refactor/negated_conditions_with_else.ex +++ b/lib/credo/check/refactor/negated_conditions_with_else.ex @@ -49,8 +49,10 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElse do end defp traverse({:if, meta, arguments} = ast, issues, issue_meta) do - if negated_condition?(arguments) && Credo.Code.Block.else_block?(ast) do - new_issue = issue_for(issue_meta, meta[:line], "!") + negator = negated_condition(arguments) + + if negator && Credo.Code.Block.else_block?(ast) do + new_issue = issue_for(issue_meta, meta[:line], negator) {ast, issues ++ [new_issue]} else @@ -62,27 +64,21 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElse do {ast, issues} end - defp negated_condition?(arguments) when is_list(arguments) do - arguments |> List.first() |> negated_condition?() - end - - defp negated_condition?({:!, _meta, _arguments}) do - true - end + defp negated_condition({:!, _, _}), do: "!" - defp negated_condition?({:not, _meta, _arguments}) do - true - end + defp negated_condition({:not, _, _}), do: "not" # parentheses around the condition wrap it in a __block__ - defp negated_condition?({:__block__, _meta, arguments}) do - negated_condition?(arguments) + defp negated_condition({:__block__, _, arguments}) do + negated_condition(arguments) end - defp negated_condition?(_) do - false + defp negated_condition(arguments) when is_list(arguments) do + arguments |> List.first() |> negated_condition() end + defp negated_condition(_), do: nil + defp issue_for(issue_meta, line_no, trigger) do format_issue( issue_meta, diff --git a/test/credo/check/refactor/negated_conditions_with_else_test.exs b/test/credo/check/refactor/negated_conditions_with_else_test.exs index 0a7d9e3c2..1cbbd1715 100644 --- a/test/credo/check/refactor/negated_conditions_with_else_test.exs +++ b/test/credo/check/refactor/negated_conditions_with_else_test.exs @@ -60,7 +60,10 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElseTest do """ |> to_source_file |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 3 + assert issue.trigger == "!" + end) end test "it should report a violation with not/2 as well" do @@ -77,6 +80,9 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElseTest do """ |> to_source_file |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 3 + assert issue.trigger == "not" + end) end end