<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58037>58037</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang 15.0.1: clang-tidy check misc-const-correctness finds false-positive involving template function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          sapi33
      </td>
    </tr>
</table>

<pre>
    The following code triggers a false-positive for the clang-tidy check misc-const-correctness using clang version 15.0.1:
```c++
#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:
```console
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:
```console
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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVMFy4ygQ_RrpQkklg2VZBx0SO6nac7I1c0sh1JLZYFABciZ_vw2SHSfj7G6tCsuCbprX_R7dmu69eT4A6Y1S5k3qgQjTAfFWDgNYRzjpuXKQjcZJL0_B0RKPG4Tiesi87N6JOIB4JUfpRCaMdh7f1oLwGpwjk4tBgzc5YURpNFmVeZGvEnaXFPukuEs2xTxEQu_DmFcpk1qoCdEkbOcQkR4S9nDTiFbgxw9rfHs4jop7QAf_PoLmRyDPFx_nuwCA3c2RiQfnHyctPAJM6DYmgu50Q8YXnHguNdiE1ssJ1YKS4HMJZeZYMxpiwU3K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