MISRA 2008:无法理解 5-0-13 规则
MISRA 2008: Cannot understand 5-0-13 rule
我无法理解 5-0-13 规则本身:
Rule 5-0-13: The condition of an if-statement and the condition of an iteration-statement shall have type bool.
Rationale: If an expression with type other than bool is used in the condition of an if-statement or iteration-statement, then its result will be implicitly converted to bool. The condition expression shall contain an explicit test (yielding a result of type bool) in order to clarify the intentions of the developer.
Exception: A condition of the form type-specifier-seq declarator is not required to have type bool.
This exception is introduced because alternative mechanisms for achieving the same effect are cumbersome and error-prone.
extern int32_t * fn ( );
extern int32_t fn2 ( );
extern bool fn3 ( );
while ( int32_t * p = fn ( ) ) // Compliant by exception
{
// Code
}
// The following is a cumbersome but compliant example
do
{
int32_t * p = fn ( );
if ( NULL == p )
{
break;
}
// Code...
}
while ( true ); // Compliant
while ( int32_t length = fn2 ( ) ) // Compliant by exception
{
// Code
}
while ( bool flag = fn3 ( ) ) // Compliant
{
// Code
}
if ( int32_t * p = fn ( ) ) // Compliant by exception
if ( int32_t length = fn2 ( ) ) // Compliant by exception
if ( bool flag = fn3 ( ) ) // Compliant
if ( u8 ) // Non-compliant
if ( u8 && ( bool_1 <= bool_2 ) ) // Non-compliant
for ( int32_t x = 10; x; --x ) // Non-compliant
...它与
有何不同
Rule 5-3-1: Each operand of the ! operator, the logical && or the logical || operators shall have type bool
Rationale: The use of operands with types other than bool with these operators is unlikely to be meaningful (or intended). This rule allows the detection of such uses, which often occur because the logical operators (&&, || and !) can be easily confused with the bitwise operators (&, | and ~).
if ( ( a < b ) && ( c < d ) ) // Compliant
if ( 1 && ( c < d ) ) // Non-compliant
if ( ( a < b ) && ( c + d ) ) // Non-compliant
if ( u8_a && ( c + d ) ) // Non-compliant
if ( !0 ) // Non-compliant –
// also breaks other rules
if ( !ptr ) // Non-compliant
if ( !false ) // Compliant with this rule,
// but breaks others
规则 5-3-1 也适用于 if
、while
和 for
之外,例如
extern void set_flag(bool);
set_flag( ( a < b ) && ( c < d ) ); // Compliant
set_flag( 1 && ( c < d ) ); // Non-compliant
set_flag( ( a < b ) && ( c + d ) ); // Non-compliant
set_flag( u8_a && ( c + d ) ); // Non-compliant
set_flag( !0 ) // Non-compliant –
// also breaks other rules
set_flag( !ptr ); // Non-compliant
set_flag( !false ); // Compliant with this rule,
// but breaks others
bool one = ( ( a < b ) && ( c < d ) ); // Compliant
bool two = ( 1 && ( c < d ) ); // Non-compliant
bool three = ( ( a < b ) && ( c + d ) ); // Non-compliant
bool four = ( u8_a && ( c + d ) ); // Non-compliant
bool five = ( !0 ); // Non-compliant –
// also breaks other rules
bool six = ( !ptr ); // Non-compliant
bool seven = ( !false ); // Compliant with this rule,
// but breaks others
规则 5-0-13 指出 'if' 条件内的任何语句都将被视为仅产生布尔值。
此规则的目的是处理 if 条件中缺少 = 的常见问题
即,处理程序员编写赋值语句而不是比较语句的场景。
这里举例说明:
想象一下,一个程序员在 'if' 条件中犯了一个输入错误,错过了所需的额外 '='。
即,
而不是做
if (x == 0) {
// Do something when value of x is zero
}
程序员写了:
if (x = 0) {
// Do something after assgining x to 0
}
以上错误会导致容易引入但很难识别的bug
规则 5-0-13 声明将执行赋值语句 (x = 0) 并根据条件评估语句的结果。
因此,按照这个规则,如果(x = 0)赋值成功,将执行(x = 0),并执行循环内的语句。
我无法理解 5-0-13 规则本身:
Rule 5-0-13: The condition of an if-statement and the condition of an iteration-statement shall have type bool.
Rationale: If an expression with type other than bool is used in the condition of an if-statement or iteration-statement, then its result will be implicitly converted to bool. The condition expression shall contain an explicit test (yielding a result of type bool) in order to clarify the intentions of the developer.
Exception: A condition of the form type-specifier-seq declarator is not required to have type bool. This exception is introduced because alternative mechanisms for achieving the same effect are cumbersome and error-prone.
extern int32_t * fn ( ); extern int32_t fn2 ( ); extern bool fn3 ( ); while ( int32_t * p = fn ( ) ) // Compliant by exception { // Code } // The following is a cumbersome but compliant example do { int32_t * p = fn ( ); if ( NULL == p ) { break; } // Code... } while ( true ); // Compliant while ( int32_t length = fn2 ( ) ) // Compliant by exception { // Code } while ( bool flag = fn3 ( ) ) // Compliant { // Code } if ( int32_t * p = fn ( ) ) // Compliant by exception if ( int32_t length = fn2 ( ) ) // Compliant by exception if ( bool flag = fn3 ( ) ) // Compliant if ( u8 ) // Non-compliant if ( u8 && ( bool_1 <= bool_2 ) ) // Non-compliant for ( int32_t x = 10; x; --x ) // Non-compliant
...它与
有何不同Rule 5-3-1: Each operand of the ! operator, the logical && or the logical || operators shall have type bool
Rationale: The use of operands with types other than bool with these operators is unlikely to be meaningful (or intended). This rule allows the detection of such uses, which often occur because the logical operators (&&, || and !) can be easily confused with the bitwise operators (&, | and ~).
if ( ( a < b ) && ( c < d ) ) // Compliant if ( 1 && ( c < d ) ) // Non-compliant if ( ( a < b ) && ( c + d ) ) // Non-compliant if ( u8_a && ( c + d ) ) // Non-compliant if ( !0 ) // Non-compliant – // also breaks other rules if ( !ptr ) // Non-compliant if ( !false ) // Compliant with this rule, // but breaks others
规则 5-3-1 也适用于 if
、while
和 for
之外,例如
extern void set_flag(bool);
set_flag( ( a < b ) && ( c < d ) ); // Compliant
set_flag( 1 && ( c < d ) ); // Non-compliant
set_flag( ( a < b ) && ( c + d ) ); // Non-compliant
set_flag( u8_a && ( c + d ) ); // Non-compliant
set_flag( !0 ) // Non-compliant –
// also breaks other rules
set_flag( !ptr ); // Non-compliant
set_flag( !false ); // Compliant with this rule,
// but breaks others
bool one = ( ( a < b ) && ( c < d ) ); // Compliant
bool two = ( 1 && ( c < d ) ); // Non-compliant
bool three = ( ( a < b ) && ( c + d ) ); // Non-compliant
bool four = ( u8_a && ( c + d ) ); // Non-compliant
bool five = ( !0 ); // Non-compliant –
// also breaks other rules
bool six = ( !ptr ); // Non-compliant
bool seven = ( !false ); // Compliant with this rule,
// but breaks others
规则 5-0-13 指出 'if' 条件内的任何语句都将被视为仅产生布尔值。
此规则的目的是处理 if 条件中缺少 = 的常见问题
即,处理程序员编写赋值语句而不是比较语句的场景。
这里举例说明:
想象一下,一个程序员在 'if' 条件中犯了一个输入错误,错过了所需的额外 '='。
即,
而不是做
if (x == 0) {
// Do something when value of x is zero
}
程序员写了:
if (x = 0) {
// Do something after assgining x to 0
}
以上错误会导致容易引入但很难识别的bug
规则 5-0-13 声明将执行赋值语句 (x = 0) 并根据条件评估语句的结果。
因此,按照这个规则,如果(x = 0)赋值成功,将执行(x = 0),并执行循环内的语句。