Fixed #3356 (false positive: (warning) Comparison of a boolean with integer that is neither 1 nor 0)

This commit is contained in:
Daniel Marjamäki 2011-11-29 18:14:57 +01:00
parent b538f49a6e
commit b61c01c6a4
2 changed files with 8 additions and 2 deletions

View File

@ -1364,7 +1364,7 @@ void CheckOther::checkComparisonOfBoolWithInt()
std::map<unsigned int, bool>::const_iterator iVar = boolvars.find(varTok->varId()); std::map<unsigned int, bool>::const_iterator iVar = boolvars.find(varTok->varId());
if (iVar != boolvars.end() && iVar->second && // Variable has to be a boolean if (iVar != boolvars.end() && iVar->second && // Variable has to be a boolean
((tok->strAt(1) != "==" && tok->strAt(1) != "!=") || ((tok->strAt(1) != "==" && tok->strAt(1) != "!=") ||
((MathLib::toLongNumber(numTok->str()) != 0) && (!_tokenizer->code_is_c() || MathLib::toLongNumber(numTok->str()) != 1)))) { // == 0 and != 0 are allowed, for C also == 1 and != 1 (MathLib::toLongNumber(numTok->str()) != 0 && MathLib::toLongNumber(numTok->str()) != 1))) { // == 0 and != 0 are allowed, for C also == 1 and != 1
comparisonOfBoolWithIntError(varTok, numTok->str()); comparisonOfBoolWithIntError(varTok, numTok->str());
} }
} else if (Token::Match(tok, "%num% >|>=|==|!=|<=|< %var%")) { // Comparing number with variable } else if (Token::Match(tok, "%num% >|>=|==|!=|<=|< %var%")) { // Comparing number with variable
@ -1373,7 +1373,7 @@ void CheckOther::checkComparisonOfBoolWithInt()
std::map<unsigned int, bool>::const_iterator iVar = boolvars.find(varTok->varId()); std::map<unsigned int, bool>::const_iterator iVar = boolvars.find(varTok->varId());
if (iVar != boolvars.end() && iVar->second && // Variable has to be a boolean if (iVar != boolvars.end() && iVar->second && // Variable has to be a boolean
((tok->strAt(1) != "==" && tok->strAt(1) != "!=") || ((tok->strAt(1) != "==" && tok->strAt(1) != "!=") ||
((MathLib::toLongNumber(numTok->str()) != 0) && (!_tokenizer->code_is_c() || MathLib::toLongNumber(numTok->str()) != 1)))) { // == 0 and != 0 are allowed, for C also == 1 and != 1 (MathLib::toLongNumber(numTok->str()) != 0 && MathLib::toLongNumber(numTok->str()) != 1))) { // == 0 and != 0 are allowed, for C also == 1 and != 1
comparisonOfBoolWithIntError(varTok, numTok->str()); comparisonOfBoolWithIntError(varTok, numTok->str());
} }
} else if (Token::Match(tok, "true|false >|>=|==|!=|<=|< %var%")) { // Comparing boolean constant with variable } else if (Token::Match(tok, "true|false >|>=|==|!=|<=|< %var%")) { // Comparing boolean constant with variable

View File

@ -3379,6 +3379,12 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f(bool x) {\n" // #3356
" if (x == 1) {\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(bool x) {\n" check("void f(bool x) {\n"
" if (x != 10) {\n" " if (x != 10) {\n"
" printf(\"foo\");\n" " printf(\"foo\");\n"