From eac040a00bc5eab11fd829b8002efbd34cb070cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 25 Sep 2019 15:25:19 +0200 Subject: [PATCH] Various clang-tidy fixes (#2192) * use range loops * removed redundant string initializations * use nullptr * use proper boolean false * removed unnecessary continue from end of loop * removed unnecessary c_str() usage * use emplace_back() * removed redundant void arguments --- lib/checkbool.cpp | 2 +- lib/importproject.cpp | 4 ++-- lib/library.cpp | 4 ++-- lib/path.cpp | 2 +- lib/preprocessor.cpp | 2 +- lib/standards.h | 4 ++-- lib/symboldatabase.cpp | 12 ++++++------ lib/templatesimplifier.cpp | 4 ++-- lib/token.cpp | 2 +- lib/tokenize.cpp | 7 +++---- test/testfunctions.cpp | 2 +- test/testmathlib.cpp | 4 ++-- test/testmemleak.cpp | 2 +- test/testother.cpp | 4 ++-- test/testsuppressions.cpp | 2 +- test/testtoken.cpp | 16 ++++++++-------- test/testtokenize.cpp | 4 ++-- 17 files changed, 38 insertions(+), 39 deletions(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index a385d8336..dc38193ec 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -440,7 +440,7 @@ void CheckBool::assignBoolToFloatError(const Token *tok) "Boolean value assigned to floating point variable.", CWE704, false); } -void CheckBool::returnValueOfFunctionReturningBool(void) +void CheckBool::returnValueOfFunctionReturningBool() { if (!mSettings->isEnabled(Settings::STYLE)) return; diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 5e5d6c82b..ac8920fd8 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -626,7 +626,7 @@ void ImportProject::importVcxproj(const std::string &filename, std::mapName(), "ClCompile") == 0) { const char *include = e->Attribute("Include"); if (include && Path::acceptFile(include)) - compileList.push_back(include); + compileList.emplace_back(include); } } } @@ -700,7 +700,7 @@ void ImportProject::importBcb6Prj(const std::string &projectFilename) if (std::strcmp(f->Name(), "FILE") == 0) { const char *filename = f->Attribute("FILENAME"); if (filename && Path::acceptFile(filename)) - compileList.push_back(filename); + compileList.emplace_back(filename); } } } else if (std::strcmp(node->Name(), "MACROS") == 0) { diff --git a/lib/library.cpp b/lib/library.cpp index fb5cf7cad..4b6951fe0 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -39,7 +39,7 @@ static std::vector getnames(const char *names) ret.emplace_back(names, p-names); names = p + 1; } - ret.push_back(names); + ret.emplace_back(names); return ret; } @@ -96,7 +96,7 @@ Library::Error Library::load(const char exename[], const char path[]) std::list cfgfolders; #ifdef FILESDIR - cfgfolders.push_back(FILESDIR "/cfg"); + cfgfolders.emplace_back(FILESDIR "/cfg"); #endif if (exename) { const std::string exepath(Path::fromNativeSeparators(Path::getPathFromFilename(exename))); diff --git a/lib/path.cpp b/lib/path.cpp index f36c30a3e..4d06a66d5 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -134,7 +134,7 @@ std::string Path::getCurrentPath() #ifndef _WIN32 if (getcwd(currentPath, 4096) != nullptr) #else - if (_getcwd(currentPath, 4096) != 0) + if (_getcwd(currentPath, 4096) != nullptr) #endif return std::string(currentPath); diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 3badff031..e5107c466 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -543,7 +543,7 @@ static simplecpp::DUI createDUI(const Settings &mSettings, const std::string &cf } if (Path::isCPP(filename)) - dui.defines.push_back("__cplusplus"); + dui.defines.emplace_back("__cplusplus"); dui.undefined = mSettings.userUndefs; // -U dui.includePaths = mSettings.includePaths; // -I diff --git a/lib/standards.h b/lib/standards.h index 753643c7e..00ef313d8 100644 --- a/lib/standards.h +++ b/lib/standards.h @@ -56,7 +56,7 @@ struct Standards { } return false; } - const std::string getC(void) const { + const std::string getC() const { switch (c) { case C89: return "c89"; @@ -90,7 +90,7 @@ struct Standards { } return false; } - const std::string getCPP(void) const { + const std::string getCPP() const { switch (cpp) { case CPP03: return "c++03"; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 575eb20e6..ccc97d818 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -715,13 +715,13 @@ void SymbolDatabase::createSymbolDatabaseClassInfo() // fill in base class info for (std::list::iterator it = typeList.begin(); it != typeList.end(); ++it) { // finish filling in base class info - for (unsigned int i = 0; i < it->derivedFrom.size(); ++i) { - const Type* found = findType(it->derivedFrom[i].nameTok, it->enclosingScope); + for (Type::BaseInfo & i : it->derivedFrom) { + const Type* found = findType(i.nameTok, it->enclosingScope); if (found && found->findDependency(&(*it))) { // circular dependency //mTokenizer->syntaxError(nullptr); } else { - it->derivedFrom[i].type = found; + i.type = found; } } } @@ -842,7 +842,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() Scope *scope = &(*it); if (!scope->definedType) { - mBlankTypes.push_back(Type()); + mBlankTypes.emplace_back(); scope->definedType = &mBlankTypes.back(); } @@ -3344,8 +3344,8 @@ const Function *Function::getOverriddenFunction(bool *foundAllBaseClasses) const const Function * Function::getOverriddenFunctionRecursive(const ::Type* baseType, bool *foundAllBaseClasses) const { // check each base class - for (std::size_t i = 0; i < baseType->derivedFrom.size(); ++i) { - const ::Type* derivedFromType = baseType->derivedFrom[i].type; + for (const ::Type::BaseInfo & i : baseType->derivedFrom) { + const ::Type* derivedFromType = i.type; // check if base class exists in database if (!derivedFromType || !derivedFromType->classScope) { if (foundAllBaseClasses) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index dfe299c74..d301c4d95 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -3420,7 +3420,7 @@ void TemplateSimplifier::printOut(const std::string & text) const unsigned int decl1Index = 0; for (const auto & decl1 : mTemplateDeclarations) { if (decl1.isSpecialization() && mapItem.first == decl1.token()) { - bool found = 0; + bool found = false; unsigned int decl2Index = 0; for (const auto & decl2 : mTemplateDeclarations) { if (mapItem.second == decl2.token()) { @@ -3455,7 +3455,7 @@ void TemplateSimplifier::printOut(const std::string & text) const unsigned int decl1Index = 0; for (const auto & decl1 : mTemplateDeclarations) { if (mapItem.first == decl1.token()) { - bool found = 0; + bool found = false; unsigned int decl2Index = 0; for (const auto & decl2 : mTemplateDeclarations) { if (mapItem.second == decl2.token()) { diff --git a/lib/token.cpp b/lib/token.cpp index fe0656361..6b0cec3df 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1016,7 +1016,7 @@ void Token::insertToken(const std::string &tokenStr, const std::string &original if (mImpl->mScopeInfo) { // If the brace is immediately closed there is no point opening a new scope for it if (tokenStr == "{") { - std::string nextScopeNameAddition = ""; + std::string nextScopeNameAddition; // This might be the opening of a member function Token *tok1 = newToken; while (Token::Match(tok1->previous(), "const|volatile|final|override|&|&&|noexcept")) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d50d5b058..d9c8e2ac1 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -867,7 +867,7 @@ void Tokenizer::simplifyTypedef() if (tokOffset->next()->str() == "(") tokOffset = tokOffset->next(); else if (Token::simpleMatch(tokOffset, "( * (")) { - pointers.push_back("*"); + pointers.emplace_back("*"); tokOffset = tokOffset->tokAt(2); } @@ -2837,7 +2837,7 @@ void Tokenizer::calculateScopes() for (auto tok = list.front(); tok; tok = tok->next()) tok->scopeInfo(nullptr); - std::string nextScopeNameAddition = ""; + std::string nextScopeNameAddition; std::shared_ptr primaryScope = std::make_shared("", nullptr); list.front()->scopeInfo(primaryScope); @@ -2847,7 +2847,7 @@ void Tokenizer::calculateScopes() tok->scopeInfo(tok->previous()->scopeInfo()); if (Token::Match(tok, "using namespace %name% ::|<|;")) { - std::string usingNamespaceName = ""; + std::string usingNamespaceName; for (const Token* namespaceNameToken = tok->tokAt(2); !Token::simpleMatch(namespaceNameToken, ";"); namespaceNameToken = namespaceNameToken->next()) { @@ -7993,7 +7993,6 @@ bool Tokenizer::simplifyRedundantParentheses() tok->deleteNext(); tok2->deleteThis(); ret = true; - continue; } if (Token::simpleMatch(tok->previous(), "? (") && Token::simpleMatch(tok->link(), ") :")) { diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 04dbc49ef..70c2f7621 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -38,7 +38,7 @@ private: settings.addEnabled("style"); settings.addEnabled("warning"); settings.addEnabled("portability"); - settings.libraries.push_back("posix"); + settings.libraries.emplace_back("posix"); settings.standards.c = Standards::C11; settings.standards.cpp = Standards::CPP11; LOAD_LIB_2(settings.library, "std.cfg"); diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index e259f7a00..0acc2449f 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -704,7 +704,7 @@ private: ASSERT_EQUALS(false, MathLib::isIntHex("")); } - void isValidIntegerSuffix(void) const { + void isValidIntegerSuffix() const { // negative testing ASSERT_EQUALS(false, MathLib::isValidIntegerSuffix("")); ASSERT_EQUALS(false, MathLib::isValidIntegerSuffix("ux")); @@ -889,7 +889,7 @@ private: ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142) } - void isdec(void) const { + void isdec() const { // positive testing ASSERT_EQUALS(true, MathLib::isDec("1")); ASSERT_EQUALS(true, MathLib::isDec("+1")); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 17342d1ea..4467691f7 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2066,7 +2066,7 @@ private: void run() OVERRIDE { settings.inconclusive = true; - settings.libraries.push_back("posix"); + settings.libraries.emplace_back("posix"); settings.addEnabled("warning"); LOAD_LIB_2(settings.library, "std.cfg"); diff --git a/test/testother.cpp b/test/testother.cpp index 2fa8271a0..79851ee67 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -235,7 +235,7 @@ private: TEST_CASE(unusedVariableValueTemplate); // #8994 } - void check(const char code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool runSimpleChecks=true, bool verbose=false, Settings* settings = 0) { + void check(const char code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool runSimpleChecks=true, bool verbose=false, Settings* settings = nullptr) { // Clear the error buffer.. errout.str(""); @@ -305,7 +305,7 @@ private: void checkposix(const char code[]) { static Settings settings; settings.addEnabled("warning"); - settings.libraries.push_back("posix"); + settings.libraries.emplace_back("posix"); check(code, nullptr, // filename diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 3b2cf5a0c..43763daf3 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -509,7 +509,7 @@ private: settings.addEnabled("style"); settings.inlineSuppressions = true; settings.relativePaths = true; - settings.basePaths.push_back("/somewhere"); + settings.basePaths.emplace_back("/somewhere"); const char code[] = "struct Point\n" "{\n" diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 464e26fc3..3f11654f6 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -833,14 +833,14 @@ private: void isStandardType() const { std::vector standard_types; - standard_types.push_back("bool"); - standard_types.push_back("char"); - standard_types.push_back("short"); - standard_types.push_back("int"); - standard_types.push_back("long"); - standard_types.push_back("float"); - standard_types.push_back("double"); - standard_types.push_back("size_t"); + standard_types.emplace_back("bool"); + standard_types.emplace_back("char"); + standard_types.emplace_back("short"); + standard_types.emplace_back("int"); + standard_types.emplace_back("long"); + standard_types.emplace_back("float"); + standard_types.emplace_back("double"); + standard_types.emplace_back("size_t"); std::vector::const_iterator test_op, test_ops_end = standard_types.end(); for (test_op = standard_types.begin(); test_op != test_ops_end; ++test_op) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 77f6c64e6..513cfba71 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -489,7 +489,7 @@ private: // filter out ValueFlow messages.. const std::string debugwarnings = errout.str(); errout.str(""); - std::istringstream istr2(debugwarnings.c_str()); + std::istringstream istr2(debugwarnings); std::string line; while (std::getline(istr2,line)) { if (line.find("valueflow.cpp") == std::string::npos) @@ -519,7 +519,7 @@ private: // filter out ValueFlow messages.. const std::string debugwarnings = errout.str(); errout.str(""); - std::istringstream istr2(debugwarnings.c_str()); + std::istringstream istr2(debugwarnings); std::string line; while (std::getline(istr2,line)) { if (line.find("valueflow.cpp") == std::string::npos)