Skip to main content

W0042 - Equality check with unnecessary operator

Warning

fn(A, Same, Diff) ->
case A =:= foo of
%% ^^^^^^^^^💡 weak: Consider rewriting to an equality match.
true -> Same;
_ -> Diff
end.
fn(A, Same, Diff) ->
case A =/= foo of
%% ^^^^^^^^^💡 weak: Consider rewriting to an equality match.
false -> Same;
true -> Diff
end.
fn(A, Same, Diff) ->
if A =:= foo -> Same;
%% ^^^^^^^^^💡 weak: Consider rewriting to an equality match.
true -> Diff
end.

Explanation

The warning message indicates an unidiomatic way to express a structural equality check.

To fix this warning, replace the use of the =:= operator and subsequent check of the result with a direct use of an equality pattern:

fn(A, Same, Diff) ->
case A of
foo -> Same;
_ -> Diff
end.

Notes

We do not offer rewrites where the pattern would end up being a bare variable.

For example, the following code is not rewritten:

fn(A, B, Same, Diff) ->
case A =:= B of
true -> Same;
_ -> Diff
end.

Since the resulting pattern would be a bare variable, e.g. B in:

fn(A, B, Same, Diff) ->
case A of
B -> Same;
_ -> Diff
end.

Subsequent refactorings might then rename B, changing the pattern from an equality check to a new binding, with potentially subtle and confusing results. We want to avoid introducing dangerous patterns like this.