From a64669b1ecb58480f5177a61eaaf12845c9f32d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 30 Sep 2012 17:22:35 +0200 Subject: [PATCH] Fixed #4203 (Don't warn about setting NULL value for pointers and not using that value) --- .travis.yml | 2 +- lib/checkunusedvar.cpp | 2 +- test/testunusedvar.cpp | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7f6d1f054..a2b6281ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,4 @@ compiler: script: - make test - $CXX -o cppcheck -O2 cli/*.cpp lib/*.cpp -Ilib - - ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=unreadVariable --suppress=duplicateBranch --suppress=stlIfStrFind -q cli gui lib -igui/test + - ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=duplicateBranch --suppress=stlIfStrFind -q cli gui lib -igui/test diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 095298b65..31baf351e 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -250,7 +250,7 @@ void Variables::write(unsigned int varid, const Token* tok) if (usage) { usage->_write = true; - if (!usage->_var->isStatic()) + if (!usage->_var->isStatic() && !Token::simpleMatch(tok->next(), "= 0 ;")) usage->_read = false; usage->_lastAccess = tok; } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index fbccd5e97..c895c719e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -144,6 +144,7 @@ private: TEST_CASE(localvarIfNOT); // #3104 - if ( NOT var ) TEST_CASE(localvarAnd); // #3672 TEST_CASE(localvarSwitch); // #3744 - false positive when localvar is used in switch + TEST_CASE(localvarNULL); // #4203 - Setting NULL value is not redundant - it is safe TEST_CASE(crash1); } @@ -3317,6 +3318,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarNULL() { // #4203 - Setting NULL value is not redundant - it is safe + functionVariableUsage("void f() {\n" + " char *p = malloc(100);\n" + " foo(p);\n" + " free(p);\n" + " p = NULL;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void crash1() { functionVariableUsage("SAL_WNODEPRECATED_DECLARATIONS_PUSH\n" "void convertToTokenArray() {\n"