rrrene* About

Credo: Introducing Lint attributes

I just released v0.3.3 of Credo, a static analysis tool for the Elixir language.

With the v0.3.0 release we finally introduced support for per-function exclusion of checks. This has been a community requested feature since the very beginning and I am happy to announce: it’s finally here.

I was opposed to “magic comments” like # credo: <check-name>:off, since they would clutter your code, be very tool-specific and not very “Elixirish”.

As it turns out the solution was quiet easy: Let’s use a module attribute like @doc, which also contains code-relevant information which is often written in comments in other environments. Building on this idea, Credo now let’s you use a module attribute named @lint to configure linting for specific functions.

@lint attributes let you exclude specific functions completely:

# Case 1: don't lint anything inside `my_fun`

@lint false
def my_fun do
end

In my mind, this reads just right. Given that you should use these function-specific exclusion rarely, it clearly communicates intention and does not clutter your code. But say you have a function where a specific check fails, but you know that this is an instance where you are fine with it. You can now mark that function to ignore all issues generated by that particular check (using the same syntax used in the config file):

# Case 2: don't run the TagTODO check inside `my_fun`

@lint {Credo.Check.Design.TagTODO, false}
def my_fun do
end

To exclude more than one check, e.g. a whole category of checks, you can use a Regex instead of the check module:

# Case 3: don't run any Refactor checks inside `my_fun`

@lint {~r/Refactor/, false}
def my_fun do
end

Finally, you can supply multiple tuples as a list and combine all of the above:

# Case 4: combine the above

@lint [{Credo.Check.Design.TagTODO, false}, {~r/Refactor/, false}]
def my_fun do
end

I hope you enjoy this release as much as I do. Thanks to all your feedback, we’re already at v0.3.3.

You can check out Credo on GitHub.