There is a dynamic list search function that writes pointers to those elements in which the value of the selected data field matches the search query. According to the results of the work, it should return true if the vector has at least one element, or false if the vector is empty. The program, in the scenarios I have conceived, works correctly, but the compiler's warning (Visual Studio Community 2019) strains me warning C4715: DynamicList::search: значение возвращается не при всех путях выполнения . I just can not understand why he swears, because after the end of the cycle, the function continues its execution. Is this because of the looped conditions?

 bool DynamicList::search(unsigned int search_key, unsigned int key_numeric) { DynamicListElement *current_element = this->first_element; while (current_element != nullptr) { if (search_key == NUMBER) { if (current_element->getNumber() == key_numeric) { this->search_buffer.push_back(current_element); //std::vector<DynamicListElement*> search_buffer } } current_element = current_element->getNextElement(); } if (this->search_buffer.size() == 0) { current_element = nullptr; return false; } else if (this->search_buffer.size() > 0) { current_element = nullptr; return true; } } 

I also tried using the .empty() method in conditions, but the warning persisted.

UPD: Used return !this->search_buffer.empty(); instead of both conditions at the end.

  • Return put at the end of the function. - Andrey Sv

3 answers 3

Rewrite

 if (this->search_buffer.size() == 0) { current_element = nullptr; return false; } else if (this->search_buffer.size() > 0) { current_element = nullptr; return true; } 

as

 current_element = nullptr; return this->search_buffer.size() > 0; 

If, of course, you have this->search_buffer.size() not a symbolic one and cannot return a negative value :)

  • Beautiful decision! Thanks, now I know that this is possible. No, search_buffer is of type std :: vector . - P1xelGuarD 8:08 pm
  • Well ... I wrote almost the same thing first, and only then noticed this answer. The only difference is that my size implicitly applies to bool. - Qwertiy pm
  • @Qwertiy By the way, even shorter :) - Harry
 if (this->search_buffer.size() == 0) { current_element = nullptr; return false; } else if (this->search_buffer.size() > 0) { current_element = nullptr; return true; } 
 current_element = nullptr; return this->search_buffer.size(); 
     else if (this->search_buffer.size() > 0) // условие не выполняется { current_element = nullptr; return true; } // попадаем сюда и функция заканчивается 

    Logically, if the buffer size type is unsigned, then this second condition is redundant. It makes sense to redo the first condition into if(this->search_buffer.empty()) , and it’s best to get rid of unnecessary branching altogether:

     current_element = nullptr; return not this->search_buffer.empty(); 
    • size () can return less than 0? - P1xelGuarD 7:58 pm
    • @ P1xelGuarD theoretically does not prevent size() returning 0 when it is called again, but I do not think that the compiler is analyzing in depth - VTT
    • That is, in fact, is enough for If 'and for size ()> 0 and the subsequent else ? Got it - P1xelGuarD
    • I have an overloaded function for another data type of a search query that uses a similar construct, but with .empty () == true and .empty () == false , but the visual is scolding it. Although, in essence, .empty () cannot return anything other than these two values. : / - P1xelGuarD 8:04 pm
    • @ P1xelGuarD This single call can return only one of two values, and there is no reason to believe that a repeated call should return the same as the first one, the compiler may not have enough - VTT