Issue 179941
Summary clang-tidy readability-use-anyofallof fails to consider dereference through shared_ptr to be a mutation
Labels clang-tidy
Assignees
Reporter PJBoy
    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 <memory>

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<S> (&xs)[1])
{
 for (std::unique_ptr<S>& x : xs)
        if (x->check())
        {
 x->mutate();
            return true;
        }

    return false;
}

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

    return false;
}
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to