Credo: how to exclude functions?

The checks PipeChainStart and ABC-Size allow a parameter excluded_functions.
This does not seem to work.

https://hexdocs.pm/credo/Credo.Check.Refactor.PipeChainStart.html

I have this function credo does not like:

def hello do
    Kernel.+(1, 1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
    |> Kernel.+(1)
  end

credo complains:

┃ [F] → Function is too complex (ABC size is 31, max is 30).
┃       lib/credo_check.ex:3:7 #(CredoCheck.hello)
┃ [F] → Pipe chain should start with a raw value.
┃       lib/credo_check.ex:5 #(CredoCheck.hello)

with this config:

{Credo.Check.Refactor.PipeChainStart, [excluded_functions: ["hello"]]},
{Credo.Check.Refactor.ABCSize, [excluded_functions: ["hello"]]},

I also tried

  • :hello
  • "CredoCheck.hello"
  • "hello/0"
  • ~r/hello/

Based on the tests for PipeChainStart, excluded_functions is a list of functions that are allowed to start a chain, not functions that are allowed to have a chain in them:

You likely want inline config comments on hello instead.

3 Likes

thanks for looking into this.

Makes sense for Pipe, but for ABC excluded_functions should exclude functions that are allowed to be complex, right?

Looks like Pipe has similar functionality, for similar reasons:

1 Like

Hi but how can I exclude a function?

For example I have

┃ [F] → Pipe chain should start with a raw value.

(row in Transaction)
    |> from(where: &1.id == ^transaction_id)
    |> Worms.Repo.one()

How can I exclude the from function to be warned by credo?

This may be an unwelcome tangent, but credo would not complain if you wrote this code in the more idiomatic way

Worms.Repo.get(Transaction, transaction_id)
1 Like