MISRA C++:2008 Rule 5-0-8
An explicit integral or floating-point conversion shall not increase the size of the underlying type of a cvalue expression
Description
Rule Definition
An explicit integral or floating-point conversion shall not increase the size of the underlying type of a cvalue expression.
Rationale
Expressions flagged by this checker follow the detailed specifications for cvalue expressions from the MISRA™ C++ documentation.
If you evaluate an expression and later cast the result to a different type, the
cast has no effect on the underlying type of the evaluation (the widest of operand
data types in the expression). For instance, in this example, the sum of two
short
operands is cast to the wider type
int
.
short op1; short op2; int res; res= static_cast<int> (op1 + op2);
int
because of the later cast.Additional Message in Report
An explicit integral or floating-point conversion shall not increase the size of the underlying type of a cvalue expression.
Complex expression of underlying type
typeBeforeConversion
may only be cast to narrower
integer type of same signedness, however the destination type is
typeAfterconversion
.
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
Check Information
Group: Expressions |
Category: Required |
Version History
Introduced in R2013b