Skip to content

Commit cf40ca2

Browse files
committed
Fix false positive in UnusedFunctionReturnHelper
Refs #1167
1 parent 9c5e185 commit cf40ca2

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

lib/credo/check/warning/unused_function_return_helper.ex

+16-8
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ defmodule Credo.Check.Warning.UnusedFunctionReturnHelper do
230230
end
231231
end
232232

233+
# anon_fun.()
234+
defp verify_candidate(
235+
{{:., _, [{fun_name, _, nil}]}, _, arguments} = ast,
236+
:not_verified = acc,
237+
candidate
238+
)
239+
when is_atom(fun_name) and is_list(arguments) do
240+
# IO.inspect(ast, label: "anon_fun.() (#{Macro.to_string(candidate)} #{acc})")
241+
242+
if Credo.Code.contains_child?(arguments, candidate) do
243+
{nil, :VERIFIED}
244+
else
245+
{ast, acc}
246+
end
247+
end
248+
233249
# module.my_fun()
234250
defp verify_candidate(
235251
{{:., _, [{module, _, []}, fun_name]}, _, arguments} = ast,
@@ -278,14 +294,6 @@ defmodule Credo.Check.Warning.UnusedFunctionReturnHelper do
278294
end
279295
end
280296

281-
{:., [line: 3, column: 31],
282-
[
283-
{{:., [line: 3, column: 22],
284-
[{:__aliases__, [line: 3, column: 5], [:CredoSampleModule]}, :runner]},
285-
[line: 3, column: 23], []},
286-
:do_some_function
287-
]}
288-
289297
# module.my_fun()
290298
defp verify_candidate(
291299
{{:., _, [{module_variable, _, nil}, fun_name]}, _, arguments} = ast,

test/credo/check/warning/unused_enum_operation_test.exs

+15
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ defmodule Credo.Check.Warning.UnusedEnumOperationTest do
145145
|> refute_issues()
146146
end
147147

148+
test "it should NOT report a violation when inside an anon function call" do
149+
"""
150+
defmodule CredoSampleModule do
151+
def fun(anon_func) do
152+
anon_func.(Enum.random(1..10))
153+
154+
:ok
155+
end
156+
end
157+
"""
158+
|> to_source_file
159+
|> run_check(@described_check)
160+
|> refute_issues()
161+
end
162+
148163
test "it should NOT report a violation when inside of assignment" do
149164
"""
150165
defmodule CredoSampleModule do

0 commit comments

Comments
 (0)