Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] modernize-use-ranges does not preserve constness #110599

Open
felix642 opened this issue Sep 30, 2024 · 0 comments
Open

[clang-tidy] modernize-use-ranges does not preserve constness #110599

felix642 opened this issue Sep 30, 2024 · 0 comments

Comments

@felix642
Copy link
Contributor

The fix-it for modernize-use-ranges changes the constness on some function calls.
Let's assume the following example :

void foo(int& a);
void foo(const int& a);

struct Foo
{
    std::vector<int> a;

    void print() {
       std::for_each(a.cbegin(), a.cend(), [](auto &x) {
        foo(x);
    });

    }
};

The method currently calls void foo(const int& a) since we are using cbegin() and cend(), but after applying the fix-it the code looks like this :

void foo(int& a);
void foo(const int& a);

struct Foo
{
    std::vector<int> a;

    void print() {
       std::ranges::for_each(a, [](auto &x) {
        foo(x);
    });

    }
};

Which will call void foo(int &a) since we have removed the constness from the iterators.
To properly fix the issue the fix-it should add as_const around the range and include <utiity> if the user was previously using cbegin() and cend().

godbolt: https://godbolt.org/z/MPnoo4vGY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant