Main Content

AUTOSAR C++14 Rule A2-10-6

A class or enumeration name shall not be hidden by a variable, function or enumerator declaration in the same scope

Since R2020a

Description

Rule Definition

A class or enumeration name shall not be hidden by a variable, function or enumerator declaration in the same scope.

Rationale

When a variable, data member, function, or enumerator shares its name with a class or enumeration in the same scope, the latter is hidden. That is, all uses of the name refers to the variable, data member, function, or enumerator instead of the class or enumeration, regardless of declaration order. Hidden classes or enumerations can be misleading and can lead to compilation errors. Do not re-use names to declare classes and enumerations.

Polyspace Implementation

Polyspace® flags the declaration of a variable, data member, function, or enumerator that shares the name of a class or enumeration in the same block.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

The following example demonstrate the Polyspace implementation of AUTOSAR rule A2-10-6.

#include <cstdint>
namespace NS1
{
	class G {};
	void G() {}             //Noncompliant
}    
namespace NS2
{
	enum class H { VALUE=0, };
	std::uint8_t H = 17;    //Noncompliant
}
namespace NS3
{
	class J {};
	enum H {                 
		J=0,                 // Noncompliant 
	};
} 
main()
{
	//...
}
 

Polyspace flags the declaration of the:

  • Function G() because it hides the class G declared in the same block.

  • Variable H because it hides the enumeration H declared in the same block.

  • Enumerator J because it hides the class J is declared in the same block.

Check Information

Group: Lexical conventions
Category: Required, Automated

Version History

Introduced in R2020a