<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/127494>127494</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] misc-const-correctness false positive when using std::function default parameter
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy,
false-positive
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pkaff
</td>
</tr>
</table>
<pre>
```
#include <functional>
#include <sstream>
void func(std::function<void()> predicate = []() {})
{
static_cast<void>(predicate);
}
int main(int const /*argn*/, char * /*argv*/[])
{
std::ostringstream oss;
oss << "hello world";
func();
return 0;
}
```
Building this on Clang 19.1.7 arm64-apple-darwin24.2.0 (MacOS Sequoia 15.2) triggers a false positive for the check misc-const-correctness on the ostringstream variable:
```
error: variable 'oss' of type 'std::ostringstream' (aka 'basic_ostringstream<char>') can be declared 'const' [misc-const-correctness,-warnings-as-errors]
[build] 11 | std::ostringstream oss;
[build] | ^
[build] | const
```
while adding `const` clearly causes a compilation error.
I'm not able to reproduce this on godbolt with x86-64 clang (trunk) (or any of the older versions I tried either), so it seems like this might be MacOs specific: https://godbolt.org/z/9e47zxbcT.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVM2u2zgPfRpmQzhw6N8ssnCSG6CLD9-isy8UiYk1V7Y8kpz09ukHUtz71xYDBLAjkseHh4cS3uvryLyDag_VcSXm0Fu3m57F5bI6W_WygzpffnkHVOhRmlkxQnG4zKMM2o7CQPH0S9T74FgMSyjvblYrjCVArQ8Kig6K7icEFIcYB2qBtlA84eRYaSlChDrig9wjitDsoTnGvLyL73mHiOiDCFp-k8KHn2DFE1D7CpSA96nm-GCkx4CD0CNQG1-lHX1AoBNQJ9x1BOrSnwPKXjgE6t6CtyW48PpMZenO-uD0eH0Igdb7B4GYY72PIkFxQCDq2RiLd-uMAqKF5iNxUeyN_ePYcZjdiPmHlt5Naj9ro_R4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