llvm / llvm/llvm-project

clang-tidy readability-use-anyofallof fails to consider dereference through shared_ptr to be a mutation

Open
#179,941 3 comments 0 reactions 0 assignees View on GitHub
clang-tidy false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The `readability-use-anyofallof` check is supposed to suppress its diagnosis if it detects a mutation of the loop variable. In the case of the loop variable being a pointer, calling a non-const member function is enough to be considered a mutation. If the loop variable is instead a `std::shared_ptr`, it is not. Curiously, if the loop variable is a `std::unique_ptr`, the indirect member function call is correctly considered to be a mutation (even though `std::unique_ptr::operator->()` is const qualified like `std::shared_ptr`).

Testcase https://godbolt.org/z/eWE5MKnWY
```cpp
#include

struct S
{
void mutate() {}
bool check() const { return true; }
};

bool f_good_raw(S* (&xs)[1])
{
for (S* x : xs)
if (x->check())
{
x->mutate();
return true;
}

return false;
}

bool f_good_unique(std::unique_ptr (&xs)[1])
{
for (std::unique_ptr& x : xs)
if (x->check())
{
x->mutate();
return true;
}

return false;
}

bool f_bad(std::shared_ptr (&xs)[1])
{
for (std::shared_ptr& x : xs)
if (x->check())
{
x->mutate();
return true;
}

return false;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.