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

MenuManager: modernize for loops #14033

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PaddleStroke
Copy link
Contributor

MenuManager: modernize for loops

@github-actions github-actions bot added the Core Issue or PR touches core sections (App, Gui, Base) of FreeCAD label May 15, 2024
@sliptonic sliptonic modified the milestone: 1.0 May 15, 2024
@chennes
Copy link
Member

chennes commented May 17, 2024

Can you check your const-correctness here? In at least a couple cases you've lost the iterator const-ness, and a quick glance through the loops makes it look like more of them could be const as well. We should silence those linter errors either by ensuring that the implicit sharing doesn't make copies, or if that's not a problem, by telling the linter to not display that error.

@PaddleStroke
Copy link
Contributor Author

There are 2 for loops that were using ConstIterator, one of these :

QAction* MenuManager::findAction(const QList<QAction*>& acts, const QString& item) const
{
    for (QList<QAction*>::ConstIterator it = acts.begin(); it != acts.end(); ++it) {
        if ((*it)->data().toString() == item) {
            return *it;
        }
    }

    return nullptr; // no item with the user data found
}

However its the iterator that is const, not the action. So you cannot change to :

QAction* MenuManager::findAction(const QList<QAction*>& acts, const QString& item) const
{
    for (const auto* action : acts) {
        if (action->data().toString() == item) {
            return action;
        }
    }

    return nullptr; // no item with the user data found
}

Because then the return type is not correct anymore. Now it needs to be const QAction* which may break something downstream if those functions expect a non-const QAction*. Fixing const-correctness accross multiple files is outside the scope of this tiny PR that only want to simplify the syntax I came accross.

I do not understand the Lint warnings. However I only changed the syntax to newer type of for loop. So I do not see how it could create actual issues.

@wwmayer
Copy link
Contributor

wwmayer commented May 18, 2024

The linter warnings are not about the iterator variable but about the container where unnecessarily a deep copy could be created: c++11 range-loop might detach Qt container (QList) [-Wclazy-range-loop-detach]

To fix this warning the line
for (auto* item : _items) {
should be changed to
for (auto* item : std::as_const(_items)) {

@PaddleStroke
Copy link
Contributor Author

I have changed to :
for (auto& item : _items) {

I think this syntax is preventing the deep copy issue and looks easier on the eye than the std::as_const.

Can you please let me know if this is ok or if it is better to use std::as_const?
Thanks

@PaddleStroke
Copy link
Contributor Author

@wwmayer ?

@wwmayer
Copy link
Contributor

wwmayer commented May 27, 2024

Using the reference seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Issue or PR touches core sections (App, Gui, Base) of FreeCAD
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants