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

use auto for iterators #6969

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

use auto for iterators #6969

wants to merge 2 commits into from

Conversation

firewave
Copy link
Collaborator

No description provided.

@firewave firewave force-pushed the auto_it branch 2 times, most recently from c9b94be to ef37224 Compare October 30, 2024 21:50
@firewave
Copy link
Collaborator Author

/home/runner/work/cppcheck/cppcheck/lib/utils.h:412:16: error: returning a constant reference parameter may cause use-after-free when the parameter is constructed from a temporary [bugprone-return-const-ref-from-parameter,-warnings-as-errors]
  412 |         return t;
      |                ^
#include <set>
#include <type_traits>

namespace utils {
    template<class T>
    constexpr typename std::add_const<T>::type & as_const(T& t) noexcept
    {
        return t;
    }
}

struct C
{
    const int* f() const
    {
        const auto it = utils::as_const(v).find(0);
        if (it == v.cend())
            return nullptr;
        return &(*it);
    }

    std::set<int> v;
};

https://godbolt.org/z/91qxYYxcz

If you remove the const from the method the warning goes away.

This happens because we try as_const() something that is already const. That is intentional though.

It seems like this might just be a false positive since everything seems to work fine and the CI is happy otherwise.

@firewave
Copy link
Collaborator Author

These changes exposed a unreadVariable false negative: https://trac.cppcheck.net/ticket/13303.

@firewave
Copy link
Collaborator Author

This is ready for review but keeping a draft so it does not interfere with the ValueFlow revert.

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

Successfully merging this pull request may close these issues.

1 participant