empty string test: Removed this check

This commit is contained in:
Daniel Marjamäki 2010-11-03 17:56:14 +01:00
parent 19f809c9b4
commit 14803643ca
3 changed files with 1 additions and 100 deletions

View File

@ -64,43 +64,6 @@ void CheckOther::warningOldStylePointerCast()
} }
} }
//---------------------------------------------------------------------------
// "if (strlen(s))" can be rewritten as "if (*s != '\0')"
//---------------------------------------------------------------------------
void CheckOther::checkEmptyStringTest()
{
if (!_settings->_checkCodingStyle)
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
// Non-empty string tests
if (Token::Match(tok, "if ( strlen ( %any% ) )"))
{
emptyStringTestError(tok, tok->strAt(4), false);
}
else if (Token::Match(tok, "strlen ( %any% ) !=|> 0"))
{
emptyStringTestError(tok, tok->strAt(2), false);
}
else if (Token::Match(tok, "0 < strlen ( %any% )"))
{
emptyStringTestError(tok, tok->strAt(4), false);
}
// Empty string tests
else if (Token::Match(tok, "! strlen ( %any% )"))
{
emptyStringTestError(tok, tok->strAt(3), true);
}
else if (Token::Match(tok, "strlen ( %any% ) == 0"))
{
emptyStringTestError(tok, tok->strAt(2), true);
}
}
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// fflush(stdin) <- fflush only applies to output streams in ANSI C // fflush(stdin) <- fflush only applies to output streams in ANSI C
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -2503,20 +2466,6 @@ void CheckOther::mathfunctionCallError(const Token *tok, const unsigned int numP
reportError(tok, Severity::error, "wrongmathcall", "Passing value " " to " "() leads to undefined result"); reportError(tok, Severity::error, "wrongmathcall", "Passing value " " to " "() leads to undefined result");
} }
void CheckOther::emptyStringTestError(const Token *tok, const std::string &var_name, const bool isTestForEmpty)
{
if (isTestForEmpty)
{
reportError(tok, Severity::performance,
"emptyStringTest", "Empty string test can be simplified to \"*" + var_name + " == '\\0'\"");
}
else
{
reportError(tok, Severity::performance,
"emptyStringTest", "Non-empty string test can be simplified to \"*" + var_name + " != '\\0'\"");
}
}
void CheckOther::fflushOnInputStreamError(const Token *tok, const std::string &varname) void CheckOther::fflushOnInputStreamError(const Token *tok, const std::string &varname)
{ {
reportError(tok, Severity::error, reportError(tok, Severity::error,

View File

@ -72,7 +72,6 @@ public:
// Coding style checks // Coding style checks
checkOther.checkConstantFunctionParameter(); checkOther.checkConstantFunctionParameter();
checkOther.checkIncompleteStatement(); checkOther.checkIncompleteStatement();
checkOther.checkEmptyStringTest();
checkOther.invalidFunctionUsage(); checkOther.invalidFunctionUsage();
checkOther.checkZeroDivision(); checkOther.checkZeroDivision();
@ -132,9 +131,6 @@ public:
void lookupVar(const Token *tok1, const std::string &varname); void lookupVar(const Token *tok1, const std::string &varname);
/** @brief %Check for inefficient empty string test*/
void checkEmptyStringTest();
/** @brief %Check for using fflush() on an input stream*/ /** @brief %Check for using fflush() on an input stream*/
void checkFflushOnInputStream(); void checkFflushOnInputStream();
@ -180,7 +176,6 @@ public:
void strPlusChar(const Token *tok); void strPlusChar(const Token *tok);
void zerodivError(const Token *tok); void zerodivError(const Token *tok);
void mathfunctionCallError(const Token *tok, const unsigned int numParam = 1); void mathfunctionCallError(const Token *tok, const unsigned int numParam = 1);
void emptyStringTestError(const Token *tok, const std::string &var_name, const bool isTestForEmpty);
void fflushOnInputStreamError(const Token *tok, const std::string &varname); void fflushOnInputStreamError(const Token *tok, const std::string &varname);
void redundantAssignmentInSwitchError(const Token *tok, const std::string &varname); void redundantAssignmentInSwitchError(const Token *tok, const std::string &varname);
void selfAssignmentError(const Token *tok, const std::string &varname); void selfAssignmentError(const Token *tok, const std::string &varname);
@ -219,9 +214,6 @@ public:
unusedVariableError(0, "varname"); unusedVariableError(0, "varname");
unreadVariableError(0, "varname"); unreadVariableError(0, "varname");
unassignedVariableError(0, "varname"); unassignedVariableError(0, "varname");
// performance
emptyStringTestError(0, "varname", true);
} }
std::string name() const std::string name() const
@ -260,8 +252,7 @@ public:
"* mutual exclusion over || always evaluating to true\n" "* mutual exclusion over || always evaluating to true\n"
// optimisations // optimisations
"* optimisation: detect post increment/decrement\n" "* optimisation: detect post increment/decrement\n";
"* optimisation: simplify empty string tests\n";
} }
private: private:

View File

@ -67,8 +67,6 @@ private:
TEST_CASE(mathfunctionCall1); TEST_CASE(mathfunctionCall1);
TEST_CASE(emptyStringTest);
TEST_CASE(fflushOnInputStreamTest); TEST_CASE(fflushOnInputStreamTest);
TEST_CASE(sizeofsizeof); TEST_CASE(sizeofsizeof);
@ -121,7 +119,6 @@ private:
checkOther.checkZeroDivision(); checkOther.checkZeroDivision();
checkOther.checkMathFunctions(); checkOther.checkMathFunctions();
checkOther.checkEmptyStringTest();
checkOther.checkFflushOnInputStream(); checkOther.checkFflushOnInputStream();
checkOther.checkSelfAssignment(); checkOther.checkSelfAssignment();
checkOther.invalidScanf(); checkOther.invalidScanf();
@ -896,42 +893,6 @@ private:
} }
void emptyStringTest()
{
check("void foo()\n"
"{\n"
" if (strlen(str) == 0)\n"
" {\n"
" std::cout << str;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (performance) Empty string test can be simplified to \"*str == '\\0'\"\n", errout.str());
check("if (!strlen(str)) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Empty string test can be simplified to \"*str == '\\0'\"\n", errout.str());
check("if (strlen(str) == 0) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Empty string test can be simplified to \"*str == '\\0'\"\n", errout.str());
check("if (strlen(str)) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Non-empty string test can be simplified to \"*str != '\\0'\"\n", errout.str());
check("if (strlen(str) > 0) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Non-empty string test can be simplified to \"*str != '\\0'\"\n", errout.str());
check("if (strlen(str) != 0) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Non-empty string test can be simplified to \"*str != '\\0'\"\n", errout.str());
check("if (0 != strlen(str)) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Non-empty string test can be simplified to \"*str != '\\0'\"\n", errout.str());
check("if (0 == strlen(str)) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Empty string test can be simplified to \"*str == '\\0'\"\n", errout.str());
check("if (0 < strlen(str)) { }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Non-empty string test can be simplified to \"*str != '\\0'\"\n", errout.str());
}
void fflushOnInputStreamTest() void fflushOnInputStreamTest()
{ {
check("void foo()\n" check("void foo()\n"