MISRA Discussion Forums
Rule 2.2 - dead code - 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.2 Unused code (https://forum.misra.org.uk/forumdisplay.php?fid=157)
+---- Thread: Rule 2.2 - dead code (/showthread.php?tid=1266)



Rule 2.2 - dead code - delta_controls - 25-07-2016

Does the following code example violate Rule 2.2, due to the left shift?

[code]#define BITSET(reg, bit) ((reg) |= (uint8_t)(1u


Re: Rule 2.2 - dead code - Steve Montgomery - 30-07-2016

At first sight, I didn't believe that there was any dead code in your example. However, the question prompted me to re-read Rule 2.2 just to be sure. Having done so, I think you example does contain dead code according to the rule, even though common sense says that it doesn't.

The rule says
Quote:Any operation that is executed but whose removal would not affect program behaviour constitutes dead code

The uint8_t cast seems to satisfy this definition: it is an operator and it is certainly executed but removing it will have no effect on the program's behaviour. If that's the case, then this kind of dead code must be quite common. The essential type rules, for example, may well require insertion of casts that don't actually change the program's behaviour.

Maybe I'm misunderstanding what Rule 2.2 says but I would think that it's not intended to apply to your example.


Re: Rule 2.2 - dead code - delta_controls - 04-08-2016

Thanks for the reply.

Having thought about this, I agree that there is no violation, since the rule is intended to be applied at statement level. As you say, if this weren't the case, there would be violations all over the place and therefore many deviations for any given project.


Re: Rule 2.2 - dead code - Steve Montgomery - 10-08-2016

Could we have an official comment from MISRA on this, namely:
  • does the example contain dead code or not?
  • if not, how does the cast operation avoid being dead given the Glossary definition?



Re: Rule 2.2 - dead code - misra-c - 23-09-2016

There are various issues raised by the original question and subsequent discussion which we will address in turn.



1. Rule 2.2 applies to operators not statements. 

  It is not a question of whether a whole statement is dead code, but whether there is an operation within the expression that has no effect on the program behaviour.



2. There are two operators of interest in this macro expansion.  First is the operation
  1u << (bit)  which is expanded to 1u << ( 0 )
  The shift operation has no effect on the program behaviour since shifting any value by 0 always produces the same answer.  Therefore the "<<(0)" can be removed and there is a violation of rule 2.2.

3. The second operation of interest is the cast which in this macro expansion has no effect and therefore is strictly speaking a violation of rule 2.2.
  However we agree with the previous poster that this was probably not what was intended by this rule. We will revisit this issue at a later date.