public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/96468] New: Warn when an empty while loop could have been a do-while
@ 2020-08-04 17:32 josephcsible at gmail dot com
  2020-08-05  7:30 ` [Bug c/96468] " rguenth at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: josephcsible at gmail dot com @ 2020-08-04 17:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96468
           Summary: Warn when an empty while loop could have been a
                    do-while
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: josephcsible at gmail dot com
  Target Milestone: ---

Consider this C code:

typedef int sig_atomic_t;
volatile sig_atomic_t signaled;
_Bool getX(int *);
void processX(int);
void f(void)
{
    {
        int x;
        if(getX(&x))
            processX(x);
    }
    while(!signaled);
    /* do some other stuff */
}

There's two possibilities for what the author meant for it to do:
1. Do getX (and maybe processX) once (in a block just to minimize the scope of
x), then busy-wait until signaled becomes true, and then do some other stuff.
This is what it actually does.
2. Keep doing getX (and maybe processX) in a loop until signaled becomes true,
and then do some other stuff. This isn't what it actually does, because the
author forgot the "do" keyword.

We currently emit no warnings for this code, even when compiled with "-Wall
-Wextra". I propose that when we see "while(condition);", we warn that a "do"
may be missing, and if it's not, that you should use "while(condition){}"
instead (except in cases where it's impossible to just be missing the "do"
keyword, like if the "while" is at the beginning of a block).

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

end of thread, other threads:[~2020-08-06 23:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-04 17:32 [Bug c/96468] New: Warn when an empty while loop could have been a do-while josephcsible at gmail dot com
2020-08-05  7:30 ` [Bug c/96468] " rguenth at gcc dot gnu.org
2020-08-05 14:48 ` msebor at gcc dot gnu.org
2020-08-05 15:03 ` josephcsible at gmail dot com
2020-08-05 15:45 ` [Bug tree-optimization/96468] " msebor at gcc dot gnu.org
2020-08-05 15:56 ` josephcsible at gmail dot com
2020-08-06  0:07 ` [Bug c/96468] " msebor at gcc dot gnu.org
2020-08-06 23:09 ` 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).