Remove redundant parts of conditional expressions (#988)

All issues were found with PVS-Studio:
V560 A part of conditional expression is always true: tok. astutils.cpp 407
V560 A part of conditional expression is always true: size > 0. checkbufferoverrun.cpp 709
V547 Expression 'secondTrue' is always true. checkcondition.cpp 1013
V547 Expression 'firstTrue' is always true. checkcondition.cpp 1020
V560 A part of conditional expression is always true: !scan. checkio.cpp 1036
V560 A part of conditional expression is always true: scope->function. checknullpointer.cpp 395
V560 A part of conditional expression is always true: tok2. checkstl.cpp 268
V560 A part of conditional expression is always true: par. tokenize.cpp 9440
V547 Expression '!erased' is always true. symboldatabase.cpp 3990
This commit is contained in:
Oleksandr Redko 2017-11-03 11:39:57 +02:00 committed by Daniel Marjamäki
parent 5ba5ea3aaf
commit a8700f5622
8 changed files with 10 additions and 11 deletions

View File

@ -404,7 +404,7 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
return false; return false;
// address of variable // address of variable
const bool addressOf = tok && Token::simpleMatch(tok->previous(), "&"); const bool addressOf = Token::simpleMatch(tok->previous(), "&");
// passing variable to subfunction? // passing variable to subfunction?
if (Token::Match(tok->tokAt(-2), ") & %name% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] (")) if (Token::Match(tok->tokAt(-2), ") & %name% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] ("))

View File

@ -706,7 +706,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<const st
} }
// memset, memcmp, memcpy, strncpy, fgets.. // memset, memcmp, memcpy, strncpy, fgets..
if (declarationId == 0 && size > 0 && Token::Match(tok, "%name% ( !!)")) { if (declarationId == 0 && Token::Match(tok, "%name% ( !!)")) {
std::list<const Token *> callstack; std::list<const Token *> callstack;
callstack.push_back(tok); callstack.push_back(tok);
const Token* tok2 = tok->tokAt(2); const Token* tok2 = tok->tokAt(2);

View File

@ -1010,14 +1010,14 @@ void CheckCondition::checkIncorrectLogicOperator()
incorrectLogicOperatorError(tok, text, alwaysTrue, inconclusive); incorrectLogicOperatorError(tok, text, alwaysTrue, inconclusive);
} else if (printStyle && secondTrue) { } else if (printStyle && secondTrue) {
const std::string text = "If '" + cond1str + "', the comparison '" + cond2str + const std::string text = "If '" + cond1str + "', the comparison '" + cond2str +
"' is always " + (secondTrue ? "true" : "false") + "."; "' is always true.";
redundantConditionError(tok, text, inconclusive); redundantConditionError(tok, text, inconclusive);
} else if (printStyle && firstTrue) { } else if (printStyle && firstTrue) {
//const std::string text = "The comparison " + cond1str + " is always " + //const std::string text = "The comparison " + cond1str + " is always " +
// (firstTrue ? "true" : "false") + " when " + // (firstTrue ? "true" : "false") + " when " +
// cond2str + "."; // cond2str + ".";
const std::string text = "If '" + cond2str + "', the comparison '" + cond1str + const std::string text = "If '" + cond2str + "', the comparison '" + cond1str +
"' is always " + (firstTrue ? "true" : "false") + "."; "' is always true.";
redundantConditionError(tok, text, inconclusive); redundantConditionError(tok, text, inconclusive);
} }
} }

View File

@ -1033,7 +1033,7 @@ void CheckIO::checkFormatString(const Token * const tok,
break; break;
} }
} }
} else if (!scan && printWarning) { } else if (printWarning) {
std::string specifier; std::string specifier;
bool done = false; bool done = false;
while (!done) { while (!done) {

View File

@ -392,7 +392,7 @@ void CheckNullPointer::nullConstantDereference()
const Token *tok = scope->classStart; const Token *tok = scope->classStart;
if (scope->function && scope->function->isConstructor()) if (scope->function->isConstructor())
tok = scope->function->token; // Check initialization list tok = scope->function->token; // Check initialization list
for (; tok != scope->classEnd; tok = tok->next()) { for (; tok != scope->classEnd; tok = tok->next()) {

View File

@ -265,7 +265,7 @@ void CheckStl::iterators()
// bailout handling. Assume that the iterator becomes valid if we see else. // bailout handling. Assume that the iterator becomes valid if we see else.
// TODO: better handling // TODO: better handling
else if (tok2 && tok2->str() == "else") { else if (tok2->str() == "else") {
validIterator = true; validIterator = true;
} }
} }

View File

@ -3973,7 +3973,6 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
// check each function against the arguments in the function call for a match // check each function against the arguments in the function call for a match
for (std::size_t i = 0; i < matches.size();) { for (std::size_t i = 0; i < matches.size();) {
bool erased = false;
bool constFallback = false; bool constFallback = false;
const Function * func = matches[i]; const Function * func = matches[i];
size_t same = 0; size_t same = 0;
@ -3987,8 +3986,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
// check if isConst mismatches // check if isConst mismatches
if (scope->function->isConst() != func->isConst()) { if (scope->function->isConst() != func->isConst()) {
if (scope->function->isConst()) { if (scope->function->isConst()) {
if (!erased) ++i;
++i;
continue; continue;
} }
constFallback = true; constFallback = true;
@ -3998,6 +3996,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
size_t fallback1 = 0; size_t fallback1 = 0;
size_t fallback2 = 0; size_t fallback2 = 0;
bool erased = false;
for (std::size_t j = 0; j < args; ++j) { for (std::size_t j = 0; j < args; ++j) {
// don't check variadic arguments // don't check variadic arguments

View File

@ -9437,7 +9437,7 @@ void Tokenizer::simplifyOperatorName()
bool done = false; bool done = false;
while (!done && par) { while (!done && par) {
done = true; done = true;
if (par && par->isName()) { if (par->isName()) {
op += par->str(); op += par->str();
par = par->next(); par = par->next();
// merge namespaces eg. 'operator std :: string () const {' // merge namespaces eg. 'operator std :: string () const {'