MISRA Discussion Forums
Rule 10.6 An expression shall not be assigned to a wider type - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165)
+---- Thread: Rule 10.6 An expression shall not be assigned to a wider type (/showthread.php?tid=1524)



Rule 10.6 An expression shall not be assigned to a wider type - LordMordac - 27-01-2020

The full rule title is:
Quote:The value of a composite expression shall not be assigned to an object with a wider essential type

I am having a lively debate with my scanner vendor about the quality of their scans. The following example code is generating the commented violation:
[code]
static inline uint32_t bit32 (uint32_t pos)
{
static const uint32_t mask = 31U;
static const uint32_t one = 1U;

// Event misra_c_2012_rule_10_6_violation: Assigning composite expression "1U


Re: Rule 10.6 An expression shall not be assigned to a wider type - misra-c - 03-02-2020

Appendix D.7 gives the essential type of an Shift Expression. It says:
Quote: Shift( > )
1. If the left hand operand is essentially unsigned then:
1.1 If both operands are integer constant expressions then the essential type of the result is the UTLR of the result;
1.2 Else the essential type of the result is the essential type of the left hand operand
In this case 1.2 applies and essential type is that of the left hand operand. The essential type of the left hand operand is uint32_t. There is therefore no assignment to a wider essential type and no violation of rule 10.6.

There would have been a violation of rule 10.6 if the code had been written as
[code] 1U