メインコンテンツ

AUTOSAR C++14 Rule A0-1-1

A project shall not contain instances of non-volatile variables being given values that are not subsequently used

説明

ルール定義

A project shall not contain instances of non-volatile variables being given values that are not subsequently used.

根拠

変数に値を代入したが、後でその変数値を使用しない場合は、代入でプログラミング エラーが発生する可能性があります。変数を使用する予定だったが使用しなかったか、他の変数を誤って使用した可能性があります。

Polyspace 実装

ルール チェッカーは、代入された値が後で使用されない場合、ファイル スコープが設定されたローカル変数と静的変数への値の代入に対して違反を報告します。(チェッカーは、extern 指定子のない const 修飾付きグローバル変数を、ファイル スコープを持つ静的変数と見なします。)

チェッカーは、以下の場合に違反を報告します。

  • 初期化された変数が使用されない場合の初期化。

  • 代入された値が使用されない場合の値の代入。

    たとえば、変数に値を代入した後、次に変数を読み取る前に別の値を代入した場合などです。この場合、チェッカーは最初の冗長な代入にフラグを設定します。唯一の例外は、値が "変数の初期化時" に代入され、後で上書きされる場合です。

ループの最後の反復で、1 つ前の反復の代入が余剰ではない場合、チェッカーは違反を報告しません。たとえば、関数 func() でのループの最後の反復の代入 prevIter = i は余剰ですが、1 つ前の反復の代入は余剰ではありません。

void doSomething(int);

void func() {
  int prevIter=-1, uBound=100;
  for(int i=0; i < uBound; i++) {
        doSomething(prevIter);
        prevIter = i;
  }
}

トラブルシューティング

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

すべて展開する

class largeInteger {
       largeInteger(int d1, int d2, int d3):
            lastFiveDigits(d1), nextFiveDigits(d2), firstFiveDigits(d3){}
       largeInteger& operator=(const largeInteger& other) {
            if(&other !=this) {
              firstFiveDigits = other.firstFiveDigits;
              nextFiveDigits = other.nextFiveDigits;
              lastFiveDigits = other.lastFiveDigits;
            }
            return *this;
       }
       void printIntegerValue();
    private:
        int firstFiveDigits;
        int nextFiveDigits;
        int lastFiveDigits;
};

bool compareValues(largeInteger, largeInteger);

void func() {
    largeInteger largeUnit{10000,0,0}; //Compliant
    largeInteger smallUnit{1,0,0}; //Compliant
    largeInteger tinyUnit{0,1,0}; //Noncompliant
    if(compareValues (largeUnit, smallUnit)) {
        //Perform some action   
    }
}

この例では、変数 tinyUnit は初期化されますが、一度も使用されません。

チェック情報

グループ: 言語に依存しない問題
カテゴリ: Required、Automated

バージョン履歴

R2020b で導入

すべて展開する