Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.VAR.HIDDEN

Identifier 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 scope

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

An 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:

  • In C90 the minimum requirement is that the first 31 characters are significant;
  • In C99 the minimum requirement is that the first 63 characters are significant, with each universal character or extended source character counting as a single character.

Rationale

If 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:

  • 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.

Example

void 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 also

Rule 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.

Example

int16_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 scope

Rationale

If 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:

  • 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.

Example

int16_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
{
}