6

Can you help me how to avoid the warning in this code ...

Warning: Clang-Tidy: Repeated branch in conditional chain

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;
    }

Warning provided by CLion IDE

0

1 Answer 1

10

The warning is about the fact that you are doing the same thing in your if and else if branches, which is 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?
}

I suppose you could rewrite it as a single condition but it's getting a bit scary:

if (freeHub == nullptr ||
    (hub->getStatus() == STATUS_READY &&
     hub->getBusyNodes().size() < freeHub->getBusyNodes().size())) {
    freeHub = hub;
}
3
  • there ara many hubs, I need a hub with the status of "READY" and the least number of occupied nodes...
    – mat.twg
    Feb 29, 2020 at 14:16
  • @mat.twg, I understand, but according to your code you don't check those conditions when freeHub is null, right?
    – r3mus n0x
    Feb 29, 2020 at 14:18
  • Thank you very much! I was updated code, now all seems ok... also additional conditions added.
    – mat.twg
    Feb 29, 2020 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.