public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "josephcsible at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/96468] New: Warn when an empty while loop could have been a do-while
Date: Tue, 04 Aug 2020 17:32:12 +0000	[thread overview]
Message-ID: <bug-96468-4@http.gcc.gnu.org/bugzilla/> (raw)

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).

             reply	other threads:[~2020-08-04 17:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-04 17:32 josephcsible at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-96468-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).