Skip to content

Generate warning on possible confusing use of operators with same precedence? #11372

@zevv

Description

@zevv

On #nim the following came up: consider evaluating the following expression:

not 3 == 4

The above evaluates left-to-right as (not 3) == 4 since not and == have same precedence, although the user actually hoped for not (3 == 4) to happen.

Easily explained by pointing to the manual, but still a potential pitfall.

Modern C compilers tend to notice this and advice the user to use parenthesis. For example, clang says:

ico@platdoos:/tmp$ clang -c t.c -Wall
t.c:4:9: warning: logical not is only applied to the left hand side of this comparison
      [-Wlogical-not-parentheses]
        return !3 == 4;
               ^  ~~
t.c:4:9: note: add parentheses after the '!' to evaluate the comparison first
        return !3 == 4;
               ^
                (     )
t.c:4:9: note: add parentheses around left hand side expression to silence this warning
        return !3 == 4;

Is this something Nim should signal and warn about as well?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions