RULE-5-10-1: fix false positives for locals/parameters in permitted std specializations (e.g. std::hash) - #1181
Open
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
castler
marked this pull request as ready for review
August 25, 2026 05:21
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.
Description
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.
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.