llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang 15.0.1: clang-tidy check misc-const-correctness finds false-positive involving template function

Open sapi33 opened this issue 4 years ago • 2 comments

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.

sapi33 avatar Sep 28 '22 10:09 sapi33

Duplicate of #57297.

firewave avatar Sep 28 '22 11:09 firewave

@llvm/issue-subscribers-clang-tidy

llvmbot avatar Sep 28 '22 13:09 llvmbot