Environment:
SonarLint 5.3.0.36775 SonarSource
Clion 2021.1.3
Ubuntu 20.04.3 LTS
False positive on code. Sonar tells to remove the explicit return type and rely on implicit return types.
However lambdas will never return references without explicit return types. If you want to avoid the expensive copy on some object and return a reference you are forced to use explicit return types.
Sonar should only suggest implicit return type if the return type is neither a reference, nor decltype(auto).
#include <vector>
#include <functional>
class LargeClass // avoid copying instances of this class if you can
{
};
std::vector<LargeClass> someVec;
auto getItem = [&someVec](size_t index) -> const LargeClass& { return someVec.at(index); };
std::function<const LargeClass&(size_t)> typeErasedGetter = getItem;
someFunction(typeErasedGetter);