diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 7ed0f6a69..b08579e7b 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -401,7 +401,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) } // No "=", append a "=1" - if (define.find("=") == std::string::npos) + if (define.find('=') == std::string::npos) define += "=1"; if (!_settings->userDefines.empty()) diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 6dea55dc6..c669d4ff2 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -53,7 +53,7 @@ static bool astGetSizeSign(const Settings *settings, const Token *tok, unsigned return !tok->astOperand2() || astGetSizeSign(settings, tok->astOperand2(), size, sign); } if (tok->isNumber() && MathLib::isInt(tok->str())) { - if (tok->str().find("L") != std::string::npos) + if (tok->str().find('L') != std::string::npos) return false; MathLib::bigint value = MathLib::toLongNumber(tok->str()); unsigned int sz; diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index ffd470ff4..a2583e3d1 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1224,24 +1224,24 @@ void CheckUnusedVar::checkStructMemberUsage() structname.clear(); if (!structname.empty() && Token::Match(tok, "[{;]")) { - // Declaring struct variable.. - std::string varname; - // declaring a POD variable? if (!tok->next()->isStandardType()) continue; + // Declaring struct variable.. + const std::string* varname; + if (Token::Match(tok->next(), "%type% %name% [;[]")) - varname = tok->strAt(2); + varname = &tok->strAt(2); else if (Token::Match(tok->next(), "%type% %type%|* %name% [;[]")) - varname = tok->strAt(3); + varname = &tok->strAt(3); else if (Token::Match(tok->next(), "%type% %type% * %name% [;[]")) - varname = tok->strAt(4); + varname = &tok->strAt(4); else continue; // Check if the struct variable is used anywhere in the file - const std::string usagePattern(". " + varname); + const std::string usagePattern(". " + *varname); bool used = false; for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next()) { if (Token::simpleMatch(tok2, usagePattern.c_str())) { @@ -1251,7 +1251,7 @@ void CheckUnusedVar::checkStructMemberUsage() } if (! used) { - unusedStructMemberError(tok->next(), structname, varname); + unusedStructMemberError(tok->next(), structname, *varname); } } } diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 3b59f83f4..488112748 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -88,7 +88,7 @@ void ErrorLogger::ErrorMessage::setmsg(const std::string &msg) // The summary and verbose message are separated by a newline // If there is no newline then both the summary and verbose messages // are the given message - const std::string::size_type pos = msg.find("\n"); + const std::string::size_type pos = msg.find('\n'); if (pos == std::string::npos) { _shortMessage = msg; _verboseMessage = msg; diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index f36707df5..9ef2b2af1 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -397,7 +397,7 @@ template<> std::string MathLib::toString(double value) result << value; if (result.str() == "-0") return "0.0"; - if (result.str().find(".") == std::string::npos) + if (result.str().find('.') == std::string::npos) return result.str() + ".0"; return result.str(); } diff --git a/lib/settings.cpp b/lib/settings.cpp index a867b93af..89b0593a4 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -78,10 +78,10 @@ namespace { std::string Settings::addEnabled(const std::string &str) { // Enable parameters may be comma separated... - if (str.find(",") != std::string::npos) { + if (str.find(',') != std::string::npos) { std::string::size_type prevPos = 0; std::string::size_type pos = 0; - while ((pos = str.find(",", pos)) != std::string::npos) { + while ((pos = str.find(',', pos)) != std::string::npos) { if (pos == prevPos) return std::string("cppcheck: --enable parameter is empty"); const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos))); diff --git a/lib/suppressions.cpp b/lib/suppressions.cpp index 03d7efc7f..1529ff881 100644 --- a/lib/suppressions.cpp +++ b/lib/suppressions.cpp @@ -66,11 +66,11 @@ std::string Suppressions::addSuppressionLine(const std::string &line) // is a line number.. // Get position of last colon - const std::string::size_type pos = file.rfind(":"); + const std::string::size_type pos = file.rfind(':'); // if a colon is found and there is no dot after it.. if (pos != std::string::npos && - file.find(".", pos) == std::string::npos) { + file.find('.', pos) == std::string::npos) { // Try to parse out the line number try { std::istringstream istr1(file.substr(pos+1)); diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 5ffde1d93..bdab26afb 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3192,8 +3192,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const if (MathLib::isInt(arguments[j]->str())) { if (arguments[j]->str().find("ll") != std::string::npos || arguments[j]->str().find("LL") != std::string::npos) { - if (arguments[j]->str().find("u") != std::string::npos || - arguments[j]->str().find("U") != std::string::npos) { + if (arguments[j]->str().find('u') != std::string::npos || + arguments[j]->str().find('U') != std::string::npos) { if (funcarg->typeStartToken()->str() == "long" && funcarg->typeStartToken()->isLong() && funcarg->typeStartToken()->isUnsigned()) { @@ -3206,10 +3206,10 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const same++; } } - } else if (arguments[j]->str().find("l") != std::string::npos || - arguments[j]->str().find("L") != std::string::npos) { - if (arguments[j]->str().find("u") != std::string::npos || - arguments[j]->str().find("U") != std::string::npos) { + } else if (arguments[j]->str().find('l') != std::string::npos || + arguments[j]->str().find('L') != std::string::npos) { + if (arguments[j]->str().find('u') != std::string::npos || + arguments[j]->str().find('U') != std::string::npos) { if (funcarg->typeStartToken()->str() == "long" && !funcarg->typeStartToken()->isLong() && funcarg->typeStartToken()->isUnsigned()) { @@ -3222,8 +3222,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const same++; } } - } else if (arguments[j]->str().find("u") != std::string::npos || - arguments[j]->str().find("U") != std::string::npos) { + } else if (arguments[j]->str().find('u') != std::string::npos || + arguments[j]->str().find('U') != std::string::npos) { if (funcarg->typeStartToken()->str() == "int" && funcarg->typeStartToken()->isUnsigned()) { same++; @@ -3239,13 +3239,13 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const } } } else { - if (arguments[j]->str().find("f") != std::string::npos || - arguments[j]->str().find("F") != std::string::npos) { + if (arguments[j]->str().find('f') != std::string::npos || + arguments[j]->str().find('F') != std::string::npos) { if (funcarg->typeStartToken()->str() == "float") { same++; } - } else if (arguments[j]->str().find("l") != std::string::npos || - arguments[j]->str().find("L") != std::string::npos) { + } else if (arguments[j]->str().find('l') != std::string::npos || + arguments[j]->str().find('L') != std::string::npos) { if (funcarg->typeStartToken()->str() == "double" && funcarg->typeStartToken()->isLong()) { same++; @@ -3344,8 +3344,8 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const if (currScope) { while (currScope && !Token::Match(tok1, "%type% :: %any% (")) { - currScope = currScope->findRecordInNestedList(tok1->strAt(2)); tok1 = tok1->tokAt(2); + currScope = currScope->findRecordInNestedList(tok1->str()); } tok1 = tok1->tokAt(2); diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index b22643c74..a9cccb678 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -943,7 +943,7 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok) while (tok->tokAt(4) && tok->next()->isNumber() && tok->tokAt(3)->isNumber()) { // %any% %num% %any% %num% %any% const Token* op = tok->tokAt(2); const Token* after = tok->tokAt(4); - if (Token::Match(tok, "* %num% /") && (tok->strAt(3) != "0") && tok->next()->str() == MathLib::multiply(tok->strAt(3), MathLib::divide(tok->next()->str(), tok->strAt(3)))) { + if (Token::Match(tok, "* %num% /") && (op->strAt(1) != "0") && tok->next()->str() == MathLib::multiply(op->strAt(1), MathLib::divide(tok->next()->str(), op->strAt(1)))) { // Division where result is a whole number } else if (!((op->str() == "*" && (isLowerThanMulDiv(tok) || tok->str() == "*") && isLowerEqualThanMulDiv(after)) || // associative (Token::Match(op, "[/%]") && isLowerThanMulDiv(tok) && isLowerEqualThanMulDiv(after)) || // NOT associative diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 276f16f01..b6286988a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1506,14 +1506,14 @@ void Tokenizer::simplifyTypedef() } } else if (typeOf) { tok2 = copyTokens(tok2, argStart, argEnd); - } else if (tok2->tokAt(2) && tok2->strAt(2) == "[") { - while (tok2->tokAt(2) && tok2->strAt(2) == "[") { + } else if (tok2->strAt(2) == "[") { + do { if (!tok2->linkAt(2)) { syntaxError(tok2); // #6807 return; } tok2 = tok2->linkAt(2)->previous(); - } + } while (tok2->strAt(2) == "["); } if (arrayStart && arrayEnd) { @@ -2864,9 +2864,10 @@ void Tokenizer::setVarId() if (!isC()) { for (Token *tok2 = list.front(); tok2; tok2 = tok2->next()) { if (Token::Match(tok2, "%name% :: %name%")) { - if (tok2->strAt(3) == "(") + const std::string& str3 = tok2->strAt(3); + if (str3 == "(") allMemberFunctions.push_back(tok2); - else if (tok2->strAt(3) != "::" && tok2->strAt(-1) != "::") // Support only one depth + else if (str3 != "::" && tok2->strAt(-1) != "::") // Support only one depth allMemberVars.push_back(tok2); } }