Skip to content

Proposed linter for mixing logical operators without grouping #1566

Open
@MichaelChirico

Description

@MichaelChirico

Consider:

x | y & z
# or
x & y | z

I think this is a common pitfall for users not well-versed in logical operator precedence rules (myself included -- I had to look up the precedence once again while writing this post 😸).

At least "&& evaluates before ||" appears consistent across languages from what I've seen; this Q&A hints that this order was chosen "to reduce the number of parentheses" (would love a citation on that), while also highlighting an interesting language (Fortress) which declines to set arbitrary operator precedences and instead enforces parentheses in any uncommon combinations:

https://softwareengineering.stackexchange.com/q/391263/221381

The above would lint unless the user adds parentheses:

x | (y & z)
(x & y) | z

would be the logical equivalents. This would also apply to && / ||.

Related: in the Google suite, we have a linter for !(x == 2) --> x != 2. To me, the precedence of ! is clear enough not to need extra parentheses when combined with &&/||, so I didn't include it here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions