Main Content

MISRA C++:2008 Rule 5-2-10

The increment ( ++ ) and decrement ( -- ) operators should not be mixed with other operators in an expression

Description

Rule Definition

The increment ( ++ ) and decrement ( -- ) operators should not be mixed with other operators in an expression.

Rationale

Using the increment and decrement operators with other operators in an expressions results in code that is difficult to read. Such code might lead to undefined behavior.

Polyspace Implementation

Polyspace® flags an expression if it contains the increment or decrement operators mixed with other operators. If an expression contains multiple increment or decrement operators mixed with other operators, Polyspace flags the first increment or decrement 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

void foo(int a, int b){
	int c = ++a + b--; //Noncompliant
	if(--c + --a - ++b){ //Noncompliant
		//...
	}
}

Polyspace flags the mixing of the ++ and -- operators with other mathematical operators in an expression.

Check Information

Group: Expressions
Category: Advisory

Version History

Introduced in R2013b