Simplify int vs bool

This commit is contained in:
Ayaz Salikhov 2017-10-08 07:54:39 +02:00 committed by Daniel Marjamäki
parent a29b33b833
commit be2c65eb58
8 changed files with 32 additions and 27 deletions

View File

@ -2383,6 +2383,12 @@ void CheckClass::duplInheritedMembersError(const Token *tok1, const Token* tok2,
// Check that copy constructor and operator defined together // Check that copy constructor and operator defined together
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
enum CtorType {
NO,
WITHOUT_BODY,
WITH_BODY
};
void CheckClass::checkCopyCtorAndEqOperator() void CheckClass::checkCopyCtorAndEqOperator()
{ {
if (!_settings->isEnabled(Settings::WARNING)) if (!_settings->isEnabled(Settings::WARNING))
@ -2402,24 +2408,25 @@ void CheckClass::checkCopyCtorAndEqOperator()
if (!hasNonStaticVars) if (!hasNonStaticVars)
continue; continue;
int hasCopyCtor = 0; CtorType copyCtors = CtorType::NO;
int hasAssignmentOperator = 0; CtorType assignmentOperators = CtorType::NO;
std::list<Function>::const_iterator func; std::list<Function>::const_iterator func;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (!hasCopyCtor && func->type == Function::eCopyConstructor) { if (copyCtors == CtorType::NO && func->type == Function::eCopyConstructor) {
hasCopyCtor = func->hasBody() ? 2 : 1; 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); const Variable * variable = func->getArgumentVar(0);
if (variable && variable->type() && variable->type()->classScope == scope) { 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) if ((copyCtors == CtorType::WITH_BODY && assignmentOperators == CtorType::NO) ||
copyCtorAndEqOperatorError(scope->classDef, scope->className, scope->type == Scope::eStruct, hasCopyCtor != 0); (copyCtors == CtorType::NO && assignmentOperators == CtorType::WITH_BODY))
copyCtorAndEqOperatorError(scope->classDef, scope->className, scope->type == Scope::eStruct, copyCtors == CtorType::WITH_BODY);
} }
} }

View File

