MISRA.VAR.HIDDENIdentifier declared in an inner scope hides identifier in outer scope.
MISRA C 2012 Rule 5.3 :An identifier declared in an inner scope shall not hide an identifier declared in an outer scopeCategory: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationAn identifier declared in an inner scope shall be distinct from any identifier declared in an outer scope. The definition of distinct depends on the implementation and the version of the C language that is being used:
RationaleIf an identifier is declared in an inner scope but is not distinct from an identifier that already exists in an outer scope, then the inner-most declaration will “hide” the outer one. This may lead to developer confusion. Note: An identifier declared in one name space does not hide an identifier declared in a different name space. The terms outer and inner scope are defined as follows:
Examplevoid fn1 ( void ) { int16_t i; /* Declare an object "i" */ { int16_t i; /* Non-compliant - hides previous "i " */ i = 3; /* Could be confusing as to which "i" this refers */ } } struct astruct { int16_t m; }; extern void g ( struct astruct *p ); int16_t xyz = 0; /* Declare an object "xyz" */ void fn2 ( struct astruct xyz ) /* Non-compliant - outer "xyz" is * now hidden by parameter name */ { g ( &xyz ); } uint16_t speed; void fn3 ( void ) { typedef float32_t speed; /* Non-compliant - type hides object */ } See alsoRule 5.2, Rule 5.8 MISRA-C 2004 Rule 5.2 (required): Identifiers in an inner scope shall not use the same name as an identifier in an outer scope, and therefore hide that identifier.Variable declaration hides declaration in upper scope. The terms outer and inner scope are defined as follows. Identifiers that have file scope can be considered as having the outermost scope. Identifiers that have block scope have a more inner scope. Successive, nested blocks, introduce more inner scopes. The rule is only to disallow the case where a second inner definition hides an outer definition. If the second definition does not hide the first definition, then this rule is not violated. Hiding identifiers with an identifier of the same name in a nested scope leads to code that is very confusing. Exampleint16_t i; { int16_t i; /* This is a different variable */ /* This is not compliant */ i = 3; /* It could be confusing as to which i this refers */ } MISRA-C++ 2008 Rule 2—10—2 (required): Identifiers declared in an inner scope shall not hide an identifier declared in an outer scopeRationaleIf an identifier is declared in an inner scope and it uses the same name as an identifier that already exists in an outer scope, then the innermost declaration will "hide" the outer one. This may lead to developer confusion. The terms outer and inner scope are defined as follows:
Exampleint16_t i; { int16_t i; // This is a different variable // This is Non-compliant i = 3; // It could be confusing as to which i this refers } void fn ( int16_t i ) // Non-compliant { } |