メインコンテンツ

AUTOSAR C++14 Rule M4-5-1

Expressions with type bool shall not be used as operands to built-in operators other than the assignment operator =, the logical operators &&, ||, !, the equality operators == and ! =, the unary & operator, and the conditional operator

説明

ルール定義

Expressions with type bool shall not be used as operands to built-in operators other than the assignment operator =, the logical operators &&, ||, !, the equality operators == and ! =, the unary & operator, and the conditional operator.

根拠

ルールに記載されている演算子以外の演算子では、bool 型のオペランドを使用しても意味のある結果が生成されません。bool 型のオペランドをこのような演算子で使用すると、プログラミング エラーを示す可能性があります。たとえば、論理演算子 || の使用を意図していたが、ビット演算子 | を代わりに使用した場合です。

Polyspace 実装

次の演算子以外の演算子のオペランドとして boolean 型が使用されている場合、Polyspace® はこのルールの違反を報告します。

  • =

  • &&

  • ||

  • !

  • ==

  • !=

  • 単項の & および ?

トラブルシューティング

ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

void boolOperations() {
    bool lhs = true;
    bool rhs = false;
    
    int res;
    
    if(lhs & rhs) {}  //Noncompliant
    if(lhs < rhs) {}  //Noncompliant
    if(~rhs) {}       //Noncompliant
    if(lhs ^ rhs) {}  //Noncompliant
    if(lhs == rhs) {} //Compliant
    if(!rhs) {}       //Compliant
    res = lhs? -1:1;  //Compliant  
}

この例では、bool 型のオペランドを ==!、および ? 演算子で使用した場合はルールに違反しません。

チェック情報

グループ: 標準変換
カテゴリ: Required、Automated

バージョン履歴

R2019a で導入

すべて展開する