AssignIf: Detect mistake in such code: 'int x=y&4; if ((x==3)||..'
This commit is contained in:
parent
9e297c95f2
commit
9f7a0146d0
|
@ -54,13 +54,17 @@ void CheckAssignIf::assignIf()
|
|||
for (const Token *tok2 = tok->tokAt(4); tok2; tok2 = tok2->next()) {
|
||||
if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=")
|
||||
break;
|
||||
if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid)) {
|
||||
const std::string& op(tok2->strAt(3));
|
||||
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
|
||||
if (Token::Match(tok2,"if ( (| %varid% %any% %num% &&|%oror%|)", varid)) {
|
||||
const Token *vartok = tok2->tokAt(tok2->strAt(2)=="(" ? 3 : 2);
|
||||
const std::string& op(vartok->strAt(1));
|
||||
const MathLib::bigint num2 = MathLib::toLongNumber(vartok->strAt(2));
|
||||
std::string condition;
|
||||
if (Token::simpleMatch(tok2, "if ( ("))
|
||||
condition = "'" + vartok->str() + op + vartok->strAt(2) + "'";
|
||||
if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num))
|
||||
assignIfError(tok2, false);
|
||||
assignIfError(tok2, condition, false);
|
||||
else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num))
|
||||
assignIfError(tok2, true);
|
||||
assignIfError(tok2, condition, true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -68,11 +72,11 @@ void CheckAssignIf::assignIf()
|
|||
}
|
||||
}
|
||||
|
||||
void CheckAssignIf::assignIfError(const Token *tok, bool result)
|
||||
void CheckAssignIf::assignIfError(const Token *tok, const std::string &condition, bool result)
|
||||
{
|
||||
reportError(tok, Severity::style,
|
||||
"assignIfError",
|
||||
"Mismatching assignment and comparison, comparison is always " + std::string(result ? "true" : "false") + ".");
|
||||
"Mismatching assignment and comparison, comparison " + condition + (condition.empty()?"":" ") + "is always " + std::string(result ? "true" : "false") + ".");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ public:
|
|||
|
||||
private:
|
||||
|
||||
void assignIfError(const Token *tok, bool result);
|
||||
void assignIfError(const Token *tok, const std::string &condition, bool result);
|
||||
|
||||
void comparisonError(const Token *tok,
|
||||
const std::string &bitop,
|
||||
|
@ -76,7 +76,7 @@ private:
|
|||
|
||||
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
||||
CheckAssignIf c(0, settings, errorLogger);
|
||||
c.assignIfError(0, false);
|
||||
c.assignIfError(0, "", false);
|
||||
c.comparisonError(0, "&", 6, "==", 1, false);
|
||||
c.multiConditionError(0,1);
|
||||
}
|
||||
|
|
|
@ -74,6 +74,12 @@ private:
|
|||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true.\n", errout.str());
|
||||
|
||||
check("void foo(int x) {\n"
|
||||
" int y = x & 4;\n"
|
||||
" if ((y == 3) && (z == 1));\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==3' is always false.\n", errout.str());
|
||||
|
||||
// |
|
||||
check("void foo(int x) {\n"
|
||||
" int y = x | 0x14;\n"
|
||||
|
|
Loading…
Reference in New Issue