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

`A7-1-7`: Avoid reporting the same expression or declaration in multiple template instantiations

Open lcartey opened this issue 1 year ago • 1 comments

Affected rules

  • A7-1-7

Description

In a template class or template function we may report the same logical expression in multiple template instantiations.

Example

template <typename T>
class Foo {
  void bar(T t) { t; } // COMPLIANT - but erroneously reported
};

void test() {
  Foo<int> i;
  Foo<float> f;
  i.bar(1);
  f.bar(1.0f);
}

lcartey avatar Oct 03 '23 22:10 lcartey

Cannot reproduce an amended version of the test case (bar is private). For each instantiation we get an ExprStmt for t in bar, however the location for each of those is the same and the query already excludes different ExprStmts with the same location.

@lcartey Is there more context that we can use to reproduce?

rvermeulen avatar Feb 15 '24 22:02 rvermeulen

Re-reviewing the original bug report I believe I misdiagnosed this issue - it is actually completely unrelated to templates.

A fuller reproduction case is as follows:

class Test {
public:
  friend constexpr void swap(Test &lhs, Test &rhs) noexcept { lhs.swap(rhs); }
  void swap(Test &other) noexcept;
};

void test_swap() {
  Test a1, a2;
  swap(a1, a2);
}

The query reports that the function swap is defined on the same line as the expression statement lhs.swap(rhs). Reviewing the AUTOSAR rule, I don't believe the intention is to report such cases - the rule itself does not mention functions either way, and it certainly does not make sense to report the function declaration in which an expression statement is declared as contravening in this case.

As the problem is distinctly different from this initial bug report, I'm closing this as "Doesn't reproduce" and opening a new issue here to track the function problem: https://github.com/github/codeql-coding-standards/issues/628

lcartey avatar Jun 26 '24 10:06 lcartey

In debugging this issue, I identified a couple more problems with A7-1-7 and logged those separately:

  • https://github.com/github/codeql-coding-standards/issues/629
  • https://github.com/github/codeql-coding-standards/issues/630

lcartey avatar Jun 26 '24 11:06 lcartey