Hello and good morning,
so it seems that the detection for rule cpp:S6012 (“Redundant class template arguments should not be used”) does not work correctly. I’ll attach a code snippet below:
#include <cstddef>
#include <cstdlib>
template <typename T>
class MyClass
{
public:
MyClass(size_t size, const T& value = T{}) :
_size(size),
_value(value)
{}
private:
size_t _size;
T _value;
};
int main()
{
MyClass obj1(10); // does not compile (obviously)
MyClass<int> obj2(10); // "violates" rule cpp:S6012
return EXIT_SUCCESS;
}
When creating obj1
, compilation fails, since the template argument cannot be deduced - an explicit class template argument is required. However, if you do so (obj2
), both Sonarlint in VS Code and the full analysis mark it as a violation of rule cpp:S6012. (I guess the issue is with the defaulted argument in the constructor - after all, if I gave another argument when creating the objects, it would work without the explicit template argument.)
Rule cpp:6012 states “This rule raises an issue when explicit class template arguments that can be automatically deduced is specified.”. As far as I can see, it cannot be automatically deduced for obj2
so the issues should not be raised.
Feel free to correct me if I’m missing something but that just seems like a bug in the analysis.