diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 39074a8b6..2666e7c85 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2383,6 +2383,12 @@ void CheckClass::duplInheritedMembersError(const Token *tok1, const Token* tok2, // Check that copy constructor and operator defined together //--------------------------------------------------------------------------- +enum CtorType { + NO, + WITHOUT_BODY, + WITH_BODY +}; + void CheckClass::checkCopyCtorAndEqOperator() { if (!_settings->isEnabled(Settings::WARNING)) @@ -2402,24 +2408,25 @@ void CheckClass::checkCopyCtorAndEqOperator() if (!hasNonStaticVars) continue; - int hasCopyCtor = 0; - int hasAssignmentOperator = 0; + CtorType copyCtors = CtorType::NO; + CtorType assignmentOperators = CtorType::NO; std::list::const_iterator func; for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { - if (!hasCopyCtor && func->type == Function::eCopyConstructor) { - hasCopyCtor = func->hasBody() ? 2 : 1; + if (copyCtors == CtorType::NO && func->type == Function::eCopyConstructor) { + copyCtors = func->hasBody() ? CtorType::WITH_BODY : CtorType::WITHOUT_BODY; } - if (!hasAssignmentOperator && func->type == Function::eOperatorEqual) { + if (assignmentOperators == CtorType::NO && func->type == Function::eOperatorEqual) { const Variable * variable = func->getArgumentVar(0); if (variable && variable->type() && variable->type()->classScope == scope) { - hasAssignmentOperator = func->hasBody() ? 2 : 1; + assignmentOperators = func->hasBody() ? CtorType::WITH_BODY : CtorType::WITHOUT_BODY; } } } - if (std::abs(hasCopyCtor - hasAssignmentOperator) == 2) - copyCtorAndEqOperatorError(scope->classDef, scope->className, scope->type == Scope::eStruct, hasCopyCtor != 0); + if ((copyCtors == CtorType::WITH_BODY && assignmentOperators == CtorType::NO) || + (copyCtors == CtorType::NO && assignmentOperators == CtorType::WITH_BODY)) + copyCtorAndEqOperatorError(scope->classDef, scope->className, scope->type == Scope::eStruct, copyCtors == CtorType::WITH_BODY); } } diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 60322c02c..4c7e4e834 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -432,11 +432,11 @@ const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int return ""; // Check if pointer is allocated. - int realloc = 0; + bool realloc = false; for (tok = func->functionScope->classStart; tok && tok != func->functionScope->classEnd; tok = tok->next()) { if (tok->varId() == arg->declarationId()) { if (Token::Match(tok->tokAt(-3), "free ( * %name% )")) { - realloc = 1; + realloc = true; allocType = No; } else if (Token::Match(tok->previous(), "* %name% =")) { allocType = getAllocationType(tok->tokAt(2), arg->declarationId()); @@ -1172,7 +1172,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::liststr(), _settings, tokenizer->isCPP()) || - getDeallocationType(functions.top(),varid)) { + getDeallocationType(functions.top(),varid) != AllocType::No) { use = true; } } diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index d44f63564..09e1f0bca 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -79,7 +79,7 @@ public: /** is variable unused? */ bool unused() const { - return (_read == false && _write == false); + return (!_read && !_write); } std::set _aliases; diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 45b66037f..8db553636 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -114,7 +114,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin if (_settings.terminated()) return exitcode; - if (_settings.quiet == false) { + if (!_settings.quiet) { std::string fixedpath = Path::simplifyPath(filename); fixedpath = Path::toNativeSeparators(fixedpath); _errorLogger.reportOut(std::string("Checking ") + fixedpath + ' ' + cfgname + std::string("...")); @@ -312,7 +312,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin cfg = *it; // If only errors are printed, print filename after the check - if (_settings.quiet == false && (!cfg.empty() || it != configurations.begin())) { + if (!_settings.quiet && (!cfg.empty() || it != configurations.begin())) { std::string fixedpath = Path::simplifyPath(filename); fixedpath = Path::toNativeSeparators(fixedpath); _errorLogger.reportOut("Checking " + fixedpath + ": " + cfg + "..."); diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 8ecbfb94a..9c8ae6218 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -989,9 +989,9 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok) // Logical operations else if (Token::Match(op, "%oror%|&&")) { - int op1 = !MathLib::isNullValue(tok->str()); - int op2 = !MathLib::isNullValue(tok->strAt(2)); - int result = (op->str() == "||") ? (op1 || op2) : (op1 && op2); + bool op1 = !MathLib::isNullValue(tok->str()); + bool op2 = !MathLib::isNullValue(tok->strAt(2)); + bool result = (op->str() == "||") ? (op1 || op2) : (op1 && op2); tok->str(result ? "1" : "0"); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e14154710..cdf5e563d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4313,7 +4313,7 @@ bool Tokenizer::removeRedundantConditions() // Handle if with else if (Token::simpleMatch(elseTag, "else {")) { // Handle else - if (boolValue == false) { + if (!boolValue) { // Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" //remove '(false)' @@ -4342,7 +4342,7 @@ bool Tokenizer::removeRedundantConditions() // Handle if without else else { - if (boolValue == false) { + if (!boolValue) { //remove '(false)' tok->deleteNext(3); //delete dead code inside scope diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 67f04e2ae..ac48bb195 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -118,9 +118,8 @@ static bool conditionIsFalse(const Token *condition, const ProgramMemory &progra if (!condition) return false; if (condition->str() == "&&") { - const bool result1 = conditionIsFalse(condition->astOperand1(), programMemory); - const bool result2 = result1 ? true : conditionIsFalse(condition->astOperand2(), programMemory); - return result2; + return conditionIsFalse(condition->astOperand1(), programMemory) || + conditionIsFalse(condition->astOperand2(), programMemory); } ProgramMemory progmem(programMemory); MathLib::bigint result = 0; @@ -139,9 +138,8 @@ static bool conditionIsTrue(const Token *condition, const ProgramMemory &program if (!condition) return false; if (condition->str() == "||") { - const bool result1 = conditionIsTrue(condition->astOperand1(), programMemory); - const bool result2 = result1 ? true : conditionIsTrue(condition->astOperand2(), programMemory); - return result2; + return conditionIsTrue(condition->astOperand1(), programMemory) || + conditionIsTrue(condition->astOperand2(), programMemory); } ProgramMemory progmem(programMemory); bool error = false; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 602b2dd75..db85f6ea1 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -2514,9 +2514,9 @@ private: void symboldatabase42() { // only put variables in variable list GET_SYMBOL_DB("void f() { extern int x(); }\n"); - ASSERT(!!db); + ASSERT(db); const Scope * const fscope = db ? db->findScopeByName("f") : nullptr; - ASSERT(!!fscope); + ASSERT(fscope); ASSERT_EQUALS(0U, fscope ? fscope->varlist.size() : ~0U); // "x" is not a variable }