你能帮我如何避免这段代码中的警告吗...
警告: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 提供的警告
警告是关于您在
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;
}