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

            Bug ID: 70631
           Summary: Warn about redundant comparisons with -Wlogical-op
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vittorio.romeo at outlook dot com
  Target Milestone: ---

https://godbolt.org/g/Tt8hfe

int main()
{
    int x = 0;

    // warning: logical 'or' of collectively exhaustive tests is always true
[-Wlogical-op]
    if(x != 5 || x != 6) { } 

    // warning: logical 'and' of mutually exclusive tests is always false
[-Wlogical-op]
    if(x == 5 && x == 6) { } 

    // no warnings
    if(true || x == 10) { }
    if(false && x == 10) { }

    // no warnings
    if(x == 5 || x != 6) { } // x==5 is redundant
    if(x == 5 && x != 6) { } // x!=6 is redundant
}

I think gcc should warn about the redundant comparisons with -Wlogical-op.

More information:
https://www.reddit.com/r/cpp/comments/4e9kf1/logical_expressions_in_cc_mistakes_made_by/

Reply via email to