Expression Can Be Simplified

View Source

Expressions that are mathematically or logically redundant (e.g. identity or absorbing constants) should be simplified.

Avoid/prefer

Expression to avoidPreferLabel (for simplifications)
[] ++ XXlist_append_left_empty
X ++ []Xlist_append_right_empty
[] -- X[]list_subtract_from_empty
X -- []Xlist_subtract_empty
X + 0Xadd_zero_right
X - 0Xsubtract_zero
0 - X-Xsubtract_from_zero
X * 1Xmultiply_by_one_right
1 * XXmultiply_by_one_left
X div 1Xdiv_by_one
X rem 10rem_by_one
true andalso XXandalso_true
false orelse XXorelse_false
not truefalsenot_true
not falsetruenot_false
X band -1Xband_neg_one
X bor 0Xbor_zero
X bxor 0Xbxor_zero

Rationale

Operations with identity or absorbing constants (e.g. X + 0, [] ++ X, true andalso X) have well-defined outcomes that can be written more clearly by using the simplified form. Simplifying improves readability and avoids redundant code. This rule is separate from No Operator With Same Values, which targets expressions like X op X where both operands are the same.

Options

  • simplifications :: [atom()]
    • List of simplification patterns to check. Omit a label to disable that pattern.
    • Default: all of the following
    • Labels: list_append_left_empty, list_append_right_empty, list_subtract_from_empty, list_subtract_empty, add_zero_right, subtract_zero, subtract_from_zero, multiply_by_one_right, multiply_by_one_left, div_by_one, rem_by_one, andalso_true, orelse_false, not_true, not_false, band_neg_one, bor_zero, bxor_zero

Example configuration

{elvis_style, expression_can_be_simplified, #{
    simplifications =>
        [
            list_append_left_empty,
            list_append_right_empty,
            list_subtract_from_empty,
            list_subtract_empty,
            add_zero_right,
            subtract_zero,
            subtract_from_zero,
            multiply_by_one_right,
            multiply_by_one_left,
            div_by_one,
            rem_by_one,
            andalso_true,
            orelse_false,
            not_true,
            not_false,
            band_neg_one,
            bor_zero,
            bxor_zero
        ]
}}

To enable only a subset (e.g. list and arithmetic simplifications):

{elvis_style, expression_can_be_simplified, #{
    simplifications =>
        [
            list_append_left_empty,
            list_append_right_empty,
            list_subtract_empty,
            add_zero_right,
            subtract_zero,
            multiply_by_one_right,
            multiply_by_one_left
        ]
}}