inaka / elvis_core

The core of an Erlang linter
Other
61 stars 56 forks source link

New rule: No Match in Condition #289

Closed elbrujohalcon closed 1 year ago

elbrujohalcon commented 1 year ago

No Match in Condition

Brief Description

Pattern-matching should not be used in case statement conditions, like this:

case #{a := A} = do:something() of
    #{b := good} -> {a, really, nice, A};
    #{b := bad} -> {"not", a, good, A}
end

Should be on by default?

YES

Options

Reasoning

While the code as written above is valid, it's much harder to understand (particularly for large statements) than the one below.

Refactoring Proposal

case do:something() of
    #{a := A, b := good} -> {a, really, nice, A};
    #{a := A, b := bad} -> {"not", a, good, A}
end

Origin (#281)

Inspired by the Refactor.MatchInCondition rule from Credo