Skip to content

Commit 044877b

Browse files
authored
Merge pull request #1179 from PaulOstazeski/mixed-double-negation
Extend double boolean negation to include '! not' and 'not !'
2 parents f7265ed + 434026c commit 044877b

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

lib/credo/check/refactor/double_boolean_negation.ex

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,16 @@ defmodule Credo.Check.Refactor.DoubleBooleanNegation do
4545
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
4646
end
4747

48-
# Checking for `!!`
49-
defp traverse({:!, meta, [{:!, _, ast}]}, issues, issue_meta) do
50-
issue =
51-
format_issue(
52-
issue_meta,
53-
message: "Double boolean negation found.",
54-
trigger: "!!",
55-
line_no: meta[:line]
56-
)
48+
@negation_operators [:!, :not]
49+
defguard both_negations(a, b) when a in @negation_operators and b in @negation_operators
5750

58-
{ast, [issue | issues]}
59-
end
60-
61-
# Checking for `not not`
62-
defp traverse({:not, meta, [{:not, _, ast}]}, issues, issue_meta) do
51+
defp traverse({operator_a, meta, [{operator_b, _, ast}]}, issues, issue_meta)
52+
when both_negations(operator_a, operator_b) do
6353
issue =
6454
format_issue(
6555
issue_meta,
6656
message: "Double boolean negation found.",
67-
trigger: "not not",
57+
trigger: format_trigger(operator_a, operator_b),
6858
line_no: meta[:line]
6959
)
7060

@@ -74,4 +64,7 @@ defmodule Credo.Check.Refactor.DoubleBooleanNegation do
7464
defp traverse(ast, issues, _issue_meta) do
7565
{ast, issues}
7666
end
67+
68+
defp format_trigger(:!, :!), do: "!!"
69+
defp format_trigger(a, b), do: Enum.join([a, b], " ")
7770
end

test/credo/check/refactor/double_boolean_negation_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,26 @@ defmodule Credo.Check.Refactor.DoubleBooleanNegationTest do
6262
assert issue.trigger == "not not"
6363
end)
6464
end
65+
66+
test "it should report mixed violation '! not'" do
67+
"""
68+
! not true
69+
"""
70+
|> to_source_file
71+
|> run_check(@described_check)
72+
|> assert_issue(fn issue ->
73+
assert issue.trigger == "! not"
74+
end)
75+
end
76+
77+
test "it should report mixed violation 'not !'" do
78+
"""
79+
not ! true
80+
"""
81+
|> to_source_file
82+
|> run_check(@described_check)
83+
|> assert_issue(fn issue ->
84+
assert issue.trigger == "not !"
85+
end)
86+
end
6587
end

0 commit comments

Comments
 (0)