Main Content

AUTOSAR C++14 Rule M6-2-1

Assignment operators shall not be used in sub-expressions

Description

Rule Definition

Assignment operators shall not be used in sub-expressions.

Rationale

When used in a subexpression, assignment operators have side effects that are difficult to predict. These side effects might produce results contrary to developer expectations. This rule helps in avoiding confusion between the assignment operator (=) and the equal to operator (==). Do not use assignment operators in subexpressions.

Polyspace Implementation

Polyspace® raises this defect whenever a subexpression contains an assignment operator.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>

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

Because the assignment operator = is used in the subexpression (x = y), Polyspace flags it as noncompliant.

Check Information

Group: Statements
Category: Required, Automated

Version History

Introduced in R2019a