Main Content

AUTOSAR C++14 Rule M5-0-7

There shall be no explicit floating-integral conversions of a cvalue expression

Description

Rule Definition

There shall be no explicit floating-integral conversions of a cvalue expression.

Rationale

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. For instance, in this example, the result of an integer division is then cast to a floating-point type.

short num;
short den;
float res;
res= static_cast<float> (num/den);
However, a developer or code reviewer can expect that the evaluation uses the data type to which the result is cast later. For instance, one can expect a floating-point division because of the later cast.

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 func() {
     short num;
     short den;
     short res_short;
     float res_float;
     
     res_float = static_cast<float> (num/den); //Noncompliant
     
     res_short = num/den;
     res_float = static_cast<float> (res_short); //Compliant

}

In this example, the first cast on the division result violates the rule but the second cast does not.

  • The first cast can lead to the incorrect expectation that the expression is evaluated with an underlying type float.

  • The second cast makes it clear that the expression is evaluated with the underlying type short. The result is then cast to the type float.

Check Information

Group: Expressions
Category: Required, Automated

Version History

Introduced in R2019a

expand all