From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by sourceware.org (Postfix) with ESMTPS id DF907384402A for ; Thu, 9 Jul 2020 16:15:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DF907384402A Received: by mail-qk1-x72a.google.com with SMTP id r22so2282795qke.13 for ; Thu, 09 Jul 2020 09:15:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=eJstOZwZqmORow7Ok1PE9bit0Y0PPULw6SSbfzsA7Q8=; b=flaCHztgHZ+wk1UAPKhcKsrGWYmKRVrSBcW0+snHITVougupKN5je7wykSSWiZ0oLO ap2C6YuZCiSaxTrW4Luh2nKdYte37zs/Tssr844oYeLdm6I8dASR2SMWl3/U+LhzyT7L OgYMw9hzmCwBEt2RvpEBoLeV5eaKM4JmmVxw75jOVfbMIY19cSX5QDVNxoNTS9Txhx1b XhUaw2L2T2J7LmgQ8gVpyl6lcqfzWxtzu4+BFQIATrDorvux2GVq/svIN9DcM6/FwlEt SeazHDduc6F4VlIHbi2dRyiFQ7pVF2usRzOfIz2Ro+fHO7MPtX1oNpH+YO+xjB8Wh+iG 3IRg== X-Gm-Message-State: AOAM531k/ZFeAGslb6gFrC9jsKIUyQD8ynQ1VNOspXFoSRB/iApWDkvd ICefz5GZjPshTxjgZ4P5YDq0dUX2 X-Google-Smtp-Source: ABdhPJz8RmvwEFYGfsxFgWy8SrB+wFVzV3oByMGqNBf9FI7xQcgRM4N7sW9td9NrnPbfvK7Annq20g== X-Received: by 2002:a37:6894:: with SMTP id d142mr62805287qkc.440.1594311319383; Thu, 09 Jul 2020 09:15:19 -0700 (PDT) Received: from [192.168.0.41] (184-96-233-25.hlrn.qwest.net. [184.96.233.25]) by smtp.gmail.com with ESMTPSA id 140sm4154136qko.98.2020.07.09.09.15.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Jul 2020 09:15:18 -0700 (PDT) Subject: Re: gcc warn unreached else {} To: Jonny Grant , gcc-help References: <3a091703-4842-b738-bb38-fab01e787650@jguk.org> From: Martin Sebor Message-ID: <9a03ca1c-d13d-519b-8d94-78ac7260f63d@gmail.com> Date: Thu, 9 Jul 2020 10:15:17 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <3a091703-4842-b738-bb38-fab01e787650@jguk.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Jul 2020 16:15:22 -0000 On 7/9/20 3:45 AM, Jonny Grant wrote: > Hello! > > There is an example below, (my code is not like this example below). I'm reporting a possible issue, and asking if there is a way to detect it in code bases. If it's a real issue I can file a bug report on Bugzilla. > > Can gcc warn where code will not get to the 'return 3;' below? -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 > > Cheers, Jonny > > > int main(void) > { > const int i = 1; > if(1 == i) > { > return 1; > } > else if(1 != i) > { > return 2; > } > else > { > return 3; > } > } >