This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/81851] New: missing -Wduplicated-branches on if and return statements with no else
- From: "msebor at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 15 Aug 2017 20:52:15 +0000
- Subject: [Bug c/81851] New: missing -Wduplicated-branches on if and return statements with no else
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81851
Bug ID: 81851
Summary: missing -Wduplicated-branches on if and return
statements with no else
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
The -Wduplicated-branches warning triggers when the true statement and the
false statement in an if statement are the same, including when both are a
return statement. But the warning doesn't trigger for the equivalent code with
the else statement, even though it's very common to leave out the else part
when the block controlled by the if statement ends in a return.
This could be viewed more as an enhancement request than a bug since the
documentation doesn't suggest a warning in the latter case should be expected,
but since such code is pervasive I'd be more inclined to see it as a bug.
Either way, handling this case would make the warning obviously even more
useful than it already is.
$ cat b.c && gcc -O2 -S -Wduplicated-branches b.c
int f (int i)
{
if (i == 0) // -Wduplicated-branches (good)
return 0;
else
return 0;
}
int g (int i)
{
if (i == 0) // no warning
return 0;
return 0;
}
b.c: In function ‘f’:
b.c:3:6: warning: this condition has identical branches [-Wduplicated-branches]
if (i == 0) // -Wduplicated-branches (good)
^