Simplify code - bail out early, vreak loops early, reorder checks and declarations.

This commit is contained in:
Dmitry-Me 2014-06-09 13:35:30 +04:00
parent 42c3b3c89d
commit 6e1568a6db
4 changed files with 46 additions and 29 deletions

View File

@ -218,13 +218,11 @@ void CheckBool::checkComparisonOfFuncReturningBool()
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (tok->type() != Token::eComparisonOp || tok->str() == "==" || tok->str() == "!=")
continue;
const Token *first_token;
bool first_token_func_of_type_bool = false;
const Token *first_token = tok->previous();
if (tok->strAt(-1) == ")") {
first_token = tok->previous()->link()->previous();
} else {
first_token = tok->previous();
first_token = first_token->link()->previous();
}
bool first_token_func_of_type_bool = false;
if (Token::Match(first_token, "%var% (") && !Token::Match(first_token->previous(), "::|.")) {
const Function* func = first_token->function();
if (func && func->tokenDef && func->tokenDef->strAt(-1) == "bool") {
@ -233,10 +231,10 @@ void CheckBool::checkComparisonOfFuncReturningBool()
}
Token *second_token = tok->next();
bool second_token_func_of_type_bool = false;
while (second_token->str()=="!") {
second_token = second_token->next();
}
bool second_token_func_of_type_bool = false;
if (Token::Match(second_token, "%var% (") && !Token::Match(second_token->previous(), "::|.")) {
const Function* func = second_token->function();
if (func && func->tokenDef && func->tokenDef->strAt(-1) == "bool") {
@ -299,7 +297,6 @@ void CheckBool::checkComparisonOfBoolWithBool()
if (tok->type() != Token::eComparisonOp || tok->str() == "==" || tok->str() == "!=")
continue;
bool first_token_bool = false;
bool second_token_bool = false;
const Token *first_token = tok->previous();
if (first_token->varId()) {
@ -307,13 +304,17 @@ void CheckBool::checkComparisonOfBoolWithBool()
first_token_bool = true;
}
}
if (!first_token_bool)
continue;
bool second_token_bool = false;
const Token *second_token = tok->next();
if (second_token->varId()) {
if (isBool(second_token->variable())) {
second_token_bool = true;
}
}
if ((first_token_bool == true) && (second_token_bool == true)) {
if (second_token_bool) {
comparisonOfBoolWithBoolError(first_token->next(), first_token->str());
}
}
@ -374,7 +375,7 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
continue;
// Skip template parameters
if (tok->str() == "<" && tok->link()) {
if (tok->link() && tok->str() == "<") {
tok = tok->link();
continue;
}
@ -394,11 +395,11 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
continue;
}
if (Token::Match(boolExpr,"%bool%"))
// The CheckBool::checkComparisonOfBoolWithInt warns about this.
if (!numTok || !boolExpr)
continue;
if (!numTok || !boolExpr)
if (Token::Match(boolExpr,"%bool%"))
// The CheckBool::checkComparisonOfBoolWithInt warns about this.
continue;
if (boolExpr->isOp() && numTok->isName() && Token::Match(tok, "==|!="))

View File

@ -138,8 +138,6 @@ void CheckClass::constructors()
if (var->hasDefault())
usage[count].init = true;
bool inconclusive = false;
if (usage[count].assign || usage[count].init || var->isStatic())
continue;
@ -169,14 +167,19 @@ void CheckClass::constructors()
}
}
bool inconclusive = false;
// Don't warn about unknown types in copy constructors since we
// don't know if they can be copied or not..
if (!var->isPointer() &&
!(var->type() && var->type()->needInitialization != Type::True) &&
(func->type == Function::eCopyConstructor || func->type == Function::eOperatorEqual)) {
bool stdtype = false;
for (const Token *type = var->typeStartToken(); type && type->isName(); type = type->next())
stdtype |= type->isStandardType();
for (const Token *type = var->typeStartToken(); type && type->isName(); type = type->next()) {
if (type->isStandardType()) {
stdtype = true;
break;
}
}
if (!stdtype) {
if (_settings->inconclusive)
inconclusive = true;
@ -942,7 +945,8 @@ void CheckClass::checkMemset()
const Token* arg1 = tok->tokAt(2);
const Token* arg3 = arg1;
arg3 = arg3->nextArgument();
arg3 = (arg3 != nullptr) ? arg3->nextArgument() : nullptr;
if (arg3)
arg3 = arg3->nextArgument();
if (!arg3)
// weird, shouldn't happen: memset etc should have
// 3 arguments.
@ -1268,12 +1272,11 @@ void CheckClass::operatorEqToSelf()
const std::size_t classes = symbolDatabase->classAndStructScopes.size();
for (std::size_t i = 0; i < classes; ++i) {
const Scope * scope = symbolDatabase->classAndStructScopes[i];
std::list<Function>::const_iterator func;
// skip classes with multiple inheritance
if (scope->definedType->derivedFrom.size() > 1)
continue;
std::list<Function>::const_iterator func;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->type == Function::eOperatorEqual && func->hasBody) {
// make sure that the operator takes an object of the same type as *this, otherwise we can't detect self-assignment checks

View File

@ -1404,7 +1404,7 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * tok, const Settings *settings)
break;
} else if (tok1->str() == "(" || tok1->str() == "{" || tok1->str() == "[")
tok1 = tok1->link();
else if (tok1->str() == "<" && tok1->link())
else if (tok1->link() && tok1->str() == "<")
tok1 = tok1->link();
// check for some common well known functions
@ -1419,10 +1419,13 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * tok, const Settings *settings)
tempToken->str("long");
if (settings->sizeof_long != 8)
tempToken->isLong(true);
} else if (settings->sizeof_size_t == 4 && settings->sizeof_long == 4)
} else if (settings->sizeof_size_t == 4) {
if (settings->sizeof_long == 4) {
tempToken->str("long");
else if (settings->sizeof_size_t == 4)
} else {
tempToken->str("int");
}
}
tempToken->originalName("size_t");
tempToken->isUnsigned(true);

View File

@ -1820,7 +1820,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
const std::string def = getdef(line, true);
const std::string ndef = getdef(line, false);
const bool emptymatch = matching_ifdef.empty() | matched_ifdef.empty();
const bool emptymatch = matching_ifdef.empty() || matched_ifdef.empty();
if (line.compare(0, 8, "#define ") == 0) {
match = true;
@ -1842,8 +1842,14 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
}
}
for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it)
match &= bool(*it);
if (match) {
for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it) {
if (!bool(*it)) {
match = false;
break;
}
}
}
if (match) {
std::string::size_type pos = line.find_first_of(" (", 8);
@ -1915,8 +1921,12 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
if (!line.empty() && line[0] == '#') {
match = true;
for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it)
match &= bool(*it);
for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it) {
if (!bool(*it)) {
match = false;
break;
}
}
}
// #error => return ""