Skip to content

Commit

Permalink
Fix trigger in NegatedConditionsInUnless
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Feb 1, 2024
1 parent 0c99f4f commit 599fd4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/credo/check/refactor/negated_conditions_in_unless.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ defmodule Credo.Check.Refactor.NegatedConditionsInUnless do
{nil, issues}
end

defp traverse({:unless, _meta, arguments} = ast, issues, issue_meta)
when is_list(arguments) do
issue = issue_for_first_condition(List.first(arguments), issue_meta)
defp traverse(
{:unless, _meta, [{negator, meta, _arguments} | _]} = ast,
issues,
issue_meta
)
when negator in [:!, :not] do
issue = issue_for(issue_meta, meta[:line], negator)

{ast, issues ++ List.wrap(issue)}
end
Expand All @@ -47,12 +51,6 @@ defmodule Credo.Check.Refactor.NegatedConditionsInUnless do
{ast, issues}
end

defp issue_for_first_condition({:!, meta, _arguments}, issue_meta) do
issue_for(issue_meta, meta[:line], "!")
end

defp issue_for_first_condition(_, _), do: nil

defp issue_for(issue_meta, line_no, trigger) do
format_issue(
issue_meta,
Expand Down
15 changes: 15 additions & 0 deletions test/credo/check/refactor/negated_conditions_in_unless_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@ defmodule Credo.Check.Refactor.NegatedConditionsInUnlessTest do
|> run_check(@described_check)
|> assert_issue()
end

test "it should report a violation with not" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
unless not allowed? do
something
end
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue()
end
end

0 comments on commit 599fd4c

Please sign in to comment.