Closed
Description
Affected rules
- M0-1-10
Description
The query M0-1-10 generates a significant amount of false positives while analyzing "unused functions". These are noticed especially in the following scenarios.
- constexpr functions
- template instantiations
- special member functions (constructors, destructors, operators, conversion operators)
This results from the fact that CodeQL cannot track the usages of such functions in compile time constructs like constexpr, static_asserts, templates etc.
Example
test.hpp
template <class T>
constexpr T to_underlying(T value) noexcept { // M0-1-3 violation reported here
return static_cast<T>(value);
}
test.cpp
#include <type_traits>
#include "test.hpp"
template <typename T1, typename T2>
constexpr bool StaticAssertTypeEq() noexcept {
static_assert(std::is_same<T1, T2>::value, "T1 and T2 are not the same type");
return true;
}
template <typename T, int val>
class X
{
T arr[val];
};
void foo()
{
struct dummy {
dummy() noexcept(false) { static_cast<void>(0); } // M0-1-3 violation reported here
};
// usage of dummy
static_assert(!std::is_nothrow_default_constructible<X<dummy, 5>>::value,
"Must not be nothrow default constructible");
}
int main()
{
int a;
StaticAssertTypeEq<decltype(to_underlying(a)), int>(); // usage of to_underlying
foo();
return 0;
}