Fixed #162 (Initialisation of a variable by itself)

This commit is contained in:
Zachary Blair 2010-08-14 21:28:22 -07:00
parent 4ff9a1ac94
commit c8087d3389
3 changed files with 73 additions and 0 deletions

View File

@ -349,6 +349,31 @@ void CheckOther::checkRedundantAssignmentInSwitch()
}
}
//---------------------------------------------------------------------------
// int x = 1;
// x = x; // <- redundant assignment to self
//
// int y = y; // <- redundant initialization to self
//---------------------------------------------------------------------------
void CheckOther::checkSelfAssignment()
{
if (!_settings->_checkCodingStyle)
return;
const char selfAssignmentPattern[] = "%var% = %var% ;|=|)";
const Token *tok = Token::findmatch(_tokenizer->tokens(), selfAssignmentPattern);
while (tok)
{
if (tok->varId() && tok->varId() == tok->tokAt(2)->varId())
{
selfAssignmentError(tok, tok->str());
}
tok = Token::findmatch(tok->next(), selfAssignmentPattern);
}
}
//---------------------------------------------------------------------------
// strtol(str, 0, radix) <- radix must be 0 or 2-36
//---------------------------------------------------------------------------
@ -4236,3 +4261,9 @@ void CheckOther::redundantAssignmentInSwitchError(const Token *tok, const std::s
reportError(tok, Severity::style,
"redundantAssignInSwitch", "Redundant assignment of \"" + varname + "\" in switch");
}
void CheckOther::selfAssignmentError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::style,
"selfAssignment", "Redundant assignment of \"" + varname + "\" to itself");
}

View File

@ -85,6 +85,7 @@ public:
checkOther.invalidScanf();
checkOther.nullConstantDereference();
checkOther.checkSelfAssignment();
// New type of check: Check execution paths
checkOther.executionPaths();
@ -192,6 +193,9 @@ public:
/** @brief %Check for assigning to the same variable twice in a switch statement*/
void checkRedundantAssignmentInSwitch();
/** @brief %Check for assigning a variable to itself*/
void checkSelfAssignment();
// Error messages..
void cstyleCastError(const Token *tok);
void redundantIfDelete0Error(const Token *tok);
@ -220,6 +224,7 @@ public:
void fflushOnInputStreamError(const Token *tok, const std::string &varname);
void emptyCatchBlockError(const Token *tok);
void redundantAssignmentInSwitchError(const Token *tok, const std::string &varname);
void selfAssignmentError(const Token *tok, const std::string &varname);
void getErrorMessages()
{
@ -251,6 +256,7 @@ public:
sizeofCalculationError(0);
emptyCatchBlockError(0);
redundantAssignmentInSwitchError(0, "varname");
selfAssignmentError(0, "varname");
invalidScanfError(0);
// optimisations
@ -291,6 +297,7 @@ public:
"* redundant assignment in a switch statement\n"
"* look for 'sizeof sizeof ..'\n"
"* look for calculations inside sizeof()\n"
"* assignment of a variable to itself\n"
// optimisations
"* optimisation: detect post increment/decrement\n"

View File

@ -104,6 +104,7 @@ private:
TEST_CASE(switchRedundantAssignmentTest);
TEST_CASE(selfAssignment);
TEST_CASE(testScanf1);
TEST_CASE(testScanf2);
}
@ -136,6 +137,7 @@ private:
checkOther.checkMathFunctions();
checkOther.checkEmptyStringTest();
checkOther.checkFflushOnInputStream();
checkOther.checkSelfAssignment();
checkOther.invalidScanf();
}
@ -3012,6 +3014,39 @@ private:
ASSERT_EQUALS("", errout.str());
}
void selfAssignment()
{
check("void foo()\n"
"{\n"
" int x = 1;\n"
" x = x;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Redundant assignment of \"x\" to itself\n", errout.str());
check("void foo()\n"
"{\n"
" int x = x;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant assignment of \"x\" to itself\n", errout.str());
check("void foo()\n"
"{\n"
" std::string var = var = \"test\";\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant assignment of \"var\" to itself\n", errout.str());
check("void foo()\n"
"{\n"
" int x = 1;\n"
" x = x + 1;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void testScanf1()
{
check("#include <stdio.h>\n"