do { } while (0); inspection 关注
已回答
Hi,
today I tried to use CLion for an old tool - normally I use IDEA for Java coding.
I found an inspection for a do-while-loop that when fixing breaks the code, here is the source:
#include <printf.h>
extern int random();
#define STATUS_OK 0
#define STATUS_DONE (-1)
int do_something() {
return random();
}
int more_stuff() {
return random();
}
int finalize() {
return random();
}
int zmain(int argc, char *argv[]) {
printf("zzz argc=%d arg0=%s\n", argc, argv[0]);
int status, i;
do {
for (i = 0; i < 100; i++) {
status = do_something();
if (status > STATUS_OK)
goto termination;
else if (status == STATUS_DONE)
break; // This break exits the for, not the do-while.
}
for (i = 0; i < 100; i++) {
status = more_stuff();
if (status > STATUS_OK)
goto termination;
else if (status == STATUS_DONE)
break; // This break exits the for, not the do-while.
}
termination: // <= escape from processing and clean up
status = finalize();
} while (0);
return status;
}
Can / should we have an exception to the warning here ?
Sincerely, Dirk
请先登录再写评论。
Hi, Dirk,
We have a related issue in our tracker: https://youtrack.jetbrains.com/issue/CPP-3402. Feel free to comment or upvote, follow it to get updates.