warning: redundant continue statement at the end of loop statement
警告:ループ文の末尾にある冗長なcontinue文
[readability-redundant-control-flow]
■問題1:何と表示されるでしょうか?
//clang-tidy "-checks=*"
#include <stdio.h>
int main(void){
int i = 0;
do {
printf("%d\n",i);
i++;
continue;
} while(0);
}
何と表示されるか即答できなければ
do-while(0)を使うのは止めましょう。
●解答1
0
0とだけ表示されます。
■問題2:何と表示されるでしょうか?
#include <stdio.h>
int main(void){
int i = 0;
do {
printf("%d\n",i) ;
i++;
if(i < 10)
continue;
printf("ここに制御は来るか?\n") ;
} while(0);
}
即答できてもリーダブルコードを読んで
do-while(0)を使うのは止めましょう。
●解答2
0
0とだけ表示されます。
■早期リターン禁止は必須から推奨に降格
do-while(0)は制御が複雑になりやすいので
極力使用を避けましょう。
エラー処理の為に
do-while(0)構文を使用する
プロジェクトも多くありますが、
【早期リターン】や
【gotoクリーンアップ】が使えないか
検討してみて下さい。
某Mルールでは以前【早期リターン禁止】は「必要」でしたが、
2012年度版では「推奨」に降格しているので、
リーダブルコードでも紹介されている
【早期リターン】の導入を検討してみて下さい。
参考:
https://www.jpcert.or.jp/sc-rules/c-mem12-c.html
https://swest.toppers.jp/SWEST16/data/s3d_proceeding.pdf#page=49