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

            Bug ID: 111684
           Summary: enhancement: gcc doesn't detect pointless tests
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com
  Target Milestone: ---

Given this C++ code:

extern void g( int);

void
f( int n)
{
    if (n <= 10)
        g( n);
    else if (n > 10)
        g( 10 - n);
}

gcc doesn't have much to say:

$ ~/gcc/results/bin/gcc -c -g -O2 -Wall -Wextra  oct3d.cc
$ 

This might be implemented as a simple extension of the Wduplicated-cond code.

Here is cppcheck finding the problem:

oct3d.cc:11:13: style: Expression is always true because 'else if' condition is
opposite to previous condition at line 9. [multiCondition]
 else if (n > 10)
            ^
oct3d.cc:9:8: note: first condition
 if (n <= 10)
       ^
oct3d.cc:11:13: note: else if condition is opposite to first condition
 else if (n > 10)
            ^
$ 

So these two problems in gcc trunk might be detected:

trunk.year/gcc/ada/sysdep.c:424:26: style: Expression is always true because
'else if' condition is opposite to previous condition at line 416.
[multiCondition]

trunk.year/libsanitizer/sanitizer_common/sanitizer_allocator_primary64.h:547:27:
style: Expression is always true because 'else if' condition is opposite to
previous condition at line 538. [multiCondition]

Reply via email to