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

            Bug ID: 103067
           Summary: Tautological compare warning not appearing if the
                    self-comparison is on object members
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: crillion at tiscali dot it
  Target Milestone: ---

Hi,

   I've found that on gcc 11.2 (tested on windows 10, with msys2), and
compiling with :

g++ -std=c++20 -pedantic -Wall -Wextra -Werror=return-type -Wshadow=local
-Wempty-body -fdiagnostics-color -s -Os program.cpp -o program_gpp.exe 
-LC:/programs/msys64/mingw64/lib

(I have also tried adding -Wtautological-compare but with the same effect
described below).

 if I have a code with a struct person that in operator== erroneously compares
one of its members, of an object type, with itself as in the following example
:

// this is a struct for field members, here comparison is implemented correctly
struct object_member
{
        int a = 20;
        bool operator==(const m& rhs) const
        {
                return a == rhs.a;
        }
};

// this struct has the error in the operator==
struct person
{       
        bool operator==(const person& rhs) const
        {
            // comparing this instance member with itself!
            return age == age; // instead of age == rhs.age
        }

        object_member age;
};

int main()
{
        person p1, p2;
        if(p1 == p2) std::cout << "equal!\n";
}

I don't get the tautological compare warning. The behavior seems to occur only
with struct/class types (also with string and optional), but not with primitive
numeric types (i.e. : if age is an int, I get normally the tautological-compare
warning. It occurs also if the expression is in AND with other correct
comparisons. The causing part seems to be an operator== involving an object
member (struct, class) compared with itself.

In case of a single member I get instead the warning "rhs parameter not used",
but if I add to the struct and comparison another field (ex.: int a), comparing
it correctly, no warning shows at all:

(...)
operator==(const person& rhs)
{
    return a == rhs.a && age == age;
}

(...)
int a = 0;
object_member age;
(...)

Is this correct ? or should I get this important warning also if the member is
an object ?

Note : I see clang 11.0 does not issue the warning either.

Thanks,

    Marco

Reply via email to