AssignIf: detect more problems for 'if' and 'else if' conditions

This commit is contained in:
Daniel Marjamäki 2011-08-01 11:33:09 +02:00
parent 103542bb15
commit 207038a087
3 changed files with 20 additions and 15 deletions

View File

@ -138,7 +138,7 @@ void CheckAssignIf::comparisonError(const Token *tok, bool result)
void CheckAssignIf::multicompare() void CheckAssignIf::multiCondition()
{ {
if (!_settings->_checkCodingStyle) if (!_settings->_checkCodingStyle)
return; return;
@ -167,7 +167,7 @@ void CheckAssignIf::multicompare()
tok2 = tok2->next()->link(); tok2 = tok2->next()->link();
// check condition.. // check condition..
if (Token::Match(opar, "( %varid% == %num% &&|%oror%|)", varid)) if (Token::Match(opar, "( %varid% ==|& %num% &&|%oror%|)", varid))
{ {
const MathLib::bigint num2 = MathLib::toLongNumber(opar->strAt(3)); const MathLib::bigint num2 = MathLib::toLongNumber(opar->strAt(3));
if (num2 < 0) if (num2 < 0)
@ -175,7 +175,7 @@ void CheckAssignIf::multicompare()
if ((num1 & num2) == num2) if ((num1 & num2) == num2)
{ {
multicompareError(opar, tok->linenr()); multiConditionError(opar, tok->linenr());
} }
} }
} }
@ -183,12 +183,11 @@ void CheckAssignIf::multicompare()
} }
} }
void CheckAssignIf::multicompareError(const Token *tok, unsigned int line1) void CheckAssignIf::multiConditionError(const Token *tok, unsigned int line1)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "Comparison is always false because otherwise the condition at line " errmsg << "'else if' condition matches previous condition at line "
<< line1 << line1;
<< " is not false";
reportError(tok, Severity::style, "multicompare", errmsg.str()); reportError(tok, Severity::information, "multiCondition", errmsg.str());
} }

View File

@ -52,7 +52,7 @@ public:
CheckAssignIf checkAssignIf(tokenizer, settings, errorLogger); CheckAssignIf checkAssignIf(tokenizer, settings, errorLogger);
checkAssignIf.assignIf(); checkAssignIf.assignIf();
checkAssignIf.comparison(); checkAssignIf.comparison();
checkAssignIf.multicompare(); checkAssignIf.multiCondition();
} }
/** mismatching assignment / comparison */ /** mismatching assignment / comparison */
@ -62,20 +62,20 @@ public:
void comparison(); void comparison();
/** match 'if' and 'else if' conditions */ /** match 'if' and 'else if' conditions */
void multicompare(); void multiCondition();
private: private:
void assignIfError(const Token *tok, bool result); void assignIfError(const Token *tok, bool result);
void comparisonError(const Token *tok, bool result); void comparisonError(const Token *tok, bool result);
void multicompareError(const Token *tok, unsigned int line1); void multiConditionError(const Token *tok, unsigned int line1);
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.assignIfError(0, false); c.assignIfError(0, false);
c.comparisonError(0, false); c.comparisonError(0, false);
c.multicompareError(0,1); c.multiConditionError(0,1);
} }
std::string myName() const std::string myName() const
@ -88,7 +88,7 @@ private:
return "Match assignments and conditions:\n" return "Match assignments and conditions:\n"
" * Mismatching assignment and comparison => comparison is always true/false\n" " * Mismatching assignment and comparison => comparison is always true/false\n"
" * Mismatching lhs and rhs in comparison => comparison is always true/false\n" " * Mismatching lhs and rhs in comparison => comparison is always true/false\n"
" * Match 'if' and 'else if' conditions"; " * Detect matching 'if' and 'else if' conditions";
} }
}; };
/// @} /// @}

View File

@ -59,7 +59,7 @@ private:
CheckAssignIf checkAssignIf(&tokenizer, &settings, this); CheckAssignIf checkAssignIf(&tokenizer, &settings, this);
checkAssignIf.assignIf(); checkAssignIf.assignIf();
checkAssignIf.comparison(); checkAssignIf.comparison();
checkAssignIf.multicompare(); checkAssignIf.multiCondition();
} }
void assignAndCompare() void assignAndCompare()
@ -107,8 +107,14 @@ private:
" if (x & 7);\n" " if (x & 7);\n"
" else if (x == 1);\n" " else if (x == 1);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Comparison is always false because otherwise the condition at line 3 is not false\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (information) 'else if' condition matches previous condition at line 3\n", errout.str());
check("void foo(int x)\n"
"{\n"
" if (x & 7);\n"
" else if (x & 1);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (information) 'else if' condition matches previous condition at line 3\n", errout.str());
} }
}; };