Skip to main content

W0088 - Unnecessary function argument

Error​

-module(main).
-export([run/1]).

run(X) -> step(X, ctx).

step(a, _Ctx) -> 1;
%% ^^^^ warning: W0088: Argument 2 of step/2 is ignored by every clause and only passed by local calls, so it can be removed.
step(_, _) -> 2.

Explanation​

Every clause of step/2 ignores its second argument, and every caller is in the same module. The argument carries no information, so callers are forced to compute and pass a value for nothing, and readers have to work out that it is unused.

The diagnostic is only reported when removing the argument is a local change:

  • the function is not exported, the module does not use export_all or a parse transform and is not a parse transform itself, and the function is not under conditional compilation
  • every reference to the function is an unqualified call with all its arguments in its own module, or its -spec, and none of the calls goes through a macro, including ?FUNCTION_NAME
  • no attribute names the function, as step/2 or {step, 2} (for example -nifs, -compile({inline, ...}) or an -export in an inactive preprocessor branch)

References from other modules of the same application, its tests included, are searched for, since a test can export a function at runtime and refer to it as fun mod:step/2. Other applications are not searched.

Fix​

Remove the parameter from every clause, the argument from every call and the type from the spec:

run(X) -> step(X).

step(a) -> 1;
step(_) -> 2.

There is no quick-fix. Before removing the argument, check whether the callers' expressions for it have side effects, and whether removing them leaves a variable, local type or local function unused, so that the argument can be removed further up as well.