diff --git a/lib/credo/check/readability/nested_function_calls.ex b/lib/credo/check/readability/nested_function_calls.ex index b60dff1c6..63bc24a6c 100644 --- a/lib/credo/check/readability/nested_function_calls.ex +++ b/lib/credo/check/readability/nested_function_calls.ex @@ -252,6 +252,13 @@ defmodule Credo.Check.Readability.NestedFunctionCalls do ), do: true + # Kernel.to_string() in a string interpolation + def cannot_be_in_pipeline?( + {{:., _, [Kernel, :to_string]}, meta, _} = ast, + _excluded_functions, + _excluded_argument_types + ), do: meta[:from_interpolation] + # Elixir <= 1.8.0 # '__#{val}__' are compiled to String.to_charlist("__#{val}__") # we want to consider these charlists a valid pipe chain start diff --git a/test/credo/check/readability/nested_function_calls_test.exs b/test/credo/check/readability/nested_function_calls_test.exs index a6b09ad1f..865c941ca 100644 --- a/test/credo/check/readability/nested_function_calls_test.exs +++ b/test/credo/check/readability/nested_function_calls_test.exs @@ -190,4 +190,28 @@ defmodule Credo.Check.Readability.NestedFunctionCallsTest do |> run_check(NestedFunctionCalls) |> assert_issue() end + + test "it should NOT report any issues when Kernel.to_string() is called inside a string interpolation" do + ~S""" + defmodule FeatureFlag.Adapters.Test do + def get(flag, context, fallback) do + args = {flag, context, fallback} + case :ets.lookup(@table, flag) do + [] -> + raise "No stubs found for #{inspect(args)}" + + stubs -> + attempt_stubs(args, Enum.reverse(stubs)) + end + end + + defp attempt_stubs(args, []) do + raise "No stub found for args: #{inspect(args)}" + end + end + """ + |> to_source_file() + |> run_check(NestedFunctionCalls) + |> refute_issues() + end end