メインコンテンツ

MISRA C:2023 Rule 13.4

The result of an assignment operator should not be used

R2024a 以降

説明

ルール定義

The result of an assignment operator should not be used 1 .

根拠

同じ式で以下が発生する場合、ルールに違反します。

  • 代入演算子が変数に作用する。

  • 別の読み取りまたは演算が代入結果に対して実行される。

たとえば、式 a[x]=a[x=y]; はこのルールに違反しています。[] 演算子は代入 x=y の結果に作用しています。

トラブルシューティング

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

すべて展開する

int x, y, b, c, d;
int a[10];
unsigned int bool_var, false=0, true=1;

int foo(void) {

    x = y;            /* Compliant - x is not used */

    a[x] = a[x = y];  /* Non-compliant - Value of x=y is used */

    if ( bool_var = false )/* Non-compliant - bool_var=false is used */
{}


    if ( bool_var == false ) {}   /* Compliant */

    if ( ( 0u == 0u ) || ( bool_var = true ) )/* Non-compliant */
                    /*- even though (bool_var=true) is not evaluated */
 {} 
    

    if ( ( x = f () ) != 0 )/* Non-compliant - value of x=f() is used */
 {} 
    a[b += c] = a[b];/* Non-compliant - value of b += c is used */
    b = c = d = 0; /* Non-compliant - value of d=0 and c=d=0 are used */

}

この例では、代入結果が使用される場合、ルールに違反します。

チェック情報

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

バージョン履歴

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.