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
//---------------------------------------------------------------------------
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<Function>::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);
}
}

View File

@ -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::list<const Toke
;
} else if (functions.empty() ||
!test_white_list(functions.top()->str(), _settings, tokenizer->isCPP()) ||
getDeallocationType(functions.top(),varid)) {
getDeallocationType(functions.top(),varid) != AllocType::No) {
use = true;
}
}

View File

@ -79,7 +79,7 @@ public:
/** is variable unused? */
bool unused() const {
return (_read == false && _write == false);
return (!_read && !_write);
}
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())
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 + "...");

View File

@ -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");
}

View File

@ -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

View File

@ -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;

View File

@ -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
}