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

clang 15.0.1: misc-const-correctness mislead by template function yields false-positive for uninvolved variable

Open sapi33 opened this issue 4 years ago • 2 comments

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.

sapi33 avatar Sep 29 '22 10:09 sapi33

@llvm/issue-subscribers-clang-tidy

llvmbot avatar Sep 29 '22 12:09 llvmbot

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.

vbvictor avatar May 23 '26 21:05 vbvictor