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

            Bug ID: 101368
           Summary: -Wlogical-op and string comparison
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arnaud02 at users dot sourceforge.net
  Target Milestone: ---

Considering
#include <string_view>
bool f(int num) {
    return num != 2 || num != 3;
}

bool g(std::string_view s) {
    using namespace std::literals;
    return s != "AA"sv || s != "BB"sv;
}

"g++ -std=c++17 -Wlogical-op -O2" emits:
<source>: In function 'bool f(int)':
<source>:3:21: warning: logical 'or' of collectively exhaustive tests is always
true [-Wlogical-op]
    3 |     return num != 2 || num != 3;
      |            ~~~~~~~~~^~~~~~~~~~~

g++ spots the defect in "f". It would be incredibly useful if the similar
defect was reported in "g".

In this case, the gcc x64 backend emits identical code for f and g:
f(int):
        mov     eax, 1
        ret
g(std::basic_string_view<char, std::char_traits<char> >):
        mov     eax, 1
        ret

Reply via email to