From efaaa58896719d18e1a6627c4bb18e95c627091a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 28 Jul 2022 22:53:59 +0200 Subject: [PATCH] fixed some `modernize-use-emplace` false negatives and some `bugprone-assignment-in-if-condition` warnings (#4311) --- cli/processexecutor.cpp | 4 ++-- lib/checkcondition.cpp | 12 ++++++------ lib/checkother.cpp | 8 ++++---- lib/checkstl.cpp | 4 ++-- lib/preprocessor.cpp | 4 ++-- lib/symboldatabase.cpp | 6 ++---- lib/templatesimplifier.cpp | 2 +- lib/tokenize.cpp | 4 ++-- lib/valueflow.cpp | 4 ++-- test/testsuite.cpp | 4 ++-- 10 files changed, 25 insertions(+), 27 deletions(-) diff --git a/cli/processexecutor.cpp b/cli/processexecutor.cpp index 439fac187..47e7cb508 100644 --- a/cli/processexecutor.cpp +++ b/cli/processexecutor.cpp @@ -237,8 +237,8 @@ unsigned int ProcessExecutor::check() std::exit(EXIT_FAILURE); } - int flags = 0; - if ((flags = fcntl(pipes[0], F_GETFL, 0)) < 0) { + int flags = fcntl(pipes[0], F_GETFL, 0); + if (flags < 0) { std::cerr << "#### ThreadExecutor::check, fcntl(F_GETFL) failed: "<< std::strerror(errno) << std::endl; std::exit(EXIT_FAILURE); } diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index fdef5c65f..fcb9f88ed 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -821,8 +821,8 @@ void CheckCondition::oppositeInnerConditionError(const Token *tok1, const Token* const std::string s1(tok1 ? tok1->expressionString() : "x"); const std::string s2(tok2 ? tok2->expressionString() : "!x"); const std::string innerSmt = innerSmtString(tok2); - errorPath.emplace_back(ErrorPathItem(tok1, "outer condition: " + s1)); - errorPath.emplace_back(ErrorPathItem(tok2, "opposite inner condition: " + s2)); + errorPath.emplace_back(tok1, "outer condition: " + s1); + errorPath.emplace_back(tok2, "opposite inner condition: " + s2); const std::string msg("Opposite inner '" + innerSmt + "' condition leads to a dead code block.\n" "Opposite inner '" + innerSmt + "' condition leads to a dead code block (outer condition is '" + s1 + "' and inner condition is '" + s2 + "')."); @@ -836,8 +836,8 @@ void CheckCondition::identicalInnerConditionError(const Token *tok1, const Token const std::string s1(tok1 ? tok1->expressionString() : "x"); const std::string s2(tok2 ? tok2->expressionString() : "x"); const std::string innerSmt = innerSmtString(tok2); - errorPath.emplace_back(ErrorPathItem(tok1, "outer condition: " + s1)); - errorPath.emplace_back(ErrorPathItem(tok2, "identical inner condition: " + s2)); + errorPath.emplace_back(tok1, "outer condition: " + s1); + errorPath.emplace_back(tok2, "identical inner condition: " + s2); const std::string msg("Identical inner '" + innerSmt + "' condition is always true.\n" "Identical inner '" + innerSmt + "' condition is always true (outer condition is '" + s1 + "' and inner condition is '" + s2 + "')."); @@ -854,8 +854,8 @@ void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, c const std::string cond(cond1 ? cond1->expressionString() : "x"); const std::string value = (cond2 && cond2->valueType() && cond2->valueType()->type == ValueType::Type::BOOL) ? "false" : "0"; - errorPath.emplace_back(ErrorPathItem(cond1, "If condition '" + cond + "' is true, the function will return/exit")); - errorPath.emplace_back(ErrorPathItem(cond2, (isReturnValue ? "Returning identical expression '" : "Testing identical condition '") + cond + "'")); + errorPath.emplace_back(cond1, "If condition '" + cond + "' is true, the function will return/exit"); + errorPath.emplace_back(cond2, (isReturnValue ? "Returning identical expression '" : "Testing identical condition '") + cond + "'"); reportError(errorPath, Severity::warning, diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b9796268b..a68f0dfdb 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1635,9 +1635,9 @@ void CheckOther::constVariableError(const Variable *var, const Function *functio ErrorPath errorPath; std::string id = "const" + vartype; std::string message = "$symbol:" + varname + "\n" + vartype + " '$symbol' can be declared as " + ptrRefArray; - errorPath.push_back(ErrorPathItem(var ? var->nameToken() : nullptr, message)); + errorPath.emplace_back(var ? var->nameToken() : nullptr, message); if (var && var->isArgument() && function && function->functionPointerUsage) { - errorPath.push_front(ErrorPathItem(function->functionPointerUsage, "You might need to cast the function pointer here")); + errorPath.emplace_front(function->functionPointerUsage, "You might need to cast the function pointer here"); id += "Callback"; message += ". However it seems that '" + function->name() + "' is a callback function, if '$symbol' is declared with const you might also need to cast function pointer(s)."; } @@ -3467,8 +3467,8 @@ void CheckOther::checkShadowVariables() void CheckOther::shadowError(const Token *var, const Token *shadowed, std::string type) { ErrorPath errorPath; - errorPath.push_back(ErrorPathItem(shadowed, "Shadowed declaration")); - errorPath.push_back(ErrorPathItem(var, "Shadow variable")); + errorPath.emplace_back(shadowed, "Shadowed declaration"); + errorPath.emplace_back(var, "Shadow variable"); const std::string &varname = var ? var->str() : type; const std::string Type = char(std::toupper(type[0])) + type.substr(1); const std::string id = "shadow" + Type; diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index fbdbc8661..bb1be7156 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -809,7 +809,7 @@ void CheckStl::mismatchingContainers() if (!i) continue; const Token * const argTok = args[argnr - 1]; - containers[i->container].push_back({argTok, i}); + containers[i->container].emplace_back(ArgIteratorInfo{argTok, i}); } // Lambda is used to escape the nested loops @@ -1002,7 +1002,7 @@ struct InvalidContainerAnalyzer { ep.emplace_front(ftok, "After calling '" + ftok->expressionString() + "', iterators or references to the container's data may be invalid ."); - result.push_back(Info::Reference{tok, ep, ftok}); + result.emplace_back(Info::Reference{tok, ep, ftok}); } } return result; diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 96aadd796..72a9345ed 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -110,7 +110,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std: std::vector suppressions = Suppressions::parseMultiSuppressComment(comment, &errmsg); if (!errmsg.empty()) - bad->push_back(BadInlineSuppression(tok->location, errmsg)); + bad->emplace_back(tok->location, errmsg); for (const Suppressions::Suppression &s : suppressions) { if (!s.errorId.empty()) @@ -127,7 +127,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std: inlineSuppressions.push_back(s); if (!errmsg.empty()) - bad->push_back(BadInlineSuppression(tok->location, errmsg)); + bad->emplace_back(tok->location, errmsg); } return true; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index e285c9839..5fccbec83 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5724,10 +5724,9 @@ const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startSc } } else { const Type * type = scope->findType(tok->str()); - const Scope *scope1; if (type) return type; - else if ((scope1 = scope->findRecordInBase(tok->str()))) { + else if (const Scope *scope1 = scope->findRecordInBase(tok->str())) { type = scope1->definedType; if (type) return type; @@ -5764,10 +5763,9 @@ const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startSc } } else { const Type * type = scope->findType(tok->str()); - const Scope *scope1; if (type) return type; - else if ((scope1 = scope->findRecordInBase(tok->str()))) { + else if (const Scope *scope1 = scope->findRecordInBase(tok->str())) { type = scope1->definedType; if (type) return type; diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 37653c162..9d1e0c073 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1069,7 +1069,7 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration) // default parameter value? else if (Token::Match(tok, "= !!>")) { if (defaultedArgPos.insert(templatepar).second) { - eq.push_back(Default{tok, nullptr}); + eq.emplace_back(Default{tok, nullptr}); } else { // Ticket #5605: Syntax error (two equal signs for the same parameter), bail out eq.clear(); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2a37fb9dc..9124c554c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3492,13 +3492,13 @@ void VariableMap::addVariable(const std::string& varname, bool globalNamespace) } std::map::iterator it = mVariableId.find(varname); if (it == mVariableId.end()) { - mScopeInfo.top().push_back(std::pair(varname, 0)); + mScopeInfo.top().emplace_back(varname, 0); mVariableId[varname] = ++mVarId; if (globalNamespace) mVariableId_global[varname] = mVariableId[varname]; return; } - mScopeInfo.top().push_back(std::pair(varname, it->second)); + mScopeInfo.top().emplace_back(varname, it->second); it->second = ++mVarId; } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1ab594a2e..59e3b09fd 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2737,7 +2737,7 @@ struct ValueFlowAnalyzer : Analyzer { if (std::none_of(refs.begin(), refs.end(), [&](const ReferenceToken& ref) { return tok == ref.token; })) - refs.push_back(ReferenceToken{tok, {}}); + refs.emplace_back(ReferenceToken{tok, {}}); for (const ReferenceToken& ref:refs) { Action a = analyzeToken(ref.token, tok, d, inconclusiveRefs && ref.token != tok); if (internalMatch(ref.token)) @@ -3143,7 +3143,7 @@ struct SubExpressionAnalyzer : ExpressionAnalyzer { } void internalUpdate(Token* tok, const ValueFlow::Value& v, Direction) override { - partialReads->push_back(std::make_pair(tok, v)); + partialReads->emplace_back(tok, v); } // No reanalysis for subexression diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 32766aeb6..0d76bd235 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -175,8 +175,8 @@ std::string TestFixture::deleteLineNumber(const std::string &message) while ((pos = result.find(':', pos)) != std::string::npos) { // get number if (pos + 1 == result.find_first_of("0123456789", pos + 1)) { - std::string::size_type after; - if ((after = result.find_first_not_of("0123456789", pos + 1)) != std::string::npos + std::string::size_type after = result.find_first_not_of("0123456789", pos + 1); + if (after != std::string::npos && result.at(after) == ':') { // erase NUMBER result.erase(pos + 1, after - pos - 1);