codeql-coding-standards icon indicating copy to clipboard operation
codeql-coding-standards copied to clipboard

`A2-10-1`: Report type and function identifier hiding

Open lcartey opened this issue 2 years ago • 2 comments

Affected rules

  • A2-10-1

Description

A2-10-1 states "An identifier declared in an inner scope shall not hide an identifier declared in an outer scope.". The existing query looks for user defined variables which cause hiding, but functions and types can also be hidden (and cause hiding).

Example

int a;
namespace b {
        void a() { } // NON_COMPLIANT
        int c() { return a; }
}

lcartey avatar Nov 03 '22 08:11 lcartey

This is an interesting example that is close to the exception discussed in Autosar, but still marked as non-compliant in the examples in Autosar. It is interesting because like the exception we can still reference a in the global scope as:

int a;
namespace b {
        void a() { }
        int c() { return ::a; }
}

rvermeulen avatar Feb 28 '24 23:02 rvermeulen

It seems MISRA C++ 23 completely removes the exceptions in 6.5.1. The also only focus on variables (6.4.1) and member functions (6.4.2)

It is interesting to have the distinction between global namespace and named namespace for the exception since both cases the hidden variable can still be referenced, but in one case it is accepted and the other not. Seems equally confusing as is confirmed by MISRA C++ 23.

rvermeulen avatar Feb 28 '24 23:02 rvermeulen