@ -432,11 +432,11 @@ const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int
return ""; return "";
// Check if pointer is allocated. // Check if pointer is allocated.
int realloc = 0; bool realloc = false;
for (tok = func->functionScope->classStart; tok && tok != func->functionScope->classEnd; tok = tok->next()) { for (tok = func->functionScope->classStart; tok && tok != func->functionScope->classEnd; tok = tok->next()) {
if (tok->varId() == arg->declarationId()) { if (tok->varId() == arg->declarationId()) {
if (Token::Match(tok->tokAt(-3), "free ( * %name% )")) { if (Token::Match(tok->tokAt(-3), "free ( * %name% )")) {
realloc = 1; realloc = true;
allocType = No; allocType = No;
} else if (Token::Match(tok->previous(), "* %name% =")) { } else if (Token::Match(tok->previous(), "* %name% =")) {
allocType = getAllocationType(tok->tokAt(2), arg->declarationId()); allocType = getAllocationType(tok->tokAt(2), arg->declarationId());
@ -1172,7 +1172,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
; ;
} else if (functions.empty() || } else if (functions.empty() ||
!test_white_list(functions.top()->str(), _settings, tokenizer->isCPP()) || !test_white_list(functions.top()->str(), _settings, tokenizer->isCPP()) ||
getDeallocationType(functions.top(),varid)) { getDeallocationType(functions.top(),varid) != AllocType::No) {
use = true; use = true;
} }
} }

View File

@ -79,7 +79,7 @@ public:
/** is variable unused? */ /** is variable unused? */
bool unused() const { bool unused() const {
return (_read == false && _write == false); return (!_read && !_write);
} }
std::set<unsigned int> _aliases; std::set<unsigned int> _aliases;

View File

@ -114,7 +114,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
if (_settings.terminated()) if (_settings.terminated())
return exitcode; return exitcode;
if (_settings.quiet == false) { if (!_settings.quiet) {
std::string fixedpath = Path::simplifyPath(filename); std::string fixedpath = Path::simplifyPath(filename);
fixedpath = Path::toNativeSeparators(fixedpath); fixedpath = Path::toNativeSeparators(fixedpath);
_errorLogger.reportOut(std::string("Checking ") + fixedpath + ' ' + cfgname + std::string("...")); _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; cfg = *it;
// If only errors are printed, print filename after the check // 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); std::string fixedpath = Path::simplifyPath(filename);
fixedpath = Path::toNativeSeparators(fixedpath); fixedpath = Path::toNativeSeparators(fixedpath);
_errorLogger.reportOut("Checking " + fixedpath + ": " + cfg + "..."); _errorLogger.reportOut("Checking " + fixedpath + ": " + cfg + "...");

View File

@ -989,9 +989,9 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
// Logical operations // Logical operations
else if (Token::Match(op, "%oror%|&&")) { else if (Token::Match(op, "%oror%|&&")) {
int op1 = !MathLib::isNullValue(tok->str()); bool op1 = !MathLib::isNullValue(tok->str());
int op2 = !MathLib::isNullValue(tok->strAt(2)); bool op2 = !MathLib::isNullValue(tok->strAt(2));
int result = (op->str() == "||") ? (op1 || op2) : (op1 && op2); bool result = (op->str() == "||") ? (op1 || op2) : (op1 && op2);
tok->str(result ? "1" : "0"); tok->str(result ? "1" : "0");
} }

View File

@ -4313,7 +4313,7 @@ bool Tokenizer::removeRedundantConditions()
// Handle if with else // Handle if with else
if (Token::simpleMatch(elseTag, "else {")) { if (Token::simpleMatch(elseTag, "else {")) {
// Handle else // Handle else
if (boolValue == false) { if (!boolValue) {
// Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}" // Convert "if( false ) {aaa;} else {bbb;}" => "{bbb;}"
//remove '(false)' //remove '(false)'
@ -4342,7 +4342,7 @@ bool Tokenizer::removeRedundantConditions()
// Handle if without else // Handle if without else
else { else {
if (boolValue == false) { if (!boolValue) {
//remove '(false)' //remove '(false)'
tok->deleteNext(3); tok->deleteNext(3);
//delete dead code inside scope //delete dead code inside scope

View File

@ -118,9 +118,8 @@ static bool conditionIsFalse(const Token *condition, const ProgramMemory &progra
if (!condition) if (!condition)
return false; return false;
if (condition->str() == "&&") { if (condition->str() == "&&") {
const bool result1 = conditionIsFalse(condition->astOperand1(), programMemory); return conditionIsFalse(condition->astOperand1(), programMemory) ||
const bool result2 = result1 ? true : conditionIsFalse(condition->astOperand2(), programMemory); conditionIsFalse(condition->astOperand2(), programMemory);
return result2;
} }
ProgramMemory progmem(programMemory); ProgramMemory progmem(programMemory);
MathLib::bigint result = 0; MathLib::bigint result = 0;
@ -139,9 +138,8 @@ static bool conditionIsTrue(const Token *condition, const ProgramMemory &program
if (!condition) if (!condition)
return false; return false;
if (condition->str() == "||") { if (condition->str() == "||") {
const bool result1 = conditionIsTrue(condition->astOperand1(), programMemory); return conditionIsTrue(condition->astOperand1(), programMemory) ||
const bool result2 = result1 ? true : conditionIsTrue(condition->astOperand2(), programMemory); conditionIsTrue(condition->astOperand2(), programMemory);
return result2;
} }
ProgramMemory progmem(programMemory); ProgramMemory progmem(programMemory);
bool error = false; bool error = false;

View File

@ -2514,9 +2514,9 @@ private:
void symboldatabase42() { // only put variables in variable list void symboldatabase42() { // only put variables in variable list
GET_SYMBOL_DB("void f() { extern int x(); }\n"); GET_SYMBOL_DB("void f() { extern int x(); }\n");
ASSERT(!!db); ASSERT(db);
const Scope * const fscope = db ? db->findScopeByName("f") : nullptr; 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 ASSERT_EQUALS(0U, fscope ? fscope->varlist.size() : ~0U); // "x" is not a variable
} }