elvis_core icon indicating copy to clipboard operation
elvis_core copied to clipboard

New rule: Avoid Comparison to Boolean

Open elbrujohalcon opened this issue 1 year ago • 0 comments

Avoid Comparison to Boolean

Brief Description

A rule that disallows comparison to booleans.

Should be on by default?

YES

Reasoning

This is because these expressions evaluate to true or false, so you could get the same result by using either the variable directly or negating the variable.

Examples

-module bad.

-export [my_fun/2].

my_fun(P1, P2) ->
    case do:something(P1) == false orelse do:something(P2) == false of
        true -> "There is a false";
        false -> "All true"
    end.
-module good.

-export [my_fun/2].

my_fun(P1, P2) ->
    case (not do:something(P1)) orelse (not do:something(P2)) of
        true -> "There is a false";
        false -> "All true"
    end.

%% … or even …

my_fun(P1, P2) ->
    case do:something(P1) andalso do:something(P2) of
        true -> "All true";
        false -> "There is a false"
    end.

Origin (#281)

Inspired by the ComparisonToBoolean rule from Dogma.

elbrujohalcon avatar Feb 28 '23 14:02 elbrujohalcon