Bug 111684 - enhancement: gcc doesn't warn about pointless tests
Summary: enhancement: gcc doesn't warn about pointless tests
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning cppcheck
  Show dependency treegraph
 
Reported: 2023-10-03 18:49 UTC by David Binderman
Modified: 2023-10-14 23:09 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Binderman 2023-10-03 18:49:59 UTC
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]
Comment 1 Andrew Pinski 2023-10-03 18:53:45 UTC
Note GCC does detect the pointless just does not warn about it.
Comment 2 David Binderman 2023-10-03 20:38:51 UTC
I've had a quick look at the linux-6.6-rc4 kernel code 
and found about a dozen examples of this problem, so there 
would be some customers for a solution.
Comment 3 David Binderman 2023-10-04 07:27:04 UTC
(In reply to David Binderman from comment #2)
> I've had a quick look at the linux-6.6-rc4 kernel code 
> and found about a dozen examples of this problem, so there 
> would be some customers for a solution.

A full look suggests there are about 30 examples. Surprising.