Main Content

MISRA C++:2023 Rule 6.2.2

All declarations of a variable or function shall have the same type

Since R2024b

Description

This checker is deactivated in a default Polyspace® as You Code analysis . See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).

Rule Definition

All declarations of a variable or function shall have the same type.

Rationale

If the declarations of an object or function in two different translation units have incompatible types, the behavior is undefined.

Polyspace Implementation

Polyspace considers two types to be compatible if they have the same size and signedness in the environment that you use. The checker is not raised on unused code such as

  • Noninstantiated templates

  • Uncalled static or extern functions

  • Uncalled and undefined local functions

  • Unused types and variables

Polyspace reports a violation if you declare a function with a [[noreturn]] attribute in one translation unit, but another translation unit that declares the same function does not use the [[noreturn]] attribute.

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

file1.cpp

                  
                  
typedef          char         char_t;
typedef signed   short        int16_t;
typedef signed   long         int64_t;

namespace bar {
	int64_t a;
	int16_t c;  

};

file2.cpp

                  
                  
typedef          char         char_t;
typedef signed   int          int32_t;

namespace bar {
	extern char_t c;    // Noncompliant
	extern int32_t a;
	void foo(void){
		++a;
		++c;
	}
}; 

In this example, the variable bar::c is defined as a char in file2.cpp and as a signed short in file1.cpp. In the target processor i386, the size of these types are not equal. Polyspace flags the definition of bar::c.

The variable bar::a is defined as a long in file1.cpp and as an int in file2.cpp. In the target processor i386, both int and long has a size of 32 bits. Because the definitions of bar::a is compatible in both files, Polyspace does not raise a flag.

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2024b