MISRA Discussion Forums
What about unused variables? - 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: What about unused variables? (/showthread.php?tid=1080)



What about unused variables? - swestin - 18-07-2014

The guidelines proscribe declaring unused types, tags on structures or enums, macros, or labels.

Shouldn't there be a prohibition of undeclared variables, too?

-Stephen H. Westin


Re: What about unused variables? - misra-c - 22-08-2014

We are assuming that the question is intended to read "Shouldn't there be a prohibition of unused variables, too?"

Rule 2.7 covers variables that are declared as parameters to functions.

MISRA C:2012 permits other variables to be initialised and not used. However, if a variable is assigned a value, which is not used, then dead code will exist and rule 2.2 will be violated.

Unused initialisations are permitted because a common programming style is to use configuration files and conditional compilation. This can lead in some configurations to some variables being initialised but never used.


RE: What about unused variables? - misra-c - 01-07-2024

A new Rule 2.8 "A project should not contain unused object definitions" was added by MISRA C:2012 Amendment 4

Thanks for the suggestion