AutosarC++18_03-A2.10.1

An identifier declared in an inner scope shall not hide an identifier declared in an outer scope

Required inputs: IR

Rationale

If an identifier is declared in an inner scope and it uses the same name as an identifierthat already exists in an outer scope, then the innermost declaration will “hide” theouter one. This may lead to developer confusion. The terms outer and inner scope aredefined 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.

Note that declaring identifiers in different named namespaces, classes, structs or enumclasses will not hide other identifiers from outer scope, because they can be accessedusing fully-qualified id.

Exceptions

An identifier declared within a namespace using the same name as an identifier of the containing namespace does not violate the rule.

An identifier declared locally inside a lambda expression and not referring to a name of a captured variable does not violate the rule.

Example
//% $Id: A2-10-1.cpp 313834 2018-03-27 11:35:19Z michal.szczepankiewicz $
#include <cstdint>
std::int32_t sum = 0;
namespace {
std::int32_t sum; // Non-compliant, hides sum in outer scope
}
class C1 {
  std::int32_t sum; // Compliant, does not hide sum in outer scope
};
namespace n1 {
std::int32_t sum; // Compliant, does not hide sum in outer scope
namespace n2 {
std::int32_t sum; // Compliant, does not hide sum in outer scope
}
} // namespace n1

std::int32_t idx;
void F1(std::int32_t idx) {
  // Non-compliant, hides idx in outer scope
}

void F2() {
  std::int32_t max = 5;

  for (std::int32_t idx = 0; idx < max;
       ++idx) // Non-compliant, hides idx in outer scope
  {
    for (std::int32_t idx = 0; idx < max;
         ++idx) // Non-compliant, hides idx in outer scope
    {
    }
  }
}
void F3() {
  std::int32_t i = 0;
  std::int32_t j = 0;
  auto lambda = [i]() {
    std::int32_t j = 10; // Compliant - j was not captured, so it does not hide
                         // j in outer scope
    return i + j;
  };
}
See Also
  • MISRA C++ 2008 [6]: required 2-10-2 Identifiers declared in an inner scope shall not hide an identifier declared in an outer scope.
  • JSF December 2005 [7]: 4.15 AV Rule 135 Identifiers in an inner scope shall not use the same name as an identifier in an outer scope, and therefore hide that identifier.
  • HIC++ v4.0 [8]: 3.1.1 Do not hide declarations.

Possible Messages

Key

Text

Severity

Disabled

hiding

{} hides {}

None

False

Options

allow_hiding_if_condition_in_else

allow_hiding_if_condition_in_else : bool = False

Allow hiding a variable declared in the condition of an if-statement within the else block. This is useful to allow re-use of the same variable name in a chain of else-if using C++17 variable declarations in the if-statement:
  if (auto x = get(1); x.ok()) {}
  else if (auto x = get(2); x.ok()) {}
This option causes the rule to act as if variables declared in the if condition were only visible in the then-branch.
 

allow_in_classes

allow_in_classes : bool = True

Whether identifiers in local classes can hide surrounding ones.
 

allow_in_lambdas

allow_in_lambdas : bool = True

Whether identifiers in lambdas can hide surrounding ones.
 

allow_in_namespaces

allow_in_namespaces : bool = True

Whether identifiers in namespaces can hide surrounding ones.
 

allow_parameter_in_function_declaration

allow_parameter_in_function_declaration : bool = False

Whether identifiers of parameters in function declarations may hide surrounding ones, given the function declaration uses a different identifier for the same parameter that does not hide a surrounding one.
 

check_inheritance_hiding

check_inheritance_hiding : bool = False

Whether to check for hiding by inheritance.
 

check_nested_classes

check_nested_classes : bool = False

Whether symbols from inner classes should be reported as hiding symbols of outer classes.
 

constructor_parameters_not_hiding_fields_in_initializer_list

constructor_parameters_not_hiding_fields_in_initializer_list : bool = False

Consider use of constructor parameters in the constructor initializer list not as hiding, when parameter name is also a class field name.
 

distinguish_name_spaces

distinguish_name_spaces : bool = False

Whether C name spaces should be distinguished.
 

hiding_entities

hiding_entities : set[bauhaus.ir.LIR_Class_Name] = {'Field', 'Parameter', 'Typedef_Type', 'Variable'}

LIR node types to consider symbols that hide other symbols.
 

maxlen

maxlen : int | None = None

Number of significant characters (or None).
 

tolerate_constructor_parameters_hiding_fields

tolerate_constructor_parameters_hiding_fields : bool = False

Allow constructor parameters to hide fields
 

tolerate_function_prototype_argument_hiding

tolerate_function_prototype_argument_hiding : bool = False

Whether to tolerate arguments of a function prototype, that itself is a function argument, that have the same identifier as a argument of the outer function. If true, the rule will still report cases, where argument identifiers match up and the function prototype refers to arguments of the outer function (e.g. by decltype()).
 

tolerate_hiding_private_members

tolerate_hiding_private_members : bool = False

Whether to tolerate hiding private members of base classes.
 

tolerate_macro_local_identifier

tolerate_macro_local_identifier : bool = False

Whether an identifier being local to a macro is allowed to hide identifiers from outside the macro.