Fix 10148: False positive: knownConditionTrueFalse in for loop due to &= (#3425)

This commit is contained in:
Paul Fultz II 2021-08-28 02:28:29 -05:00 committed by GitHub
parent 6541efdb9a
commit 4566e0e439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -2160,6 +2160,9 @@ struct ValueFlowAnalyzer : Analyzer {
if (!(value->isIntValue() || value->isFloatValue() || value->isSymbolicValue()))
return Action::None;
const Token* parent = tok->astParent();
// Only if its invertible
if (value->isImpossible() && !Token::Match(parent, "+=|-=|*=|++|--"))
return Action::None;
if (parent && parent->isAssignmentOp() && astIsLHS(tok) &&
parent->astOperand2()->hasKnownValue()) {

View File

@ -3770,6 +3770,17 @@ private:
" return a || ! b || ! a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition '!a' is always true\n", errout.str());
// #10148
check("void f(int i) {\n"
" if (i >= 64) {}\n"
" else if (i >= 32) {\n"
" i &= 31;\n"
" if (i == 0) {}\n"
" else {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void alwaysTrueSymbolic()