llvm-project
llvm-project copied to clipboard
clang 15.0.1: clang-tidy check misc-const-correctness finds false-positive involving template function
The following code triggers a false-positive for the clang-tidy check misc-const-correctness using clang version 15.0.1:
#include <string>
#include <sstream>
template<typename T>
std::string testFunction(const T& p_container)
{
std::ostringstream result;
for (typename T::const_iterator it=p_container.begin();it!=p_container.end();++it)
result << *it;
return result.str();
}
int main()
{
return EXIT_SUCCESS;
}
The std::ostringstream result is falsely detected as not being altered and hence the warning to declare it as const:
main.cpp:7:5: warning: variable 'result' of type 'std::ostringstream' (aka 'basic_ostringstream<char>') can be declared 'const' [misc-const-correctness]
std::ostringstream result;
^
const
We use libc++ and invoke clang-tidy with a command like this:
clang-tidy --extra-arg-before=-stdlib=libc++ -checks=-*,misc-const-correctness main.cpp
We encountered a similar case, though in a template class, but were so far not able to derive a suitable example.
Duplicate of #57297.
@llvm/issue-subscribers-clang-tidy