Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/credo/check/readability/nested_function_calls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ defmodule Credo.Check.Readability.NestedFunctionCalls do
{nil, acc}
end

# We don't look into guards
defp traverse({expr, _meta, _args}, acc, _issue) when expr in ~w[defguard defguardp when]a do
{nil, acc}
end

# We don't look into typespec attributes
defp traverse({:@, _, [{attr_name, _, _args}]}, acc, _issue)
when attr_name in ~w[callback macrocallback opaque spec type typep]a do
Expand Down
19 changes: 18 additions & 1 deletion test/credo/check/readability/nested_function_calls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@
# cases NOT raising issues
#

test "it should NOT report code with no nested function calls" do
test "it should NOT report code with nested guard calls" do

Check failure on line 10 in test/credo/check/readability/nested_function_calls_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test it should NOT report code with nested guard calls (Credo.Check.Readability.NestedFunctionCallsTest)
"""
defmodule CredoSampleModule do
# Note: for some reason test does not fail when using local functions
# that's why I've added "Kernel." prefix for each case
defguardp nested_guardp(data) when Kernel.is_atom(hd(hd(data)))
defguard nested_guard(data) when nested_guardp(data) or Kernel.is_binary(hd(hd(data)))

def nested_guard_def(data) when nested_guard_defp(data) or Kernel.is_binary(hd(hd(data)))
defp nested_guard_defp(data) when Kernel.is_atom(hd(hd(data)))
end
"""
|> to_source_file()
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report code with nested type calls" do
"""
defmodule CredoSampleModule do
@callback callback_name :: Keyword.t(Some.remote(some_arg))
Expand Down
Loading