メインコンテンツ

MISRA C++:2023 Rule 8.18.2

The result of an assignment operator should not be used

R2024b 以降

説明

ルール定義

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

根拠

大きい式の内部で代入演算を使用すると、コードの可読性が低下する可能性があります。

さらに、代入演算子を部分式で使用すると、追跡が難しい二次的影響がもたらされ、開発者の想定に反する結果が生じる可能性があります。

Polyspace 実装

代入演算が大きい式の内部の部分式である場合、ルール チェッカーは違反を報告します。部分式が評価されない場合、たとえば sizeof 演算子の内部の場合、チェッカーはこれを例外とします。

トラブルシューティング

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

すべて展開する

#include <cstdint>

bool example(int x, int y)
{
	if (x == 10) 		//Compliant
	{
		return true;
	}
	
       if ((x = y) == 0)        //Noncompliant
	{
		return false;
	}
	
	return false;
}

代入演算子 = が部分式 (x = y) で使用されているため、Polyspace は非準拠としてこれにフラグを設定します。

チェック情報

グループ: Expressions
カテゴリ: 推奨

バージョン履歴

R2024b で導入

すべて展開する


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.