c++ 警告:Clang-Tidy:条件链中重复分支

问题描述 投票:0回答:1

你能帮我如何避免这段代码中的警告吗...

警告:Clang-Tidy:条件链中重复分支

HubInterface *getFreeHub() {
        HubInterface *freeHub = nullptr;
        while (freeHub == nullptr) {
            for (auto hub : this->getHubList()) {
                if (freeHub == nullptr) {
                    freeHub = hub;
                } else if (hub->getStatus() == STATUS_READY &&
                           hub->getBusyNodes().size() < freeHub->getBusyNodes().size()) {
                    freeHub = hub;
                }
            }
            sleep(5);
        }
        return freeHub;
    }

CLion IDE 提供的警告

c++ c++14 clion clang-tidy
1个回答
11
投票

警告是关于您在

if
else if
分支中做同样的事情,即
freeHub = hub;

if (freeHub == nullptr) {
    freeHub = hub;
} else if (hub->getStatus() == STATUS_READY &&
           hub->getBusyNodes().size() < freeHub->getBusyNodes().size()) {
    freeHub = hub; // same as above, what is the point?
}

我想你可以将其重写为单个条件,但它变得有点可怕:

if (freeHub == nullptr ||
    (hub->getStatus() == STATUS_READY &&
     hub->getBusyNodes().size() < freeHub->getBusyNodes().size())) {
    freeHub = hub;
}
© www.soinside.com 2019 - 2024. All rights reserved.