This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Detecting superfluous "else"




On 19/07/2018 18:56, Eric Gallager wrote:
On 7/19/18, U.Mutlu <um@mutluit.com> wrote:
Hi,
it makes me 'crazy' when I see such if-else constructs:
    if (x)
      return 7;
    else
      return 4;

(Of course in this case one better would use the shorthand "return x ? 7 :
4;", but that's not the issue here)

The 'else' is obviously superfluous/redundant, ie. unneeded at all:
    if (x)
      return 7;
    return 4;

Is it possible to warn about such unneccessary occurances of "else"?
If not, then I suggest to add a new warning code -Wsuperfluous-else or
-Wredundant-else or so.

Thx

Semi-related: bug 81851: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81851
(my disagreement with that request is similar to my disagreement with
this request)
Your example might be worth a warning, to allow the developer to refactor

int g (int i)
{
   if (i == 0)   // no warning
     return 0;
#if SOME_OTHER_PLATFORM
   if (i == 2)
     return 1;
#endif
   return 0;
}
to
int g (int i)
{
#if SOME_OTHER_PLATFORM
   if (i == 2)
     return 1;
#endif
   return 0;
}
It is purely stylistic, but if one is worried about dup branch at all, than it's reasonable to assume one would want this warning too.
It would require distinguishing between multiple possible branches by the preprocessor, rather than the compiler. No idea who emits the dup branch warning.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]