C and C++


LANG.STRUCT.NBC : Condition Is Not Boolean

Summary

The controlling expression of an if, for, while, do-while, or ternary ?: statement is not a Boolean expression.

Properties

Class Name Condition Is Not Boolean
Significance style
Mnemonic LANG.STRUCT.NBC
Categories
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.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Condition Is Not Boolean"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

#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;
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.