From 599fd4cdecbfb53695eb9859b1bfbba4b8038403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20F=C3=B6hring?= Date: Thu, 1 Feb 2024 05:30:19 +0100 Subject: [PATCH] Fix trigger in NegatedConditionsInUnless --- .../refactor/negated_conditions_in_unless.ex | 16 +++++++--------- .../negated_conditions_in_unless_test.exs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/credo/check/refactor/negated_conditions_in_unless.ex b/lib/credo/check/refactor/negated_conditions_in_unless.ex index 2c7b7d08c..7babac05a 100644 --- a/lib/credo/check/refactor/negated_conditions_in_unless.ex +++ b/lib/credo/check/refactor/negated_conditions_in_unless.ex @@ -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 @@ -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, diff --git a/test/credo/check/refactor/negated_conditions_in_unless_test.exs b/test/credo/check/refactor/negated_conditions_in_unless_test.exs index 4b21beaa4..51fa94e91 100644 --- a/test/credo/check/refactor/negated_conditions_in_unless_test.exs +++ b/test/credo/check/refactor/negated_conditions_in_unless_test.exs @@ -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