RULE-6-9-1: fix false positives for redeclarations that use the same type alias - #1180
Draft
castler wants to merge 1 commit into
Draft
RULE-6-9-1: fix false positives for redeclarations that use the same type alias#1180castler wants to merge 1 commit into
castler wants to merge 1 commit into
Conversation
RULE-6-9-1 reported false positives on entities that use identical type alias spellings in every declaration (e.g. a function prototype in a header and its definition in a .cpp both returning the same aliased type). The query decides that two declaration entries disagree using `t.getATypeNameUse() = decl1 and not t.getATypeNameUse() = decl2`. `TypedefType.getATypeNameUse()` is documented as incomplete and, in whole-program extraction, is inconsistent across translation units: the same header line yields one DeclarationEntry per including TU, and the alias is associated with some copies but not others. The query then pairs a "use" entry with a "no-use" entry and reports a spurious divergence. Add three guards, backed by helper predicates: - sameSourceLocation: drop pairs that are the same source declaration seen from different TUs (same file/line/column). - template-instantiation exclusion: synthesised instantiation entries duplicate the template's entries without recording type-name uses. - typeAliasMentionedIn: before reporting that decl2 fails to use the alias, confirm via TypeMention (which records every syntactic type mention). Match by qualified name so a generic alias template (Result) is recognised as the instantiated result (Result<X>), and extend the search range to the function body start to catch trailing return types. Validated on a real codebase: 234 -> 30 findings (87% reduction, all eliminated findings verified as false positives). The existing unit test still passes, so no true positives are lost. No qltest regression test is added because the false positive is an emergent property of multi-TU whole-program extraction and does not reproduce in the single/two-TU test harness. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
castler
marked this pull request as draft
August 24, 2026 13:29
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.
Sorry - this is not yet close ready to being reviewed!
Problem
RULE-6-9-1("The same type aliases shall be used in all declarations of thesame entity") reports a large number of false positives on real code. Every one
of the reported entities actually uses identical alias spellings in all its
declarations, e.g. a function prototype in a header and its definition in a
.cppboth returningelement_fq_id&,LoggingCallback,Result<...>, etc.Root cause
The query fires on:
TypedefType.getATypeNameUse()is documented as a conservative / incompleterelation ("not necessarily all type name uses"; it omits uses on prototypes
and around template instantiations). Worse, in whole-program extraction it is
inconsistent across translation units: the same header line produces
multiple
DeclarationEntryobjects (one per including TU) andgetATypeNameUse()associates the alias with some copies but not others.Direct evidence (return-by-
const HandleType&accessor):The query pairs the
YESentry (decl1) with aNOentry (decl2) andreports a divergence, even though both declarations spell the alias identically.
Fix
Add three guards to the
whereclause, backed by three helper predicates:sameSourceLocation(decl1, decl2)— two entries at the exact same file /line / column are the same source declaration seen from different TUs, not
two redeclarations that could disagree. Exclude them.
duplicate the template's entries without recording type-name uses.
typeAliasMentionedIn(t, decl2)— before reporting thatdecl2fails touse
t, confirm it viaTypeMention, which records every syntactic typemention with its location. Matching is done by qualified name so that a
mention of a generic alias template (
Result) is recognised as the samealias as the instantiated
getATypeNameUse()result (Result<X>). Forfunction definitions the mention search range is extended to the start of the
function body to catch trailing return types (
auto f() -> T).Validation
eclipse-score/communication,//score/message_passing+//score/mw/com, whole-program CodeQL database, coding-standards pack2.62.0baseline): 276 → 30 findings (89% reduction). All 246 eliminatedfindings were manually verified as false positives (identical alias spelling
confirmed by reading the source at both declaration sites); the 30 remaining
findings are genuine spelling mismatches (e.g. a class member alias used in
the header vs. the underlying namespace-scope alias spelled out explicitly in
the out-of-line definition).
TypeAliasesDeclaration.qlrefstill PASSES — thegenuine
INT/Index-vs-int/idivergences are still reported, so no truepositives are lost.
Known limitation
The residual ~30 findings (on our codebase) are return-by-
const Alias&accessors and some template members where neither
getATypeNameUse()norTypeMentionemits any mention of the alias on the relevant line, so noreliable signal exists to suppress them without risking false negatives. These
are all genuine violations in our codebase, so this is expected, not a gap.
Why no qltest regression test
The false positive is an emergent property of whole-program (multi-hundred-TU)
extraction: the cross-TU
getATypeNameUse()asymmetry does not arise in asingle TU (the relation is symmetric there) and did not reproduce in a minimal
2-TU header test either (the extractor merge stayed symmetric). This is the same
reason the existing per-rule test never caught the issue. The fix is therefore
validated against the real codebase numbers above rather than a synthetic
.expected.