Fix issue 9351: false negative: (style) Condition '...' is always true (#2201)

This commit is contained in:
Paul Fultz II 2019-09-23 01:49:04 -05:00 committed by Daniel Marjamäki
parent 32c89345f0
commit a903aa7070
3 changed files with 11 additions and 2 deletions

View File

@ -1357,8 +1357,9 @@ void CheckCondition::alwaysTrueFalse()
const bool compExpr = Token::Match(tok, "%comp%|!"); // a compare expression const bool compExpr = Token::Match(tok, "%comp%|!"); // a compare expression
const bool returnStatement = Token::simpleMatch(tok->astTop(), "return") && const bool returnStatement = Token::simpleMatch(tok->astTop(), "return") &&
Token::Match(tok->astParent(), "%oror%|&&|return"); Token::Match(tok->astParent(), "%oror%|&&|return");
const bool ternaryExpression = Token::simpleMatch(tok->astParent(), "?");
if (!(constIfWhileExpression || constValExpr || compExpr || returnStatement)) if (!(constIfWhileExpression || constValExpr || compExpr || returnStatement || ternaryExpression))
continue; continue;
if (returnStatement && (!scope->function || !Token::simpleMatch(scope->function->retDef, "bool"))) if (returnStatement && (!scope->function || !Token::simpleMatch(scope->function->retDef, "bool")))

View File

@ -4730,7 +4730,7 @@ static void valueFlowInferCondition(TokenList* tokenlist,
continue; continue;
if (tok->hasKnownValue()) if (tok->hasKnownValue())
continue; continue;
if (Token::Match(tok, "%var%") && (Token::Match(tok->astParent(), "&&|!|%oror%") || if (Token::Match(tok, "%var%") && (Token::Match(tok->astParent(), "?|&&|!|%oror%") ||
Token::Match(tok->astParent()->previous(), "if|while ("))) { Token::Match(tok->astParent()->previous(), "if|while ("))) {
const ValueFlow::Value* result = proveNotEqual(tok->values(), 0); const ValueFlow::Value* result = proveNotEqual(tok->values(), 0);
if (!result) if (!result)

View File

@ -3295,6 +3295,14 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #9351
check("int f(int x) {\n"
" const bool b = x < 42;\n"
" if(b) return b?0:-1;\n"
" return 42;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (style) Condition 'b' is always true\n", errout.str());
// #9362 // #9362
check("uint8_t g();\n" check("uint8_t g();\n"
"void f() {\n" "void f() {\n"