public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96214] New: gcc warn unreached else {}
@ 2020-07-16  0:19 jg at jguk dot org
  2020-07-16  7:01 ` [Bug c++/96214] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: jg at jguk dot org @ 2020-07-16  0:19 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/96214] gcc warn unreached else {}
  2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
@ 2020-07-16  7:01 ` pinskia at gcc dot gnu.org
  2020-07-16 14:44 ` msebor at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-07-16  7:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |diagnostic

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/96214] gcc warn unreached else {}
  2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
  2020-07-16  7:01 ` [Bug c++/96214] " pinskia at gcc dot gnu.org
@ 2020-07-16 14:44 ` msebor at gcc dot gnu.org
  2020-07-16 20:23 ` jg at jguk dot org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-07-16 14:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-07-16
                 CC|                            |mpolacek at gcc dot gnu.org,
                   |                            |msebor at gcc dot gnu.org

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed.  Let me CC Marek who implemented -Wduplicated-branches for his
thoughts.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/96214] gcc warn unreached else {}
  2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
  2020-07-16  7:01 ` [Bug c++/96214] " pinskia at gcc dot gnu.org
  2020-07-16 14:44 ` msebor at gcc dot gnu.org
@ 2020-07-16 20:23 ` jg at jguk dot org
  2020-07-16 23:15 ` egallager at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jg at jguk dot org @ 2020-07-16 20:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214

--- Comment #2 from Jonny Grant <jg at jguk dot org> ---
Thank you. Saw there is -Wdangling-else already as well

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/96214] gcc warn unreached else {}
  2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
                   ` (2 preceding siblings ...)
  2020-07-16 20:23 ` jg at jguk dot org
@ 2020-07-16 23:15 ` egallager at gcc dot gnu.org
  2020-07-16 23:45 ` jg at jguk dot org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: egallager at gcc dot gnu.org @ 2020-07-16 23:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Jonny Grant from comment #2)
> Thank you. Saw there is -Wdangling-else already as well

-Wdangling-else is something different; it's more of a split-off from
-Wparentheses, i.e. missing braces

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/96214] gcc warn unreached else {}
  2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
                   ` (3 preceding siblings ...)
  2020-07-16 23:15 ` egallager at gcc dot gnu.org
@ 2020-07-16 23:45 ` jg at jguk dot org
  2020-08-01 12:17 ` jg at jguk dot org
  2022-03-02  2:46 ` egallager at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: jg at jguk dot org @ 2020-07-16 23:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214

--- Comment #4 from Jonny Grant <jg at jguk dot org> ---
I realised there could be many else {} that can't be executed, added one more
to the example.

int main(void)
{
     const int i = 1;
     if(1 == i)
     {
         return 1;
     }
     else if(1 != i)
     {
         return 2;
     }
     else if(2 != i)
     {
         return 2;
     }
     else
     {
         return 3;
     }
}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/96214] gcc warn unreached else {}
  2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
                   ` (4 preceding siblings ...)
  2020-07-16 23:45 ` jg at jguk dot org
@ 2020-08-01 12:17 ` jg at jguk dot org
  2022-03-02  2:46 ` egallager at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: jg at jguk dot org @ 2020-08-01 12:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214

--- Comment #5 from Jonny Grant <jg at jguk dot org> ---
I saw this similar one too:-

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options
-Wduplicated-cond

Warn about duplicated conditions in an if-else-if chain. For instance, warn for
the following code:

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/96214] gcc warn unreached else {}
  2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
                   ` (5 preceding siblings ...)
  2020-08-01 12:17 ` jg at jguk dot org
@ 2022-03-02  2:46 ` egallager at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-03-02  2:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96214

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu.org

--- Comment #6 from Eric Gallager <egallager at gcc dot gnu.org> ---
could also fit in with recent attempts to revive -Wunreachable-code

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-03-02  2:46 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-16  0:19 [Bug c++/96214] New: gcc warn unreached else {} jg at jguk dot org
2020-07-16  7:01 ` [Bug c++/96214] " pinskia at gcc dot gnu.org
2020-07-16 14:44 ` msebor at gcc dot gnu.org
2020-07-16 20:23 ` jg at jguk dot org
2020-07-16 23:15 ` egallager at gcc dot gnu.org
2020-07-16 23:45 ` jg at jguk dot org
2020-08-01 12:17 ` jg at jguk dot org
2022-03-02  2:46 ` egallager at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).