Skip to content

Commit 599fd4c

Browse files
committed
Fix trigger in NegatedConditionsInUnless
1 parent 0c99f4f commit 599fd4c

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lib/credo/check/refactor/negated_conditions_in_unless.ex

+7-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ defmodule Credo.Check.Refactor.NegatedConditionsInUnless do
3636
{nil, issues}
3737
end
3838

39-
defp traverse({:unless, _meta, arguments} = ast, issues, issue_meta)
40-
when is_list(arguments) do
41-
issue = issue_for_first_condition(List.first(arguments), issue_meta)
39+
defp traverse(
40+
{:unless, _meta, [{negator, meta, _arguments} | _]} = ast,
41+
issues,
42+
issue_meta
43+
)
44+
when negator in [:!, :not] do
45+
issue = issue_for(issue_meta, meta[:line], negator)
4246

4347
{ast, issues ++ List.wrap(issue)}
4448
end
@@ -47,12 +51,6 @@ defmodule Credo.Check.Refactor.NegatedConditionsInUnless do
4751
{ast, issues}
4852
end
4953

50-
defp issue_for_first_condition({:!, meta, _arguments}, issue_meta) do
51-
issue_for(issue_meta, meta[:line], "!")
52-
end
53-
54-
defp issue_for_first_condition(_, _), do: nil
55-
5654
defp issue_for(issue_meta, line_no, trigger) do
5755
format_issue(
5856
issue_meta,

test/credo/check/refactor/negated_conditions_in_unless_test.exs

+15
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,19 @@ defmodule Credo.Check.Refactor.NegatedConditionsInUnlessTest do
5959
|> run_check(@described_check)
6060
|> assert_issue()
6161
end
62+
63+
test "it should report a violation with not" do
64+
"""
65+
defmodule CredoSampleModule do
66+
def some_function(parameter1, parameter2) do
67+
unless not allowed? do
68+
something
69+
end
70+
end
71+
end
72+
"""
73+
|> to_source_file
74+
|> run_check(@described_check)
75+
|> assert_issue()
76+
end
6277
end

0 commit comments

Comments
 (0)