https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90052

            Bug ID: 90052
           Summary: No -Wtautological-compare warning for (x == 1 && x ==
                    2)
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

G++ doesn't warn about:

int f(int x)
{
  if (x == 1 && x == 2)
    return x;
  if (x != 1 || x != 2)
    return x;
  return 0;
}

Clang warns with -Wtautological-compare (but not -Wall -Wextra):

taut.cc:5:14: warning: overlapping comparisons always evaluate to true
[-Wtautological-overlap-compare]
  if (x != 1 || x != 2)
      ~~~~~~~^~~~~~~~~
taut.cc:3:14: warning: overlapping comparisons always evaluate to false
[-Wtautological-overlap-compare]
  if (x == 1 && x == 2)
      ~~~~~~~^~~~~~~~~
2 warnings generated.

It's easy to accidentally write && when you mean || or vice versa, so the
warning is useful.

Reply via email to