diff --git a/CheckOther.cpp b/CheckOther.cpp index 1ce2683f8..580e16904 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -710,14 +710,14 @@ void CheckIncompleteStatement() if ( parlevel != 0 ) continue; - if ( Match(tok,"; %str%") && !Match(gettok(tok,2), ",") ) + if ( !Match(tok,"#") && Match(tok->next,"; %str%") && !Match(gettok(tok,3), ",") ) { std::ostringstream errmsg; errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with string constant"; ReportErr(errmsg.str()); } - if ( Match(tok,"; %num%") && !Match(gettok(tok,2), ",") ) + if ( !Match(tok,"#") && Match(tok->next,"; %num%") && !Match(gettok(tok,3), ",") ) { std::ostringstream errmsg; errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with numeric constant"; diff --git a/Makefile b/Makefile index bc2dffe08..d4bd7ab54 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckOther.cpp CommonCheck.cpp tokenize.cpp OBJS=$(SRCS:%.cpp=%.o) -TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testmemleak.o testunusedvar.o +TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testmemleak.o testother.o testunusedvar.o %.o: %.cpp g++ -Wall -pedantic -g -I. -o $@ -c $^ diff --git a/testother.cpp b/testother.cpp new file mode 100644 index 000000000..0f23c1343 --- /dev/null +++ b/testother.cpp @@ -0,0 +1,70 @@ + +// Check for unused variables.. + +#include "testsuite.h" +#include "tokenize.h" +#include "CheckOther.h" + +#include + +extern std::ostringstream errout; + +class TestOther : public TestFixture +{ +public: + TestOther() : TestFixture("TestOther") + { } + +private: + void check( const char code[] ) + { + // Tokenize.. + tokens = tokens_back = NULL; + std::istringstream istr(code); + TokenizeCode( istr ); + SimplifyTokenList(); + + // Clear the error buffer.. + errout.str(""); + + // Check for unused variables.. + CheckIncompleteStatement(); + } + + void run() + { + TEST_CASE( test1 ); + TEST_CASE( test2 ); + } + + void test1() + { + check( "void foo()\n" + "{\n" + " const char def[] =\n" + "#ifdef ABC\n" + " \"abc\";\n" + "#else\n" + " \"not abc\";\n" + "#endif\n" + "}\n" ); + + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + void test2() + { + // Todo: remove the ';' before the string + + check( "void foo()\n" + "{\n" + " ;\"abc\";\n" + "}\n" ); + + ASSERT_EQUALS( std::string("[test.cpp:3]: Redundant code: Found a statement that begins with string constant\n"), errout.str() ); + } +}; + +REGISTER_TEST( TestOther ) + +