cpp:S5425 false positive on lambda with templated invocation

So in this case, an exception needs to be made - std::forward() does not need to be called on the parameter, even though it is a universal reference. It seems that you already make an exception for the easy case (direct invocation), but not when operator() is called explicitly. I need to call operator() explicitly in order to specify template arguments for a templated operator().

  • Are you using
    • SonarQube - which version?
      Enterprise Edition Version 9.9 (build 65466)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
#include <iostream>
#include <typeinfo>

template<typename F>
void Invoke(F&& f) // No warning
{
    f();
}

template<typename F>
void InvokeExplicit(F&& f) // cpp:S5425 warning
{
    f.operator()();
}

template<typename F>
void InvokeTemplated(F&& f) // cpp:S5425 warning
{
    f.template operator()<int>();
}

int main(){
    Invoke([](){std::cout << "hello1\n"; });
    Invoke([](){std::cout << "hello2\n"; });
    InvokeTemplated([]<typename T>() {std::cout << typeid(T).name() << "\n";});
}

Thank you for the report, @mtnpke.
I agree that we should provide an exception for passing a functor by forwarding references consistently, regardless of call syntax. I have created the ticket for the issue CPP-5507 that you can use track the prgoress.