MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / valueFlowContainerSize

Method valueFlowContainerSize

test/testvalueflow.cpp:6641–7540  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6639 }
6640
6641 void valueFlowContainerSize() {
6642 const char *code;
6643
6644 // condition
6645 code = "void f(const std::list<int> &ints) {\n"
6646 " if (!static_cast<bool>(ints.empty()))\n"
6647 " ints.front();\n"
6648 "}";
6649 ASSERT_EQUALS("", isImpossibleContainerSizeValue(tokenValues(code, "ints . front"), 0));
6650
6651 // valueFlowContainerReverse
6652 code = "void f(const std::list<int> &ints) {\n"
6653 " ints.front();\n" // <- container can be empty
6654 " if (ints.empty()) {}\n"
6655 "}";
6656 ASSERT_EQUALS("", isPossibleContainerSizeValue(tokenValues(code, "ints . front"), 0));
6657
6658 code = "void f(const std::list<int> &ints) {\n"
6659 " ints.front();\n" // <- container can be empty
6660 " if (ints.size()==0) {}\n"
6661 "}";
6662 ASSERT_EQUALS("", isPossibleContainerSizeValue(tokenValues(code, "ints . front"), 0));
6663
6664 code = "void f(std::list<int> ints) {\n"
6665 " ints.front();\n" // <- no container size
6666 " ints.pop_back();\n"
6667 " if (ints.empty()) {}\n"
6668 "}";
6669 ASSERT_EQUALS("", isPossibleContainerSizeValue(tokenValues(code, "ints . front"), 1));
6670
6671 code = "void f(std::vector<int> v) {\n"
6672 " v[10] = 0;\n" // <- container size can be 10
6673 " if (v.size() == 10) {}\n"
6674 "}";
6675 ASSERT_EQUALS("", isPossibleContainerSizeValue(tokenValues(code, "v ["), 10));
6676
6677 code = "void f(std::vector<std::string> params) {\n"
6678 " switch(x) {\n"
6679 " case CMD_RESPONSE:\n"
6680 " if(y) { break; }\n"
6681 " params[2];\n" // <- container use
6682 " break;\n"
6683 " case CMD_DELETE:\n"
6684 " if (params.size() < 2) { }\n" // <- condition
6685 " break;\n"
6686 " }\n"
6687 "}";
6688 ASSERT(tokenValues(code, "params [ 2 ]").empty());
6689
6690 // valueFlowAfterCondition
6691 code = "void f(const std::vector<std::string>& v) {\n"
6692 " if(v.empty()) {\n"
6693 " v.front();\n"
6694 " }\n"
6695 "}\n";
6696 ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "v . front"), 0));
6697
6698 code = "void f(const std::vector<std::string>& v) {\n"

Callers

nothing calls this directly

Calls 4

removeImpossibleFunction · 0.85
frontMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected