clang 15.0.1: misc-const-correctness mislead by template function yields false-positive for uninvolved variable
Due to findings in our code we created a small example which demonstrates that the clang-tidy check misc-const-correctness emits a false-positive warning for a variable which is not involved in the template function:
#include <functional>
template<typename T>
void function(const std::function<void (const T&)> p_function=[](const T&) { /* do nothing */ })
{
const T value = T();
if (p_function)
p_function(value);
}
int main()
{
unsigned int value=0U;
value=1U;
function<float>(); // warning regarding 'value'
//function<float>(nullptr); // no warning
return EXIT_SUCCESS;
}
This is the warning:
<source>:13:5: warning: variable 'value' of type 'unsigned int' can be declared 'const' [misc-const-correctness]](javascript:;)
unsigned int value=0U;
^
const
The example can bee seen/tested here: https://godbolt.org/z/aYrnn3sbr
If function() is called with nullptr as argument then no warning is emitted.
Note that in our case we got a false-positive warning for each non-const variable inside the method, i.e. all were affected.
@llvm/issue-subscribers-clang-tidy
This currently no longer repro on trunk: https://godbolt.org/z/eYxdY95T1. I suppose it was fixed in some release between 15 and 23. I will close this issue for now , please feel free to reopen it with updated reproducer in godbolt.