This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Detecting superfluous "else"
- From: Paul Koning <paulkoning at comcast dot net>
- To: GCC Mailing List <gcc at gcc dot gnu dot org>
- Date: Thu, 19 Jul 2018 09:01:01 -0400
- Subject: Re: Detecting superfluous "else"
- References: <5B5050B2.3010701@mutluit.com>
> On Jul 19, 2018, at 4:49 AM, 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.
I don't see any reason to warn about that code. It's perfectly valid, and in my view is clearer than the alternative. I've written both but I most often write the "else" variant for the reason that it expresses the semantics explicitly.
Warnings are appropriate for code that is known to be a source of bugs, or where there is a reasonable chance that the intent of the programmer doesn't match what was actually written. That's not the case here.
paul