Skip to content

Manual && or || #12434

@TomFryersMidsummer

Description

@TomFryersMidsummer

What it does

Suggests replacing if-else expressions where one branch is a boolean literal with && / || operators.

Advantage

  • Boolean expressions are easier to read and understand and are more concise.

Drawbacks

  • There could be cases where the underlying reasoning is made more obvious by the explicit if-else.
  • The control flow is clearer. && and || obscure branching.

Example

if a {
    b
} else {
    false
}

a && b

if a {
    true
} else {
    b
}

a || b

The other two combinations are less clear wins: I'd generally prefer !a || b to if a {b} else {true}, but exceptions seem more likely here. Of course, something like if !a {b} else {true} is much better as a || b.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions