Skip to content

Clang should warn about chained comparisons like a > b > c #60256

@zmodem

Description

@zmodem

We had this = vs. == bug in Chromium recently:

int f(int);
int g(int a) {
  a == f(a) > 1 ? 1 : f(a);
  // stuff
}

In most cases, Clang would catch a = vs == bug with -Wunused-comparison, but in this case the unintentional comparison became an operand to the ternary operator, and no warning fired.

Perhaps there should be warning for the general case of "chained comparisons" that would catch the above as well as code like:

void h(int a, int b, int c) {
  if (a > b > c) {
    // stuff...
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions