Credo: How Config Comments work
As of v1.4
of Elixir the compiler will warn you when you declare an unused module attribute. Unfortunately, this is also the case for @lint
attributes.
# BEFORE: using @lint attribute to disable Credo
# for an entire function
@lint false
defp do_stuff() do
# ...
end
With the release of Credo v0.8
, there is a new comment-based syntax. This will get rid of the compiler warnings. Also, you will be able to disable specific lines or entire files for all or only specific checks with this syntax.
# AFTER: use config comment to disable Credo
# for the exact issue you want to ignore
defp do_stuff() do
# credo:disable-for-next-line
IO.inspect {:we_want_this_inspect_in_production!}
end
There are four config comments available:
credo:disable-for-this-file
- disables Credo for the entire file, no matter where the comment is put (so you can put it somewhere it doesn’t bother you too much)credo:disable-for-next-line
- disables Credo for the next linecredo:disable-for-previous-line
- disables Credo for the previous linecredo:disable-for-lines:<count>
- disables Credo for the given number of lines (can be negative to disable previous lines)
Disabling specific checks
Each of these can also take the name of the check you want to disable:
defp do_stuff() do
# credo:disable-for-next-line Credo.Check.Warning.IoInspect
IO.inspect {:we_want_this_inspect_in_production!}
end
Lastly, you can put a regular expression (/.+/
) instead of a check name to disable multiple checks (or if you do not want to type out the checks):
defp do_stuff() do
# credo:disable-for-next-line /\.Warning\./
IO.inspect {:we_want_this_inspect_in_production!}
end
Some Batteries NOT included …
There are two features which are not supported with this new syntax:
- Let you put config comment at the end of a code line
- Let you disable Credo at a specific line and re-enable it later
The reason for the first is that comments should be on their own line (except for things like defstruct
, see Credo’s Style Guide).
The reason for the second is that people should not get used to regularly disabling Credo for large parts of their code (with the obvious exception of excluding entire files like third-party code).
Credo has always been good at rethinking seemingly tried-and-true mechanics. But: It can only get better at this through feedback. We have to reflect both the raised issues and the status quo.
While disabling Credo for a specific range of your code might seem useful, we have to ask ourselves if that is really what we want. Instead of giving people the means to silence these issues, we have to encourage them to “complain” and file a bug report with the Credo project on GitHub.
One more thing …
I would really like to thank all the contributors who make these releases possibly. Credo is very much a community effort and I applaud each and every one of you for being an active part of the Elixir community.