From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5432 invoked by alias); 21 Jul 2018 12:16:39 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 5423 invoked by uid 89); 21 Jul 2018 12:16:38 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-0.0 required=5.0 tests=BAYES_20,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=purely, H*F:D*il, unneccessary, sk:wsuperf X-HELO: mail-wr1-f52.google.com Received: from mail-wr1-f52.google.com (HELO mail-wr1-f52.google.com) (209.85.221.52) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 21 Jul 2018 12:16:36 +0000 Received: by mail-wr1-f52.google.com with SMTP id h9-v6so13593658wro.3 for ; Sat, 21 Jul 2018 05:16:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=letai-org-il.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=La8jKqojIrm5MF3xku344nPp9Bo6+8FLx/Qd8EFXec4=; b=Mx8YVbdnmZ/9VpwfoxhWzkLQVuzFfIhPai16zo55ltgsbbOtQDij0AEVkqUrXEiybk 5pMmEJ9ItgR3ygtaSXzAmozcTbE+MeHx1QexHgl5uJNcQ4/rKKIap+pcn0mA9wEEFVNZ LTvrT2jYdAUib2nr+Yn+UWfMvpkof+sWKZwJXZPoxLx8ajbTKomGgJsceVbA9g+O84MH uNYO9hFyfkwcFnpurmcwVR0DHAL4+8SOb13HJS2v+laiXeZMVAub0a03gPXrv1SI6nyN WjgFFVwGxwRv+cDCGlKtdMb4TeTgmRHf1LLJGP2iEJn01ZFbEHMdsXBePBRXr3AokYaD YFtw== Return-Path: Received: from [192.168.75.211] (bzq-84-108-63-43.cablep.bezeqint.net. [84.108.63.43]) by smtp.gmail.com with ESMTPSA id 64-v6sm5104223wrj.50.2018.07.21.05.16.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 Jul 2018 05:16:33 -0700 (PDT) Subject: Re: Detecting superfluous "else" To: gcc@gcc.gnu.org References: <5B5050B2.3010701@mutluit.com> From: Daniel Letai Message-ID: <454abc7c-c9c7-f15c-f23e-0e686bc57852@letai.org.il> Date: Sat, 21 Jul 2018 13:18:00 -0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2018-07/txt/msg00326.txt.bz2 On 19/07/2018 18:56, Eric Gallager wrote: > On 7/19/18, U.Mutlu 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.