RULE-5-10-1: fix false positives for locals/parameters in permitted std specializations (e.g. std::hash) - #1181
Draft
castler wants to merge 1 commit into
Conversation
…zations
RULE-5-10-1 (poorly-formed identifier) flagged ordinary, well-formed,
snake_case local variables and function parameters (e.g. `value`,
`result`, `view`, `instance_identifier`) with "Identifier 'X' is
defined in reserved namespace." whenever they were declared inside the
body of an explicit template specialization that C++ explicitly
permits users to add to namespace `std`, most commonly
`std::hash<UserType>::operator()`. On the eclipse-score/communication
codebase this produced 66 findings, all inside `std::hash<UserType>`-
style specializations in headers such as handle_type.h,
trace_point_key.h and service_identifier_type.h.
Root cause: `VariableDeclarationEntryIdentifier::getNamespace()` used
`Variable.getNamespace()` directly. For a `Parameter` or
`LocalVariable`, the upstream CodeQL C++ library defines this as the
namespace of the *enclosing function* (see
`codeql/cpp-all/.../Declaration.qll`), which is a reasonable general
notion of "namespace context" but is not what RULE-5-10-1 means by "is
defined in reserved namespace". A block-scope local or parameter is
scoped to the body of its enclosing function; it is not itself a
member of that function's enclosing namespace and therefore cannot
introduce a new name into (or "pollute") a reserved namespace such as
`std`, regardless of which namespace the enclosing function happens to
be declared in. This is exactly the situation for the body of a
permitted `std::hash<UserType>` specialization: the parameter and local
names are ordinary user-chosen identifiers that merely happen to be
lexically nested inside `namespace std { ... }` because the standard
requires the specialization to be written there.
Fix: in `VariableDeclarationEntryIdentifier`, only report a namespace
for variables that are not `LocalScopeVariable` (i.e. not a `Parameter`
or `LocalVariable`). This is a narrow, semantically-motivated scope
check, not a name- or path-based exclusion: any genuinely new
namespace-scope or class-scope member added directly to `std` (a new
global, function, type, or the specialization itself) is still checked
independently via `FunctionDeclarationEntryIdentifier` /
`TypeDeclarationEntryIdentifier`, and remains flagged. A new
NON_COMPLIANT test case (a function declared directly in `namespace
std`) confirms this is unaffected.
Added a regression test reproducing the `std::hash<UserType>`
specialization shape with a parameter and a local both named after
the FP pattern (COMPLIANT), and confirmed with the "teeth" test that
reverting only `Identifiers.qll` while keeping the new test makes it
fail with exactly the two spurious findings. The existing RULE-5-10-1
test suite and the shared Identifiers library test continue to pass.
Validated against the real eclipse-score/communication CodeQL database
(`//score/message_passing //score/mw/com`): "is defined in reserved
namespace" findings for this shape dropped from 66 to 0, while the
one, unrelated "starts with underscore" finding in the same rule is
unchanged. All 66 baseline findings were manually confirmed to be
locals/parameters inside `std::hash<UserType>`-style specializations,
i.e. genuine false positives.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
castler
marked this pull request as draft
August 24, 2026 19:10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
RULE-5-10-1("User-defined identifiers shall have an appropriate form") reportsIdentifier 'X' is defined in reserved namespace.for ordinary, well-formed,snake_case local variables and function parameters — e.g.
value,result,view,identifier,instance_identifier,skeleton_instance_identifier—whenever they are declared inside the body of an explicit template
specialization that C++ explicitly permits users to add to namespace
std,most commonly
std::hash<UserType>::operator():[namespace.std]/[hash.requirements]explicitly permit a program to add atemplate specialization for a standard library template (like
std::hash) tonamespace
std, provided the specialization depends on a user-defined type.valuehere is not a new member of namespacestdin the sense the rule istrying to prevent — it is an ordinary parameter name scoped to the function
body, which merely happens to be lexically nested inside
namespace std { ... }because that is where the standard requires the specialization to be written.
Root cause
VariableDeclarationEntryIdentifier::getNamespace()(incpp/common/src/codingstandards/cpp/Identifiers.qll) delegated directly toVariable.getNamespace():For a
ParameterorLocalVariable, the upstream CodeQL C++ library definesthis predicate (in
codeql/cpp-all/.../semmle/code/cpp/Declaration.qll) as thenamespace of the enclosing function:
That is a reasonable general notion of "namespace context" for other purposes,
but it is not what RULE-5-10-1 means by "is defined in reserved namespace". A
block-scope local variable or parameter is scoped to the body of its
enclosing function; it is not itself a member of that function's enclosing
namespace and cannot introduce a new name into (or "pollute") a reserved
namespace such as
std, regardless of which namespace the enclosing functionhappens to be declared in. That is exactly the situation inside a permitted
std::hash<UserType>specialization body.Fix
In
VariableDeclarationEntryIdentifier, only report a namespace for variablesthat are not a
LocalScopeVariable(the common CodeQL superclass ofParameterandLocalVariable):This is a narrow, semantically-motivated scope check — not a name- or
path-based exclusion (nothing matches on
hash,std::hash, or any filepath). It only changes behaviour for parameters/locals whose enclosing
function happens to be declared in
std/stdN/posix, which is rare outsidepermitted specializations.
Crucially, this does not weaken the rule for genuine violations: any new
entity that actually introduces a name into namespace
std/posix— a newglobal variable, a new function, a new type, or the specialization name itself
— is still caught by
VariableDeclarationEntryIdentifier(for namespace-scopeglobals, whose namespace is not derived through the
LocalScopeVariablepath),
FunctionDeclarationEntryIdentifier, orTypeDeclarationEntryIdentifierrespectively, all of which are unaffected by this change.
Test
Added to
cpp/misra/test/rules/RULE-5-10-1/test.cpp(appended, so no existing.expectedlocations shift except for our own new lines):std::hash<UserTypeForHash>specialization with a parametervalueand alocal
result— both COMPLIANT.namespace std(not aspecialization) — still NON_COMPLIANT, confirming the fix does not widen
scope beyond block-scope locals/parameters.
Teeth test: reverted only
Identifiers.qlltoHEADwhile keeping the newtest — it fails, reporting exactly the two spurious findings for
valueandresult(plus the expectednew_std_functionNON_COMPLIANT):With the fix restored,
codeql test run cpp/misra/test/rules/RULE-5-10-1/PASSES. The shared library regression test
cpp/common/test/library/codingstandards/cpp/identifiers/IdentifiersTest.qlalso still PASSES unchanged.
Validation against a real codebase
Ran the fixed query (out-of-tree,
--additional-packspointing at thischeckout,
--rerun, real release pack temporarily hidden to avoid shadowing)against a whole-program CodeQL database for
eclipse-score/communication(
//score/message_passing //score/mw/com):... is defined in reserved namespace.... starts with underscore.(unrelated shape, out of scope for this fix)All 66 baseline findings were inspected by location; every single one is a
local variable or parameter inside the body of an explicit
std::hash<UserType>-style specialization (e.g.handle_type.h,trace_point_key.h,service_identifier_type.h,skeleton_instance_identifier.h,instance_specifier.h,find_service_handle.cpp) — i.e. all 66 were genuine false positives of theshape described above, and all are eliminated by this fix. The one remaining
finding (
starts with underscore, on an unrelated_parameter name) is adifferent sub-check of the same query and is untouched by this change.
Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com