メインコンテンツ

MISRA C:2023 Rule 13.3

A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator

R2024a 以降

説明

ルール定義

A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator 1 .

根拠

同一コード行で以下が発生する場合、ルールに違反します。

  • インクリメント演算子またはデクリメント演算子が変数に作用する。

  • 別の読み取りまたは書き込み操作が変数に対して実行される。

たとえば、行 y=x++ はこのルールに違反しています。++ 演算子および = 演算子の両方が x に作用しています。

演算子の優先順位ルールは評価順序を決定しますが、++ と別の演算子を同じ行に置くとコードの可読性が低下します。

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

int input(void);
int choice(void);
int operation(int, int);

int func() {
    int x = input(), y = input(), res;
    int ch = choice();
    if (choice == -1)
        return(x++);               // Noncompliant
    if (choice == 0) {
        res = x++ + y++;           // Noncompliant
        return(res);
    }
    else if (choice == 1) {
        x++;                       // Compliant
        y++;                       // Compliant
        return (x+y);
    }
    else {
        res = operation(x++,y);    // Noncompliant
        return(res);
    }
}

この例では、++ 演算子を含む式に、演算子以外の原因で発生した二次的影響がある場合、ルールに違反します。たとえば、式 return(x++) では、return 演算が他の二次的影響となります。

チェック情報

グループ: 二次的影響
カテゴリ: 推奨
AGC カテゴリ: Readability

バージョン履歴

R2024a で導入

すべて展開する


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.