AutosarC++17_10-A2.11.1¶
An identifier declared in an inner scope shall not hide an identifier declared in an outer scope
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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-11-1.cpp 289436 2017-10-04 10:45:23Z 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
}
std::int32_t idx;
void F1(std::int32_t);
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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_hiding_if_condition_in_else¶
allow_hiding_if_condition_in_else : bool = False
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
allow_in_lambdas¶
allow_in_lambdas : bool = True
allow_in_namespaces¶
allow_in_namespaces : bool = True
allow_parameter_in_function_declaration¶
allow_parameter_in_function_declaration : bool = False
check_inheritance_hiding¶
check_inheritance_hiding : bool = False
check_nested_classes¶
check_nested_classes : bool = False
constructor_parameters_not_hiding_fields_in_initializer_list¶
constructor_parameters_not_hiding_fields_in_initializer_list : bool = False
distinguish_name_spaces¶
distinguish_name_spaces : bool = False
hiding_entities¶
hiding_entities : set[bauhaus.ir.LIR_Class_Name] = {'Field', 'Parameter', 'Typedef_Type', 'Variable'}
maxlen¶
maxlen : int | None = None
tolerate_constructor_parameters_hiding_fields¶
tolerate_constructor_parameters_hiding_fields : bool = False
tolerate_function_prototype_argument_hiding¶
tolerate_function_prototype_argument_hiding : bool = False
decltype()).
tolerate_hiding_private_members¶
tolerate_hiding_private_members : bool = False
tolerate_macro_local_identifier¶
tolerate_macro_local_identifier : bool = False