Skip to content

Commit 6bd912e

Browse files
committed
Fix trigger for NegatedConditionsWithElse
1 parent 599fd4c commit 6bd912e

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lib/credo/check/refactor/negated_conditions_with_else.ex

+12-16
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElse do
4949
end
5050

5151
defp traverse({:if, meta, arguments} = ast, issues, issue_meta) do
52-
if negated_condition?(arguments) && Credo.Code.Block.else_block?(ast) do
53-
new_issue = issue_for(issue_meta, meta[:line], "!")
52+
negator = negated_condition(arguments)
53+
54+
if negator && Credo.Code.Block.else_block?(ast) do
55+
new_issue = issue_for(issue_meta, meta[:line], negator)
5456

5557
{ast, issues ++ [new_issue]}
5658
else
@@ -62,27 +64,21 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElse do
6264
{ast, issues}
6365
end
6466

65-
defp negated_condition?(arguments) when is_list(arguments) do
66-
arguments |> List.first() |> negated_condition?()
67-
end
68-
69-
defp negated_condition?({:!, _meta, _arguments}) do
70-
true
71-
end
67+
defp negated_condition({:!, _, _}), do: "!"
7268

73-
defp negated_condition?({:not, _meta, _arguments}) do
74-
true
75-
end
69+
defp negated_condition({:not, _, _}), do: "not"
7670

7771
# parentheses around the condition wrap it in a __block__
78-
defp negated_condition?({:__block__, _meta, arguments}) do
79-
negated_condition?(arguments)
72+
defp negated_condition({:__block__, _, arguments}) do
73+
negated_condition(arguments)
8074
end
8175

82-
defp negated_condition?(_) do
83-
false
76+
defp negated_condition(arguments) when is_list(arguments) do
77+
arguments |> List.first() |> negated_condition()
8478
end
8579

80+
defp negated_condition(_), do: nil
81+
8682
defp issue_for(issue_meta, line_no, trigger) do
8783
format_issue(
8884
issue_meta,

test/credo/check/refactor/negated_conditions_with_else_test.exs

+8-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElseTest do
6060
"""
6161
|> to_source_file
6262
|> run_check(@described_check)
63-
|> assert_issue()
63+
|> assert_issue(fn issue ->
64+
assert issue.line_no == 3
65+
assert issue.trigger == "!"
66+
end)
6467
end
6568

6669
test "it should report a violation with not/2 as well" do
@@ -77,6 +80,9 @@ defmodule Credo.Check.Refactor.NegatedConditionsWithElseTest do
7780
"""
7881
|> to_source_file
7982
|> run_check(@described_check)
80-
|> assert_issue()
83+
|> assert_issue(fn issue ->
84+
assert issue.line_no == 3
85+
assert issue.trigger == "not"
86+
end)
8187
end
8288
end

0 commit comments

Comments
 (0)