Skip to main content

W0060 - Bound Variable in LHS

Error

handle_request(Message) ->
Message = next_action().
%% ^^^^^^^ 💡 warning: W0060: Match on a bound variable

Explanation

This diagnostic flags cases where a variable that is already bound appears on the left-hand side (LHS) of a match expression. This can be problematic if the binding is not intentional and can lead to subtle bugs.

Consider the following code snippet:

foo() ->
AA = foo(),
AA = bar().

The pattern on line 3 will only match if and only if the result of the call to bar/0 is the same as the call to foo/0. This behaviour could be intentional or not. If not, it can easily lead to bugs.

An alternative, more explicit, way to express that behaviour - when intentional - could be:

foo() ->
AA = foo(),
BB = bar(),
AA = BB.

Or using an assertion:

foo() ->
AA = foo(),
?assertEqual(AA, bar()).

Semantic highlighting

Note that we also have semantic highlighting of the more general case, where a bound variable appears in any pattern.