C and C++


LANG.STRUCT.NBC : ブール型ではない条件式

要旨

if, for, while, do-while もしくは三項演算子 ?: の制御式がブール式になっていません。

プロパティ

クラス名 Condition Is Not Boolean
日本語クラス名 ブール型ではない条件式
クラス分類 スタイル (style)
ニーモニック LANG.STRUCT.NBC
カテゴリー
MisraC2023 MisraC2023:14.4 The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type
Misra2012 Misra2012:14.4 The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type
Misra2004 Misra2004:13.2 Tests of a value against zero should be made explicit, unless the operand is effectively Boolean
AUTOSARC++14 AUTOSARC++14:A5-0-2 The condition of an if-statement and the condition of an iteration statement shall have type bool.
  AUTOSARC++14:A13-2-3 A relational operator shall return a boolean value.
MisraC++2008 MisraC++2008:5-0-13 The condition of an if-statement and the condition of an iteration-statement shall have type bool.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Condition Is Not Boolean"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

#include <stddef.h>

int lang_struct_nbc(int a, char *p){
    int i=0;
    int j;

    if (p != NULL) { i++;}                               /* 'p != NULL' is Boolean */
 
    if (!!p) {i++;}                                      /* '!!p' is Boolean */

    if (p) {i++;}                   /* 'Condition Is Not Boolean' warning issued here */

    while (i > 2){ i--;}                                 /* 'i > 5' is Boolean */

    while (i){ i--; }               /* 'Condition Is Not Boolean' warning issued here */

    i += a ? 3 : 1;                 /* 'Condition Is Not Boolean' warning issued here */

    for (j = 10; j > 0; j--) { i++;}                     /* 'j > 0' is Boolean */

    for (j = 10; j; j--) { i++;}    /* 'Condition Is Not Boolean' warning issued here */

    return i;
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。