github / codeql-coding-standards

This repository contains CodeQL queries and libraries which support various Coding Standards.
MIT License
129 stars 59 forks source link

`A14-5-2`: type member dependent on class' template parameter detected not dependent #739

Open fjatWbyT opened 1 month ago

fjatWbyT commented 1 month ago

Affected rules

Description

Type member defined with a dependent name and template parameter results in recommendation to be defined in a separated class because it is identified to be not dependent on the template parameter.

Example

template<typename T>
struct a_template_struct {
  using type = T;
};

template<typename T>
struct another_template_struct {
  using type = typename a_template_struct<T>::type;
};

Query cpp/autosar/src/rules/A14-5-2/NonTemplateMemberDefinedInTemplate.ql outputs that another_template_struct::type does not use any of the template arguments of another_template_struct.

fjatWbyT commented 1 month ago

Strategy proposal

Add an additional omission case for type members declared with using aliases. This is motivated by the many A14-5-2 alerts overall produced in STL-like libraries (out of which, I've estimated an 80% of them to be false positives).

For example, I measured 1988 A14-5-2 alerts in abseil-cpp including false positives in aliases such as the ones starting at this line, and reduced it to 1540 with this strategy.

I was happy to see that current unit tests would finely do to capture the impact:

 using T1 = typename template_base<T>::type;   // COMPLIANT[FALSE_POSITIVE]
 using T2 = typename template_base<int>::type; // NON_COMPLIANT

would become

 using T1 = typename template_base<T>::type;   // COMPLIANT
 using T2 = typename template_base<int>::type; // NON_COMPLIANT[FALSE_NEGATIVE]