| 1864 | } |
| 1865 | |
| 1866 | void CheckStlImpl::missingComparison() |
| 1867 | { |
| 1868 | if (!mSettings.severity.isEnabled(Severity::warning)) |
| 1869 | return; |
| 1870 | |
| 1871 | logChecker("CheckStl::missingComparison"); // warning |
| 1872 | |
| 1873 | const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase(); |
| 1874 | |
| 1875 | for (const Scope &scope : symbolDatabase->scopeList) { |
| 1876 | if (scope.type != ScopeType::eFor || !scope.classDef) |
| 1877 | continue; |
| 1878 | |
| 1879 | for (const Token *tok2 = scope.classDef->tokAt(2); tok2 != scope.bodyStart; tok2 = tok2->next()) { |
| 1880 | if (tok2->str() == ";") |
| 1881 | break; |
| 1882 | |
| 1883 | if (!Token::Match(tok2, "%var% = %name% . begin|rbegin|cbegin|crbegin ( ) ; %name% != %name% . end|rend|cend|crend ( ) ; ++| %name% ++| ) {")) |
| 1884 | continue; |
| 1885 | |
| 1886 | // same container |
| 1887 | if (tok2->strAt(2) != tok2->strAt(10)) |
| 1888 | break; |
| 1889 | |
| 1890 | const int iteratorId(tok2->varId()); |
| 1891 | |
| 1892 | // same iterator |
| 1893 | if (iteratorId == tok2->tokAt(10)->varId()) |
| 1894 | break; |
| 1895 | |
| 1896 | // increment iterator |
| 1897 | if (!Token::Match(tok2->tokAt(16), "++ %varid% )", iteratorId) && |
| 1898 | !Token::Match(tok2->tokAt(16), "%varid% ++ )", iteratorId)) { |
| 1899 | break; |
| 1900 | } |
| 1901 | |
| 1902 | const Token *incrementToken = nullptr; |
| 1903 | // Parse loop.. |
| 1904 | for (const Token *tok3 = scope.bodyStart; tok3 != scope.bodyEnd; tok3 = tok3->next()) { |
| 1905 | if (tok3->varId() == iteratorId) { |
| 1906 | if (Token::Match(tok3, "%varid% = %name% . insert ( ++| %varid% ++| ,", iteratorId)) { |
| 1907 | // skip insertion.. |
| 1908 | tok3 = tok3->linkAt(6); |
| 1909 | if (!tok3) |
| 1910 | break; |
| 1911 | } else if (Token::simpleMatch(tok3->astParent(), "++")) |
| 1912 | incrementToken = tok3; |
| 1913 | else if (Token::simpleMatch(tok3->astParent(), "+")) { |
| 1914 | if (Token::Match(tok3->astSibling(), "%num%")) { |
| 1915 | const Token* tokenGrandParent = tok3->astParent()->astParent(); |
| 1916 | if (Token::Match(tokenGrandParent, "==|!=")) |
| 1917 | break; |
| 1918 | } |
| 1919 | } else if (Token::Match(tok3->astParent(), "==|!=")) |
| 1920 | incrementToken = nullptr; |
| 1921 | } else if (tok3->str() == "break" || tok3->str() == "return") |
| 1922 | incrementToken = nullptr; |
| 1923 | } |