From 06a77679d442849e6a5eae64b5ecb0862bf6efe8 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 11 May 2012 10:38:19 -0700 Subject: [PATCH] Refactorizations: - Added support for pointers in self assignement check - Removed redundant for loop in checknullpointer.cpp - Fixed warning about signed/unsigned mismatch in cppcheck.cpp by making Settings::_maxConfig unsigned --- lib/checknullpointer.cpp | 7 ++----- lib/checkother.cpp | 2 +- lib/cppcheck.cpp | 2 +- lib/settings.h | 2 +- test/testother.cpp | 6 ++++++ 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 3a7300274..687b1de10 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -212,11 +212,8 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::listnextArgument(); // Find third parameter (first argument of va_args) formatString = formatStringTok->strValue(); } - } else if (Token::Match(&tok, "snprintf|fnprintf")) { - const Token* formatStringTok = secondParam; - for (int i = 0; i < 1 && formatStringTok; i++) { - formatStringTok = formatStringTok->nextArgument(); // Find third parameter (format string) - } + } else if (Token::Match(&tok, "snprintf|fnprintf") && secondParam) { + const Token* formatStringTok = secondParam->nextArgument(); // Find third parameter (format string) if (formatStringTok && formatStringTok->type() == Token::eString) { argListTok = formatStringTok->nextArgument(); // Find fourth parameter (first argument of va_args) formatString = formatStringTok->strValue(); diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c0e2c1944..2d950f3b9 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -894,7 +894,7 @@ void CheckOther::coutCerrMisusageError(const Token* tok, const std::string& stre static bool isPOD(const Variable* var) { // TODO: Implement real support for POD definition - return(var && var->nameToken()->previous()->isStandardType()); + return(var && (var->isPointer() || var->nameToken()->previous()->isStandardType())); } void CheckOther::checkSelfAssignment() diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 57dcd925b..a6f1606c6 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -192,7 +192,7 @@ unsigned int CppCheck::processFile(const std::string& filename) reportErr(errmsg); } - int checkCount = 0; + unsigned int checkCount = 0; for (std::list::const_iterator it = configurations.begin(); it != configurations.end(); ++it) { // Check only a few configurations (default 12), after that bail out, unless --force // was used. diff --git a/lib/settings.h b/lib/settings.h index c053e79d6..4c93960ef 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -130,7 +130,7 @@ public: /** @brief Maximum number of configurations to check before bailing. Default is 12. (--max-configs=N) */ - int _maxConfigs; + unsigned int _maxConfigs; /** * @brief Returns true if given id is in the list of diff --git a/test/testother.cpp b/test/testother.cpp index c7c308751..352d8416d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2102,6 +2102,12 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void foo() {\n" + " BAR *x = getx();\n" + " x = x;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of \"x\" to itself\n", errout.str()); + // non-primitive type -> there might be some side effects check("void foo()\n" "{\n"