xref and static checks
A type check proves what a function returns, and a style check proves how the source reads. Two more checks catch the mistakes in between: the compiler warnings a project turns into errors, and xref, which proves that every call names a function that exists. This lesson runs both, and puts them in the loop a CI job runs on every change. The Erlang toolchain provides all of them, so a project needs no outside service to check itself.
The loop
A project is checked by four tools, in this order. The compiler builds and reports a warning. rebar3 eunit runs the unit tests. rebar3 xref compares every call with the functions the project and its applications export. Dialyzer compares the values a function can return with its spec and with its callers. Elvis checks the style of the source. Each one is a lesson of this chapter; this one joins them.
Warnings as errors
The compiler already finds a variable that is assigned and never used, and a project that treats warnings as errors stops the build on it. The option is warnings_as_errors in erl_opts, and the command line has the same switch as -Werror. The function below is a fragment of a module, and it binds a value it never reads:
unused.erl
double(X) -> Y = X * 2, X.
erlc +debug_info -Werror unused.erl
compile: warnings being treated as errors unused.erl:5:5: variable 'Y' is unused % 5| Y = X * 2, % | ^ unused.erl:5:11: the result of evaluating operator '*'/2 is ignored (suppress the warning by assigning the expression to the _ variable) % 5| Y = X * 2, % | ^
The command is marked as not run on this page because the fragment has no module header; the output is from a real run on the full module, where the function starts on line 4. The first line says why the build stopped, and each warning names the file, the line and the column, then repeats the source line with a caret under the exact token. The fix is to remove the binding or to write it as a value the function returns; this module returns the result of the multiplication:
fixed.erl
-module(fixed). -export([double/1]). double(X) -> X * 2.
erlc +debug_info -Werror fixed.erl
A clean compile prints nothing and exits 0, which is the same quiet contract as Elvis. A project writes the option once in rebar.config, so every module in the build gets it without a command line.
Cross references
xref reads the compiled project and builds a table of every call and every export. It then reports a call whose target is missing, an exported function that nothing calls, and a few more relations. A fresh project is clean:
rebar3 xref
===> Verifying dependencies... ===> Analyzing applications... ===> Compiling xapp ===> Running cross reference analysis...
The command is not run on this page because it needs the project that the rebar3 lesson creates, and its output is stable. A real finding has this shape, from a run where one module calls a function that does not exist:
rebar3 xref
src/xapp_extra.erl:4: Warning: xapp_extra:run/0 is unused export (Xref) src/xapp_extra.erl:4: Warning: xapp_extra:run/0 calls undefined function missing_module:work/0 (Xref)
Two messages come from one line: the export is unused because nothing calls it, and the call names a module and function that no application in the release provides. The first field is the source file, the second its line, and the last word in parentheses names the tool. A call into an application that is missing from applications in the .app.src file is reported the same way, which is why the layout lesson insists on that list.
The CI job
The value of the checks is in running all of them on every change, not in the order a developer remembers. A job runs the four tools and stops at the first failure, which is a shell one-liner:
rebar3 compile && rebar3 eunit && rebar3 xref && rebar3 dialyzer && elvis rock
One trap of this version of rebar3 is worth knowing: rebar3 xref
prints its warnings and still exits 0. A job that only reads the status sees a green
build with a warning in the log, so the job must either add
{xref_checks, [undefined_function_calls]} to its config and
check the output, or read the lines. The same is true of a warning the compiler prints
without warnings_as_errors.
The loop is cheap because each tool reads the same beams. A change compiles once, and
the tests, the cross reference and Dialyzer reuse the output; only Elvis parses the
source again. The performance lesson
shows how to measure a build before making it longer.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
warnings_as_errorsdo?Answer
Stops the build when the compiler warns
The option makes every compiler warning a failed build, so a warning cannot live in the tree.
-
What does
rebar3 xrefcompare?Answer
Every call with the functions that exist
The cross reference builds a table of calls and exports and reports the calls with no target.
-
What does
unused.erlreport under-Werror?Answer
variable 'Y' is unusedYis bound and never read, so the compiler warns and the build stops. -
Why must a CI job read the output of
rebar3 xref?Answer
It exits 0 even when it reports a warning
The task reports its warnings and still exits 0, so the status alone is not enough.
-
What does this module print?
-module(q1). -export([main/0, double/1]). double(X) -> X * 2. main() -> io:format("~p~n", [double(21)]).Answer
42The function doubles 21 and the program prints 42.
-
What does this module print?
-module(q2). -export([main/0, count/1]). count([]) -> 0; count([_Head | Tail]) -> 1 + count(Tail). main() -> io:format("~p~n", [count([a, b])]).Answer
2The recursive clauses count one per item, so a list of two gives 2.
-
What does this module print?
-module(q3). -export([main/0, label/1]). label(N) when N > 0 -> positive; label(_N) -> other. main() -> io:format("~p~n", [label(0)]).Answer
otherThe guard fails for 0, so the second clause matches and returns
other. -
What does this module print?
-module(q4). -export([main/0, first/1]). first([Head | _Tail]) -> Head. main() -> io:format("~p~n", [first([alpha, beta, gamma])]).Answer
alphaThe pattern binds the head and ignores the tail, so the program prints
a. -
Which tool reports a call to a function that no module exports?
Answer
xrefThe cross reference table has no target for the call, and xref names the line.
-
Where does a project set the compiler option that turns warnings into errors?
Answer
In
rebar.config, undererl_optserl_optsholds the compiler options of the build, andwarnings_as_errorsis one of them.
The examples on this page were compiled with Erlang/OTP 27 (erts-15.2.7.14), and the xref and build lines were run with rebar3 3.27.3 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev