AssignIf: Detect problem for 'y=x&4; if(y!=3)..'. Ticket: #2909
This commit is contained in:
parent
47f13860b7
commit
e47aac2501
|
@ -56,12 +56,14 @@ void CheckAssignIf::check()
|
||||||
{
|
{
|
||||||
if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=")
|
if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=")
|
||||||
break;
|
break;
|
||||||
if (Token::Match(tok2,"if ( %varid% %any% %num% ) ", varid))
|
if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid))
|
||||||
{
|
{
|
||||||
const std::string op(tok2->strAt(3));
|
const std::string op(tok2->strAt(3));
|
||||||
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
|
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
|
||||||
if (op == "==" && (num & num2) != num2)
|
if (op == "==" && (num & num2) != num2)
|
||||||
mismatchError(tok2);
|
mismatchError(tok2, false);
|
||||||
|
else if (op == "!=" && (num & num2) != num2)
|
||||||
|
mismatchError(tok2, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,9 +71,9 @@ void CheckAssignIf::check()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckAssignIf::mismatchError(const Token *tok)
|
void CheckAssignIf::mismatchError(const Token *tok, bool result)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style,
|
reportError(tok, Severity::style,
|
||||||
"assignIfMismatchError",
|
"assignIfMismatchError",
|
||||||
"Mismatching assignment and condition, condition is always false");
|
"Mismatching assignment and comparison, comparison is always " + std::string(result ? "true" : "false"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,12 +58,12 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void mismatchError(const Token *tok);
|
void mismatchError(const Token *tok, bool result);
|
||||||
|
|
||||||
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
|
||||||
{
|
{
|
||||||
CheckAssignIf c(0, settings, errorLogger);
|
CheckAssignIf c(0, settings, errorLogger);
|
||||||
c.mismatchError(0);
|
c.mismatchError(0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string myName() const
|
std::string myName() const
|
||||||
|
@ -74,7 +74,7 @@ private:
|
||||||
std::string classInfo() const
|
std::string classInfo() const
|
||||||
{
|
{
|
||||||
return "Match assignments and conditions:\n"
|
return "Match assignments and conditions:\n"
|
||||||
" mismatching assignment and condition";
|
" Mismatching assignment and comparison => comparison is always true/false";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/// @}
|
/// @}
|
||||||
|
|
|
@ -65,7 +65,14 @@ private:
|
||||||
" int y = x & 4;\n"
|
" int y = x & 4;\n"
|
||||||
" if (y == 3);\n"
|
" if (y == 3);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and condition, condition is always false\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always false\n", errout.str());
|
||||||
|
|
||||||
|
check("void foo(int x)\n"
|
||||||
|
"{\n"
|
||||||
|
" int y = x & 4;\n"
|
||||||
|
" if (y != 3);\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true\n", errout.str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue