https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214
Bug ID: 96214
Summary: gcc warn unreached else {}
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jg at jguk dot org
Target Milestone: ---
First posted here
https://gcc.gnu.org/pipermail/gcc-help/2020-July/139136.html
Can g++ warn where code will not get to the 'return 3;' below?
This isn't real code, this is just an example. Upon code reviews, I do come
across something like this, so a warning would be handy.
My example
int main(void)
{
const int i = 1;
if(1 == i)
{
return 1;
}
else if(1 != i)
{
return 2;
}
else
{
return 3;
}
}
Quoting Martin Sebor
-Wduplicated-branches detects a related problem. It's implemented
entirely in the front end and quite simplistic. It just looks at
the chain of expressions controlling the if statements and compares
them for equality. I think it could be enhanced with not too much
effort to detect a subset of this problem as well by considering
the operator as well as its operands.
Here's the function that does the checking and issues the warning:
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/c-family/c-warn.c#l2491
Martin