Skip to content

Commit

Permalink
Fix false positive in UnusedFunctionReturnHelper
Browse files Browse the repository at this point in the history
Refs #1167
  • Loading branch information
rrrene committed Dec 13, 2024
1 parent 9c5e185 commit cf40ca2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/credo/check/warning/unused_function_return_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ defmodule Credo.Check.Warning.UnusedFunctionReturnHelper do
end
end

# anon_fun.()
defp verify_candidate(
{{:., _, [{fun_name, _, nil}]}, _, arguments} = ast,
:not_verified = acc,
candidate
)
when is_atom(fun_name) and is_list(arguments) do
# IO.inspect(ast, label: "anon_fun.() (#{Macro.to_string(candidate)} #{acc})")

if Credo.Code.contains_child?(arguments, candidate) do
{nil, :VERIFIED}
else
{ast, acc}
end
end

# module.my_fun()
defp verify_candidate(
{{:., _, [{module, _, []}, fun_name]}, _, arguments} = ast,
Expand Down Expand Up @@ -278,14 +294,6 @@ defmodule Credo.Check.Warning.UnusedFunctionReturnHelper do
end
end

{:., [line: 3, column: 31],
[
{{:., [line: 3, column: 22],
[{:__aliases__, [line: 3, column: 5], [:CredoSampleModule]}, :runner]},
[line: 3, column: 23], []},
:do_some_function
]}

# module.my_fun()
defp verify_candidate(
{{:., _, [{module_variable, _, nil}, fun_name]}, _, arguments} = ast,
Expand Down
15 changes: 15 additions & 0 deletions test/credo/check/warning/unused_enum_operation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ defmodule Credo.Check.Warning.UnusedEnumOperationTest do
|> refute_issues()
end

test "it should NOT report a violation when inside an anon function call" do
"""
defmodule CredoSampleModule do
def fun(anon_func) do
anon_func.(Enum.random(1..10))
:ok
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report a violation when inside of assignment" do
"""
defmodule CredoSampleModule do
Expand Down

0 comments on commit cf40ca2

Please sign in to comment.