From 14803643ca64e5c3060902fd80b19df85e22655e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 3 Nov 2010 17:56:14 +0100 Subject: [PATCH] empty string test: Removed this check --- lib/checkother.cpp | 51 ---------------------------------------------- lib/checkother.h | 11 +--------- test/testother.cpp | 39 ----------------------------------- 3 files changed, 1 insertion(+), 100 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4e70dbbc1..2830e26a8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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 //--------------------------------------------------------------------------- @@ -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"); } -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) { reportError(tok, Severity::error, diff --git a/lib/checkother.h b/lib/checkother.h index 2465f1190..4644832c8 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -72,7 +72,6 @@ public: // Coding style checks checkOther.checkConstantFunctionParameter(); checkOther.checkIncompleteStatement(); - checkOther.checkEmptyStringTest(); checkOther.invalidFunctionUsage(); checkOther.checkZeroDivision(); @@ -132,9 +131,6 @@ public: 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*/ void checkFflushOnInputStream(); @@ -180,7 +176,6 @@ public: void strPlusChar(const Token *tok); void zerodivError(const Token *tok); 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 redundantAssignmentInSwitchError(const Token *tok, const std::string &varname); void selfAssignmentError(const Token *tok, const std::string &varname); @@ -219,9 +214,6 @@ public: unusedVariableError(0, "varname"); unreadVariableError(0, "varname"); unassignedVariableError(0, "varname"); - - // performance - emptyStringTestError(0, "varname", true); } std::string name() const @@ -260,8 +252,7 @@ public: "* mutual exclusion over || always evaluating to true\n" // optimisations - "* optimisation: detect post increment/decrement\n" - "* optimisation: simplify empty string tests\n"; + "* optimisation: detect post increment/decrement\n"; } private: diff --git a/test/testother.cpp b/test/testother.cpp index 0cf5b4110..fe097ebd3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -67,8 +67,6 @@ private: TEST_CASE(mathfunctionCall1); - TEST_CASE(emptyStringTest); - TEST_CASE(fflushOnInputStreamTest); TEST_CASE(sizeofsizeof); @@ -121,7 +119,6 @@ private: checkOther.checkZeroDivision(); checkOther.checkMathFunctions(); - checkOther.checkEmptyStringTest(); checkOther.checkFflushOnInputStream(); checkOther.checkSelfAssignment(); 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() { check("void foo()\n"