![]() |
![]() |
4391 | ![]() |
||||
![]() | |||||||
| Arithmetic type - Composite expressions | |||||||
This message is issued whenever a composite expression of essentially unsigned type has been cast to a wider unsigned integer type. It is a common misconception among programmers that the type in which the composite expression is evaluated is influenced by the wider type. Instead, the type of the expression is determined solely from the type of its operands after any integral promotion. Therefore, wraparound may occur, which could be avoided if the operands were cast to the wider type, or rearranged so that wider operands appear first. For example:
/*PRQA S 1-9999 ++*/
/*PRQA S 4391 --*/
void foo (unsigned b);
unsigned f4391 (unsigned short a)
{
foo ((unsigned) (a + 1u)); /* 4391 */
return ((unsigned) (a * 2u)); /* 4391 */
}
For the purposes of QAC conversion messages, types are ordered according to the
configured size (-s option), by default; hence for a typical 32 bit configuration,
types int and long will be considered equivalent.
However, when the -strictrank option is specified, types will be ordered according to
their rank; consequently int will be considered narrower
than long.
MISRA-C:2004 Rules applicable to message 4391:
| Rule 10.3 (Required) | The value of a complex expression of integer type shall only be cast to a type of the same signedness that is no wider than the underlying type of the expression. |
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |