メインコンテンツ

AUTOSAR C++14 Rule M5-3-1

Each operand of the ! operator, the logical && or the logical || operators shall have type bool.

説明

ルール定義

Each operand of the ! operator, the logical && or the logical || operators shall have type bool.

根拠

bool オペランドを論理演算子 !&&、または || に使用する場合、プログラミング エラーを示す可能性があります。

Polyspace 実装

チェッカーは、論理演算子 !&&、または || のオペランドが実質的に boolean ではない場合に、違反を報告します。使用できるオペランドを以下に示します。

  • bool 型の変数、またはオプション [有効な boolean 型] (-boolean-types) を使用して実質的な boolean として指定された変数。

  • == または < などの演算子を使用して 2 つの変数を比較し、boolean を返す式。

トラブルシューティング

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

すべて展開する

この例では、if 条件の論理演算子 && の両方のオペランドが実質的に boolean であり、これはルールに準拠しています。else if 条件の論理演算子 && の右オペランドには非 boolean 型 (int32_t) が含まれており、これはルールに違反しています。

#include <cstdint>

class Pair
{
    int32_t leftVal;
    int32_t rightVal;
public:
    bool operator==(const Pair& anotherPair) {
        return(((*this).leftVal == anotherPair.leftVal)
             &&((*this).rightVal == anotherPair.rightVal));
    }
};

Pair getAPair();
int32_t checkPair(const Pair*);

void main() {
    Pair aPair = getAPair();
    Pair anotherPair = getAPair();
    Pair athirdPair = getAPair();
    
    if((aPair == anotherPair) &&
       (aPair == athirdPair)) //Compliant
       {}
    else if((aPair == anotherPair) && //Noncompliant
            (checkPair(&athirdPair))) 
       {} 
}

チェック情報

グループ:
カテゴリ: Required、Automated

バージョン履歴

R2019a で導入