2008-12-19 22:15:18 +01:00
/*
2009-01-21 21:04:20 +01:00
* Cppcheck - A tool for static C / C + + code analysis
2013-01-01 17:29:08 +01:00
* Copyright ( C ) 2007 - 2013 Daniel Marjamäki and Cppcheck team .
2008-12-19 22:15:18 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2009-09-27 17:08:31 +02:00
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
2008-12-19 22:15:18 +01:00
*/
2011-02-19 09:33:29 +01:00
# include "preprocessor.h"
2009-10-25 12:49:06 +01:00
# include "tokenize.h"
# include "checkother.h"
2008-12-19 22:15:18 +01:00
# include "testsuite.h"
# include <sstream>
extern std : : ostringstream errout ;
2011-10-13 20:53:06 +02:00
class TestOther : public TestFixture {
2008-12-19 22:15:18 +01:00
public :
TestOther ( ) : TestFixture ( " TestOther " )
{ }
private :
2011-10-13 20:53:06 +02:00
void run ( ) {
2012-10-21 18:18:29 +02:00
TEST_CASE ( oppositeInnerCondition ) ;
2012-12-28 20:16:51 +01:00
TEST_CASE ( emptyBrackets ) ;
2011-07-28 08:12:21 +02:00
2009-03-28 07:49:47 +01:00
TEST_CASE ( zeroDiv1 ) ;
TEST_CASE ( zeroDiv2 ) ;
2009-08-23 05:34:19 +02:00
TEST_CASE ( zeroDiv3 ) ;
2009-08-30 21:02:09 +02:00
TEST_CASE ( zeroDiv4 ) ;
2011-08-24 02:12:29 +02:00
TEST_CASE ( zeroDiv5 ) ;
2011-09-10 16:12:53 +02:00
TEST_CASE ( zeroDiv6 ) ;
2009-03-28 07:49:47 +01:00
2009-01-15 17:57:51 +01:00
TEST_CASE ( sprintf1 ) ; // Dangerous usage of sprintf
2009-01-10 12:19:17 +01:00
TEST_CASE ( sprintf2 ) ;
2009-01-10 15:29:59 +01:00
TEST_CASE ( sprintf3 ) ;
2009-01-15 17:57:51 +01:00
TEST_CASE ( sprintf4 ) ; // struct member
2010-02-01 19:46:51 +01:00
TEST_CASE ( strPlusChar1 ) ; // "/usr" + '/'
TEST_CASE ( strPlusChar2 ) ; // "/usr" + ch
TEST_CASE ( strPlusChar3 ) ; // ok: path + "/sub" + '/'
2009-02-04 19:49:19 +01:00
2009-03-24 20:24:03 +01:00
TEST_CASE ( varScope1 ) ;
2009-03-24 20:59:56 +01:00
TEST_CASE ( varScope2 ) ;
2009-05-27 19:38:26 +02:00
TEST_CASE ( varScope3 ) ;
2009-06-12 16:17:51 +02:00
TEST_CASE ( varScope4 ) ;
2009-08-16 21:12:57 +02:00
TEST_CASE ( varScope5 ) ;
2009-09-17 21:05:12 +02:00
TEST_CASE ( varScope6 ) ;
2009-10-06 17:45:28 +02:00
TEST_CASE ( varScope7 ) ;
2010-01-21 18:50:56 +01:00
TEST_CASE ( varScope8 ) ;
2011-10-05 20:30:36 +02:00
TEST_CASE ( varScope9 ) ; // classes may have extra side-effects
2010-08-26 21:57:48 +02:00
TEST_CASE ( varScope10 ) ; // Undefined macro FOR
2011-10-05 20:30:36 +02:00
TEST_CASE ( varScope11 ) ; // #2475 - struct initialization is not inner scope
2012-05-24 15:34:59 +02:00
TEST_CASE ( varScope12 ) ;
TEST_CASE ( varScope13 ) ; // variable usage in inner loop
2012-07-29 15:39:43 +02:00
TEST_CASE ( varScope14 ) ;
2013-02-12 21:16:57 +01:00
TEST_CASE ( varScope15 ) ; // #4573 if-else-if
2013-03-03 19:35:33 +01:00
TEST_CASE ( varScope16 ) ;
2013-03-15 13:00:51 +01:00
TEST_CASE ( varScope17 ) ;
2013-03-23 11:28:33 +01:00
TEST_CASE ( varScope18 ) ;
2009-03-27 17:19:34 +01:00
2009-06-18 22:26:21 +02:00
TEST_CASE ( oldStylePointerCast ) ;
2012-02-26 11:56:32 +01:00
TEST_CASE ( invalidPointerCast ) ;
2009-07-25 00:36:15 +02:00
2009-10-04 10:05:58 +02:00
TEST_CASE ( dangerousStrolUsage ) ;
TEST_CASE ( passedByValue ) ;
2010-04-02 02:19:38 +02:00
2010-04-02 07:32:03 +02:00
TEST_CASE ( mathfunctionCall1 ) ;
2011-11-17 16:31:16 +01:00
TEST_CASE ( cctypefunctionCall ) ;
2010-04-13 19:30:25 +02:00
2010-06-30 09:10:30 +02:00
TEST_CASE ( switchRedundantAssignmentTest ) ;
2012-08-22 14:40:57 +02:00
TEST_CASE ( switchRedundantOperationTest ) ;
2012-05-29 06:19:22 +02:00
TEST_CASE ( switchRedundantBitwiseOperationTest ) ;
2011-02-19 09:33:29 +01:00
TEST_CASE ( switchFallThroughCase ) ;
2011-12-07 18:20:52 +01:00
TEST_CASE ( unreachableCode ) ;
2010-08-14 15:15:12 +02:00
2012-12-07 21:27:32 +01:00
TEST_CASE ( suspiciousCase ) ;
2013-01-18 08:03:04 +01:00
TEST_CASE ( suspiciousEqualityComparison ) ;
2012-12-07 21:27:32 +01:00
2010-08-15 06:28:22 +02:00
TEST_CASE ( selfAssignment ) ;
2010-10-01 17:23:22 +02:00
TEST_CASE ( trac1132 ) ;
2011-03-20 09:55:26 +01:00
TEST_CASE ( testMisusedScopeObjectDoesNotPickFunction1 ) ;
TEST_CASE ( testMisusedScopeObjectDoesNotPickFunction2 ) ;
2010-10-01 17:23:22 +02:00
TEST_CASE ( testMisusedScopeObjectPicksClass ) ;
TEST_CASE ( testMisusedScopeObjectPicksStruct ) ;
TEST_CASE ( testMisusedScopeObjectDoesNotPickIf ) ;
TEST_CASE ( testMisusedScopeObjectDoesNotPickConstructorDeclaration ) ;
2010-10-02 10:43:12 +02:00
TEST_CASE ( testMisusedScopeObjectDoesNotPickFunctor ) ;
2010-10-02 14:59:04 +02:00
TEST_CASE ( testMisusedScopeObjectDoesNotPickLocalClassConstructors ) ;
2010-10-02 12:26:29 +02:00
TEST_CASE ( testMisusedScopeObjectDoesNotPickUsedObject ) ;
2010-12-25 08:43:52 +01:00
TEST_CASE ( testMisusedScopeObjectDoesNotPickPureC ) ;
2011-01-31 20:07:41 +01:00
TEST_CASE ( testMisusedScopeObjectDoesNotPickNestedClass ) ;
2010-10-03 23:16:11 +02:00
TEST_CASE ( trac2071 ) ;
2010-10-13 11:31:41 +02:00
TEST_CASE ( trac2084 ) ;
2012-04-12 14:02:09 +02:00
TEST_CASE ( trac3693 ) ;
2012-04-26 15:23:47 +02:00
TEST_CASE ( modulo ) ;
2011-07-17 04:06:23 +02:00
TEST_CASE ( incorrectLogicOperator1 ) ;
TEST_CASE ( incorrectLogicOperator2 ) ;
2012-03-15 20:38:28 +01:00
TEST_CASE ( incorrectLogicOperator3 ) ;
2011-08-19 19:28:37 +02:00
TEST_CASE ( secondAlwaysTrueFalseWhenFirstTrueError ) ;
2012-03-12 19:06:30 +01:00
TEST_CASE ( incorrectLogicOp_condSwapping ) ;
2012-06-23 19:54:15 +02:00
TEST_CASE ( sameExpression ) ;
2011-07-17 04:06:23 +02:00
2011-01-06 11:31:58 +01:00
TEST_CASE ( memsetZeroBytes ) ;
2011-01-22 19:21:56 +01:00
2013-02-11 20:26:27 +01:00
TEST_CASE ( redundantGetAndSetUserId ) ;
2011-01-24 21:40:49 +01:00
TEST_CASE ( clarifyCalculation ) ;
2012-08-24 11:28:50 +02:00
TEST_CASE ( clarifyStatement ) ;
2011-02-08 19:49:29 +01:00
2011-08-19 00:14:15 +02:00
TEST_CASE ( clarifyCondition1 ) ; // if (a = b() < 0)
TEST_CASE ( clarifyCondition2 ) ; // if (a & b == c)
2011-08-19 13:40:54 +02:00
TEST_CASE ( clarifyCondition3 ) ; // if (! a & b)
2011-09-13 02:46:00 +02:00
TEST_CASE ( clarifyCondition4 ) ; // ticket #3110
2012-04-08 18:07:11 +02:00
TEST_CASE ( clarifyCondition5 ) ; // #3609 CWinTraits<WS_CHILD|WS_VISIBLE>..
2012-08-02 13:03:01 +02:00
TEST_CASE ( clarifyCondition6 ) ; // #3818
2011-10-10 19:11:17 +02:00
2011-02-08 19:49:29 +01:00
TEST_CASE ( incorrectStringCompare ) ;
2011-02-11 23:38:23 +01:00
2011-04-09 21:14:01 +02:00
TEST_CASE ( duplicateIf ) ;
2012-05-14 09:57:30 +02:00
TEST_CASE ( duplicateIf1 ) ; // ticket 3689
2011-04-09 23:05:27 +02:00
TEST_CASE ( duplicateBranch ) ;
2012-07-31 23:35:56 +02:00
TEST_CASE ( duplicateBranch1 ) ; // tests extracted by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck ): Errors detected in Quake 3: Arena by PVS-Studio: Fragement 2
2013-02-18 17:18:33 +01:00
TEST_CASE ( duplicateBranch2 ) ; // empty macro
2011-04-19 01:20:27 +02:00
TEST_CASE ( duplicateExpression1 ) ;
TEST_CASE ( duplicateExpression2 ) ; // ticket #2730
2011-11-10 21:49:14 +01:00
TEST_CASE ( duplicateExpression3 ) ; // ticket #3317
2011-12-01 17:46:33 +01:00
TEST_CASE ( duplicateExpression4 ) ; // ticket #3354 (++)
2012-05-17 07:26:57 +02:00
TEST_CASE ( duplicateExpression5 ) ; // ticket #3749 (macros with same values)
2013-03-14 19:11:29 +01:00
TEST_CASE ( duplicateExpression6 ) ; // ticket #4639
2011-04-26 07:45:27 +02:00
TEST_CASE ( alwaysTrueFalseStringCompare ) ;
2012-08-24 14:25:17 +02:00
TEST_CASE ( suspiciousStringCompare ) ;
2011-08-07 01:23:09 +02:00
TEST_CASE ( checkSignOfUnsignedVariable ) ;
2012-08-21 12:28:02 +02:00
TEST_CASE ( checkSignOfPointer ) ;
2011-10-11 08:41:39 +02:00
TEST_CASE ( checkForSuspiciousSemicolon1 ) ;
TEST_CASE ( checkForSuspiciousSemicolon2 ) ;
2012-01-15 01:19:34 +01:00
2012-08-24 00:03:22 +02:00
TEST_CASE ( checkDoubleFree ) ;
2012-09-05 08:31:23 +02:00
TEST_CASE ( checkInvalidFree ) ;
2012-07-22 09:17:00 +02:00
TEST_CASE ( checkRedundantCopy ) ;
2012-08-22 15:44:20 +02:00
TEST_CASE ( checkNegativeShift ) ;
2012-08-24 11:28:50 +02:00
TEST_CASE ( incompleteArrayFill ) ;
2012-09-02 13:09:32 +02:00
TEST_CASE ( redundantVarAssignment ) ;
TEST_CASE ( redundantMemWrite ) ;
2013-01-13 12:02:10 +01:00
TEST_CASE ( varFuncNullUB ) ;
2013-02-17 17:33:32 +01:00
TEST_CASE ( checkPipeParameterSize ) ; // ticket #3521
2013-02-27 21:02:12 +01:00
TEST_CASE ( checkCastIntToCharAndBack ) ; // ticket #160
2013-03-11 17:04:30 +01:00
TEST_CASE ( checkSleepTimeIntervall )
2008-12-19 22:15:18 +01:00
}
2013-04-13 20:17:53 +02:00
void check ( const char code [ ] , const char * filename = NULL , bool experimental = false , bool inconclusive = true , bool posix = false , bool runSimpleChecks = true ) {
2010-12-01 18:00:55 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2013-02-11 20:26:27 +01:00
settings . addEnabled ( " warning " ) ;
2012-08-24 11:28:50 +02:00
settings . addEnabled ( " portability " ) ;
2012-09-02 13:09:32 +02:00
settings . addEnabled ( " performance " ) ;
2012-04-05 10:38:29 +02:00
settings . inconclusive = inconclusive ;
2011-11-21 07:31:06 +01:00
settings . experimental = experimental ;
2013-02-11 20:26:27 +01:00
settings . standards . posix = posix ;
2010-12-01 18:00:55 +01:00
2008-12-19 22:15:18 +01:00
// Tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2008-12-19 22:15:18 +01:00
std : : istringstream istr ( code ) ;
2010-12-25 08:43:52 +01:00
tokenizer . tokenize ( istr , filename ? filename : " test.cpp " ) ;
2008-12-19 22:15:18 +01:00
2010-04-21 08:38:25 +02:00
// Check..
2009-03-20 18:16:21 +01:00
CheckOther checkOther ( & tokenizer , & settings , this ) ;
2011-10-23 12:20:43 +02:00
checkOther . runChecks ( & tokenizer , & settings , this ) ;
2010-05-15 14:06:45 +02:00
2013-04-13 20:17:53 +02:00
if ( runSimpleChecks ) {
const std : : string str1 ( tokenizer . tokens ( ) - > stringifyList ( 0 , true ) ) ;
tokenizer . simplifyTokenList ( ) ;
const std : : string str2 ( tokenizer . tokens ( ) - > stringifyList ( 0 , true ) ) ;
if ( str1 ! = str2 )
warn ( ( " Unsimplified code in test case \n str1= " + str1 + " \n str2= " + str2 ) . c_str ( ) ) ;
checkOther . runSimplifiedChecks ( & tokenizer , & settings , this ) ;
}
2008-12-19 22:15:18 +01:00
}
2011-10-13 20:53:06 +02:00
class SimpleSuppressor : public ErrorLogger {
2011-02-19 09:33:29 +01:00
public :
SimpleSuppressor ( Settings & settings , ErrorLogger * next )
: _settings ( settings ) , _next ( next )
{ }
2011-10-13 20:53:06 +02:00
virtual void reportOut ( const std : : string & outmsg ) {
2011-02-19 09:33:29 +01:00
_next - > reportOut ( outmsg ) ;
}
2011-10-13 20:53:06 +02:00
virtual void reportErr ( const ErrorLogger : : ErrorMessage & msg ) {
2011-02-19 09:33:29 +01:00
if ( ! msg . _callStack . empty ( ) & & ! _settings . nomsg . isSuppressed ( msg . _id , msg . _callStack . begin ( ) - > getfile ( ) , msg . _callStack . begin ( ) - > line ) )
_next - > reportErr ( msg ) ;
}
private :
Settings & _settings ;
ErrorLogger * _next ;
} ;
2011-10-13 20:53:06 +02:00
void check_preprocess_suppress ( const char precode [ ] , const char * filename = NULL ) {
2011-02-19 09:33:29 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
if ( filename = = NULL )
filename = " test.cpp " ;
Settings settings ;
2013-03-03 11:41:59 +01:00
settings . addEnabled ( " warning " ) ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2011-09-03 15:30:30 +02:00
settings . addEnabled ( " performance " ) ;
2011-04-10 15:55:08 +02:00
settings . experimental = true ;
2011-02-19 09:33:29 +01:00
// Preprocess file..
2012-01-06 08:01:50 +01:00
SimpleSuppressor logger ( settings , this ) ;
Preprocessor preprocessor ( & settings , & logger ) ;
2011-02-19 09:33:29 +01:00
std : : list < std : : string > configurations ;
std : : string filedata = " " ;
std : : istringstream fin ( precode ) ;
preprocessor . preprocess ( fin , filedata , configurations , filename , settings . _includePaths ) ;
2012-01-06 08:01:50 +01:00
const std : : string code = preprocessor . getcode ( filedata , " " , filename ) ;
2011-02-19 09:33:29 +01:00
// Tokenize..
Tokenizer tokenizer ( & settings , & logger ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , filename ) ;
2011-03-04 08:26:14 +01:00
tokenizer . simplifyGoto ( ) ;
2011-02-19 09:33:29 +01:00
// Check..
CheckOther checkOther ( & tokenizer , & settings , & logger ) ;
checkOther . checkSwitchCaseFallThrough ( ) ;
2011-04-26 07:45:27 +02:00
checkOther . checkAlwaysTrueOrFalseStringCompare ( ) ;
2011-02-19 20:02:28 +01:00
logger . reportUnmatchedSuppressions ( settings . nomsg . getUnmatchedLocalSuppressions ( filename ) ) ;
2011-02-19 09:33:29 +01:00
}
2009-03-27 17:19:34 +01:00
2012-10-21 18:18:29 +02:00
void oppositeInnerCondition ( ) {
2012-09-05 13:48:00 +02:00
check ( " void foo(int a, int b) \n "
" { \n "
" if(a==b) \n "
" if(a!=b) \n "
" cout << a; \n "
2012-10-21 18:18:29 +02:00
" } " , " test.cpp " , true , true ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Opposite conditions in nested 'if' blocks lead to a dead code block. \n " , errout . str ( ) ) ;
2012-09-05 13:48:00 +02:00
2012-12-06 19:19:22 +01:00
check ( " void foo(int i) \n "
2012-09-05 13:48:00 +02:00
" { \n "
" if(i > 5) { \n "
" i = bar(); \n "
" if(i < 5) { \n "
2012-12-06 19:19:22 +01:00
" cout << a; \n "
2012-09-05 13:48:00 +02:00
" } \n "
" } \n "
2012-10-21 18:18:29 +02:00
" } " , " test.cpp " , true , true ) ;
2012-09-05 13:48:00 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int& i) \n "
" { \n "
" i=6; \n "
" } \n "
" void bar(int i) \n "
" { \n "
" if(i>5){ \n "
" foo(i); \n "
" if(i<5){ \n "
" } \n "
" } \n "
2012-10-21 18:18:29 +02:00
" } " , " test.cpp " , true , true ) ;
2012-09-05 13:48:00 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-12-28 20:16:51 +01:00
void emptyBrackets ( ) {
check ( " { \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-07-28 07:28:24 +02:00
2011-10-13 20:53:06 +02:00
void zeroDiv1 ( ) {
2013-04-13 20:17:53 +02:00
check ( " void foo() { \n "
" cout << 1. / 0; \n "
2009-03-28 07:49:47 +01:00
" } " ) ;
2013-04-13 20:17:53 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n " , errout . str ( ) ) ;
2009-03-28 07:49:47 +01:00
}
2011-10-13 20:53:06 +02:00
void zeroDiv2 ( ) {
2009-03-28 07:49:47 +01:00
check ( " void foo() \n "
" { \n "
" int sum = 0; \n "
" for(int i = 0; i < n; i ++) \n "
" { \n "
2012-12-06 19:19:22 +01:00
" sum += i; \n "
2009-03-28 07:49:47 +01:00
" } \n "
" cout<<b/sum; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-06-05 23:33:13 +02:00
}
2009-03-28 07:49:47 +01:00
2011-10-13 20:53:06 +02:00
void zeroDiv3 ( ) {
2009-08-23 05:34:19 +02:00
check ( " void f() \n "
" { \n "
" div_t divresult = div (1,0); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
2009-08-23 05:34:19 +02:00
}
2011-10-13 20:53:06 +02:00
void zeroDiv4 ( ) {
2009-08-30 21:02:09 +02:00
check ( " void f() \n "
" { \n "
" long a = b / 0x6; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-08-30 21:02:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" long a = b / 0x0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
2009-08-30 21:02:09 +02:00
check ( " void f() \n "
" { \n "
" long a = b / 0L; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
2009-08-30 21:02:09 +02:00
check ( " void f() \n "
" { \n "
" long a = b / 0ul; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
2009-08-30 21:02:09 +02:00
check ( " void f() \n "
" { \n "
" div_t divresult = div (1,0L); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
2009-08-30 21:02:09 +02:00
check ( " void f() \n "
" { \n "
" div_t divresult = div (1,0x5); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-08-30 21:02:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-01 16:00:26 +02:00
2009-09-01 21:06:46 +02:00
// Don't warn about floating points (gcc doesn't warn either)
// and floating points are handled differently than integers.
2009-09-01 16:00:26 +02:00
check ( " void f() \n "
" { \n "
" long a = b / 0.0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-01 21:06:46 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-01 16:00:26 +02:00
check ( " void f() \n "
" { \n "
" long a = b / 0.5; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-01 16:00:26 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-01 21:06:46 +02:00
// Don't warn about 0.0
2009-09-01 16:00:26 +02:00
check ( " void f() \n "
" { \n "
" div_t divresult = div (1,0.0); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-01 21:06:46 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-01 16:00:26 +02:00
check ( " void f() \n "
" { \n "
" div_t divresult = div (1,0.5); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-01 16:00:26 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-08-30 21:02:09 +02:00
}
2011-10-13 20:53:06 +02:00
void zeroDiv5 ( ) {
2011-08-24 02:12:29 +02:00
check ( " void f() \n "
" { { { \n "
" long a = b / 0; \n "
2013-03-20 15:36:16 +01:00
" } } } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
2011-08-24 02:12:29 +02:00
}
2011-10-13 20:53:06 +02:00
void zeroDiv6 ( ) {
2011-09-10 16:12:53 +02:00
check ( " void f() \n "
" { { { \n "
" int a = b % 0; \n "
2013-03-20 15:36:16 +01:00
" } } } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
2011-09-10 16:12:53 +02:00
}
2009-01-08 22:08:14 +01:00
2011-10-13 20:53:06 +02:00
void sprintfUsage ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2009-01-08 07:24:08 +01:00
// Tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-01-08 07:24:08 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-01-11 11:03:21 +01:00
//tokenizer.tokens()->printOut( "tokens" );
2009-01-08 07:24:08 +01:00
// Check for redundant code..
2009-03-20 18:16:21 +01:00
CheckOther checkOther ( & tokenizer , & settings , this ) ;
2009-07-05 22:16:43 +02:00
checkOther . invalidFunctionUsage ( ) ;
2009-01-08 07:24:08 +01:00
}
2009-01-08 22:08:14 +01:00
2011-10-13 20:53:06 +02:00
void sprintf1 ( ) {
2009-01-08 22:08:14 +01:00
sprintfUsage ( " void foo() \n "
" { \n "
" char buf[100]; \n "
" sprintf(buf, \" %s \" ,buf); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Undefined behavior: Variable 'buf' is used as parameter and destination in s[n]printf(). \n " , errout . str ( ) ) ;
2009-01-08 07:24:08 +01:00
}
2009-01-10 12:19:17 +01:00
2011-10-13 20:53:06 +02:00
void sprintf2 ( ) {
2009-01-10 12:19:17 +01:00
sprintfUsage ( " void foo() \n "
" { \n "
" char buf[100]; \n "
" sprintf(buf, \" %i \" ,sizeof(buf)); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-01-10 12:19:17 +01:00
}
2009-01-10 15:27:31 +01:00
2011-10-13 20:53:06 +02:00
void sprintf3 ( ) {
2009-01-10 15:27:31 +01:00
sprintfUsage ( " void foo() \n "
" { \n "
" char buf[100]; \n "
" sprintf(buf, \" %i \" ,sizeof(buf)); \n "
2009-01-10 15:29:59 +01:00
" if (buf[0]); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-01-10 15:29:59 +01:00
}
2009-01-10 15:27:31 +01:00
2011-10-13 20:53:06 +02:00
void sprintf4 ( ) {
2009-01-11 11:03:21 +01:00
sprintfUsage ( " struct A \n "
" { \n "
" char filename[128]; \n "
" }; \n "
" \n "
" void foo() \n "
" { \n "
" const char* filename = \" hello \" ; \n "
" struct A a; \n "
" snprintf(a.filename, 128, \" %s \" , filename); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-01-11 11:03:21 +01:00
}
2009-01-15 17:57:51 +01:00
2011-10-13 20:53:06 +02:00
void strPlusChar ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2009-01-15 17:57:51 +01:00
// Tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2009-01-15 17:57:51 +01:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Check for redundant code..
2009-03-20 18:16:21 +01:00
CheckOther checkOther ( & tokenizer , & settings , this ) ;
2009-01-15 17:57:51 +01:00
checkOther . strPlusChar ( ) ;
}
2011-10-13 20:53:06 +02:00
void strPlusChar1 ( ) {
2009-01-15 17:57:51 +01:00
// Strange looking pointer arithmetic..
strPlusChar ( " void foo() \n "
" { \n "
" const char *p = \" /usr \" + '/'; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Unusual pointer arithmetic. A value of type 'char' is added to a string literal. \n " , errout . str ( ) ) ;
2009-01-15 17:57:51 +01:00
}
2011-10-13 20:53:06 +02:00
void strPlusChar2 ( ) {
2009-01-15 18:12:33 +01:00
// Strange looking pointer arithmetic..
strPlusChar ( " void foo() \n "
" { \n "
2010-02-07 21:44:11 +01:00
" char ch = 1; \n "
" const char *p = ch + \" /usr \" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-02-07 21:44:11 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-02-08 08:35:53 +01:00
// Strange looking pointer arithmetic..
2010-02-07 21:44:11 +01:00
strPlusChar ( " void foo() \n "
" { \n "
" int i = 1; \n "
2010-02-22 21:30:21 +01:00
" const char* psz = \" Bla \" ; \n "
2010-02-07 21:44:11 +01:00
" const std::string str = i + psz; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-02-07 21:44:11 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-02-08 08:35:53 +01:00
}
2009-01-15 18:12:33 +01:00
2011-10-13 20:53:06 +02:00
void strPlusChar3 ( ) {
2009-01-18 18:42:41 +01:00
// Strange looking pointer arithmetic..
strPlusChar ( " void foo() \n "
" { \n "
" std::string temp = \" /tmp \" ; \n "
" std::string path = temp + '/' + \" sub \" + '/'; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-01-18 18:42:41 +01:00
}
2009-02-04 19:49:19 +01:00
2011-10-13 20:53:06 +02:00
void varScope ( const char code [ ] ) {
2009-03-24 20:24:03 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2011-10-30 11:21:46 +01:00
settings . addEnabled ( " style " ) ;
2010-12-01 18:00:55 +01:00
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Check for redundant code..
2009-03-24 20:24:03 +01:00
CheckOther checkOther ( & tokenizer , & settings , this ) ;
2009-07-05 22:16:43 +02:00
checkOther . checkVariableScope ( ) ;
2009-03-24 20:24:03 +01:00
}
2011-10-13 20:53:06 +02:00
void varScope1 ( ) {
2009-03-24 20:24:03 +01:00
varScope ( " unsigned short foo() \n "
" { \n "
" test_client CClient; \n "
" try \n "
" { \n "
" if (CClient.Open()) \n "
" { \n "
" return 0; \n "
" } \n "
" } \n "
" catch (...) \n "
" { \n "
" return 2; \n "
" } \n "
" \n "
" try \n "
" { \n "
" CClient.Close(); \n "
" } \n "
" catch (...) \n "
" { \n "
" return 2; \n "
" } \n "
" \n "
" return 1; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-03-24 20:24:03 +01:00
}
2009-03-24 20:59:56 +01:00
2011-10-13 20:53:06 +02:00
void varScope2 ( ) {
2009-03-24 20:59:56 +01:00
varScope ( " int foo() \n "
" { \n "
" Error e; \n "
" e.SetValue(12); \n "
" throw e; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-03-24 20:59:56 +01:00
}
2009-03-27 17:19:34 +01:00
2011-10-13 20:53:06 +02:00
void varScope3 ( ) {
2009-05-27 19:38:26 +02:00
varScope ( " void foo() \n "
" { \n "
" int i; \n "
" int *p = 0; \n "
" if (abc) \n "
" { \n "
" p = &i; \n "
" } \n "
" *p = 1; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-05-27 19:38:26 +02:00
}
2011-10-13 20:53:06 +02:00
void varScope4 ( ) {
2009-06-12 16:17:51 +02:00
varScope ( " void foo() \n "
" { \n "
" int i; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-12 16:17:51 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varScope5 ( ) {
2009-09-29 23:56:43 +02:00
varScope ( " void f(int x) \n "
" { \n "
" int i = 0; \n "
" if (x) { \n "
" for ( ; i < 10; ++i) ; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) The scope of the variable 'i' can be reduced. \n " , errout . str ( ) ) ;
2009-09-29 23:56:43 +02:00
2012-05-24 15:34:59 +02:00
varScope ( " void f(int x) { \n "
" const unsigned char i = 0; \n "
" if (x) { \n "
" for ( ; i < 10; ++i) ; \n "
" } \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'i' can be reduced. \n " , errout . str ( ) ) ;
2012-05-24 15:34:59 +02:00
2009-09-29 23:56:43 +02:00
varScope ( " void f(int x) \n "
2009-08-16 21:12:57 +02:00
" { \n "
" int i = 0; \n "
2009-09-29 23:56:43 +02:00
" if (x) {b()} \n "
" else { \n "
2009-08-16 21:12:57 +02:00
" for ( ; i < 10; ++i) ; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) The scope of the variable 'i' can be reduced. \n " , errout . str ( ) ) ;
2009-08-16 21:12:57 +02:00
}
2009-03-27 17:19:34 +01:00
2011-10-13 20:53:06 +02:00
void varScope6 ( ) {
2009-09-17 21:05:12 +02:00
varScope ( " void f(int x) \n "
" { \n "
" int i = x; \n "
" if (a) { \n "
" x++; \n "
" } \n "
" if (b) { \n "
" c(i); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-17 21:05:12 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-09-29 23:56:43 +02:00
varScope ( " void f() \n "
" { \n "
" int foo = 0; \n "
" std::vector<int> vec(10); \n "
" BOOST_FOREACH(int& i, vec) \n "
" { \n "
" foo += 1; \n "
" if(foo == 10) \n "
" { \n "
" return 0; \n "
" } \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-29 23:56:43 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-03-27 17:19:34 +01:00
2009-09-29 23:56:43 +02:00
varScope ( " void f(int &x) \n "
" { \n "
" int n = 1; \n "
" do \n "
" { \n "
" ++n; \n "
" ++x; \n "
" } while (x); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-29 23:56:43 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2009-03-27 17:19:34 +01:00
2011-10-13 20:53:06 +02:00
void varScope7 ( ) {
2009-10-06 17:45:28 +02:00
varScope ( " void f(int x) \n "
" { \n "
" int y = 0; \n "
" b(y); \n "
" if (x) { \n "
" y++; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-10-06 17:45:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varScope8 ( ) {
2010-01-21 18:50:56 +01:00
varScope ( " void test() { \n "
" float edgeResistance=1; \n "
" std::vector<int> edges; \n "
" BOOST_FOREACH(int edge, edges) { \n "
" edgeResistance = (edge+1) / 2.0; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'edgeResistance' can be reduced. \n " , errout . str ( ) ) ;
2010-01-21 18:50:56 +01:00
}
2011-10-13 20:53:06 +02:00
void varScope9 ( ) {
2010-04-09 16:53:27 +02:00
// classes may have extra side effects
varScope ( " class fred { \n "
" public: \n "
" void x(); \n "
" }; \n "
" void test(int a) { \n "
" fred f; \n "
" if (a == 2) { \n "
" f.x(); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-04-09 16:53:27 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varScope10 ( ) {
2010-08-26 21:57:48 +02:00
varScope ( " int f() \n "
" { \n "
" int x = 0; \n "
" FOR { \n "
" foo(x++); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-08-26 21:57:48 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varScope11 ( ) {
2011-01-17 20:51:15 +01:00
varScope ( " int f() { \n "
" int x = 0; \n "
" AB ab = { x, 0 }; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-17 20:51:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
varScope ( " int f() { \n "
" int x = 0; \n "
" if (a == 0) { ++x; } \n "
" AB ab = { x, 0 }; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-17 20:51:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
varScope ( " int f() { \n "
" int x = 0; \n "
" if (a == 0) { ++x; } \n "
" if (a == 1) { AB ab = { x, 0 }; } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-17 20:51:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void varScope12 ( ) {
2012-05-24 15:34:59 +02:00
varScope ( " void f(int x) { \n "
" int i[5]; \n "
" int* j = y; \n "
" if (x) \n "
" foo(i); \n "
" foo(j); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'i' can be reduced. \n " , errout . str ( ) ) ;
2012-05-24 15:34:59 +02:00
varScope ( " void f(int x) { \n "
" int i[5]; \n "
" int* j; \n "
" if (x) \n "
" j = i; \n "
" foo(j); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
varScope ( " void f(int x) { \n "
" const bool b = true; \n "
" x++; \n "
" if (x == 5) \n "
" foo(b); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'b' can be reduced. \n " , errout . str ( ) ) ;
2012-05-24 15:34:59 +02:00
varScope ( " void f(int x) { \n "
" const bool b = x; \n "
" x++; \n "
" if (x == 5) \n "
" foo(b); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void varScope13 ( ) {
2011-05-06 21:16:01 +02:00
// #2770
varScope ( " void f() { \n "
" int i = 0; \n "
" forever { \n "
" if (i++ == 42) { break; } \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-01-21 18:50:56 +01:00
2012-07-29 15:39:43 +02:00
void varScope14 ( ) {
// #3941
varScope ( " void f() { \n "
" const int i( foo()); \n "
" if(a) { \n "
" for ( ; i < 10; ++i) ; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2013-02-12 21:16:57 +01:00
void varScope15 ( ) {
// #4573
varScope ( " void f() { \n "
" int a,b,c; \n "
" if (a); \n "
" else if(b); \n "
" else if(c); \n "
" else; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-12 21:16:57 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2013-03-03 19:35:33 +01:00
void varScope16 ( ) {
varScope ( " void f() { \n "
" int a = 0; \n "
" while((++a) < 56) { \n "
" foo(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
varScope ( " void f() { \n "
" int a = 0; \n "
" do { \n "
" foo(); \n "
" } while((++a) < 56); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
varScope ( " void f() { \n "
" int a = 0; \n "
" do { \n "
" a = 64; \n "
" foo(a); \n "
" } while((++a) < 56); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
varScope ( " void f() { \n "
" int a = 0; \n "
" do { \n "
" a = 64; \n "
" foo(a); \n "
" } while(z()); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'a' can be reduced. \n " , errout . str ( ) ) ;
}
2013-03-15 13:00:51 +01:00
void varScope17 ( ) {
varScope ( " void f() { \n "
" int x; \n "
" if (a) { \n "
" x = stuff(x); \n "
" morestuff(x); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'x' can be reduced. \n " , errout . str ( ) ) ;
2013-03-18 16:17:53 +01:00
varScope ( " void f() { \n "
" int x; \n "
" if (a) { \n "
" x = stuff(x); \n "
" morestuff(x); \n "
" } \n "
" if (b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'x' can be reduced. \n " , errout . str ( ) ) ;
2013-03-15 13:00:51 +01:00
}
2013-03-23 11:28:33 +01:00
void varScope18 ( ) {
varScope ( " void f() { \n "
" short x; \n "
" \n "
" switch (ab) { \n "
" case A: \n "
" break; \n "
" case B: \n "
" default: \n "
" break; \n "
" } \n "
" \n "
" if (c) { \n "
" x = foo(); \n "
" do_something(x); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'x' can be reduced. \n " , errout . str ( ) ) ;
varScope ( " void f() { \n "
" short x; \n "
" \n "
" switch (ab) { \n "
" case A: \n "
" x = 10; \n "
" break; \n "
" case B: \n "
" default: \n "
" break; \n "
" } \n "
" \n "
" if (c) { \n "
" x = foo(); \n "
" do_something(x); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
varScope ( " void f() { \n "
" short x; \n "
" \n "
" switch (ab) { \n "
" case A: \n "
" if(c) \n "
" do_something(x); \n "
" break; \n "
" case B: \n "
" default: \n "
" break; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'x' can be reduced. \n " , errout . str ( ) ) ;
varScope ( " void f() { \n "
" short x; \n "
" \n "
" switch (ab) { \n "
" case A: \n "
" if(c) \n "
" do_something(x); \n "
" break; \n "
" case B: \n "
" default: \n "
" if(d) \n "
" do_something(x); \n "
" break; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void checkOldStylePointerCast ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2010-12-01 18:00:55 +01:00
2009-06-18 22:26:21 +02:00
// Tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizerCpp ( & settings , this ) ;
2009-06-18 22:26:21 +02:00
std : : istringstream istr ( code ) ;
2010-04-24 16:46:09 +02:00
tokenizerCpp . tokenize ( istr , " test.cpp " ) ;
2010-12-01 18:00:55 +01:00
Tokenizer tokenizerC ( & settings , this ) ;
2010-04-24 16:46:09 +02:00
std : : istringstream istr2 ( code ) ;
tokenizerC . tokenize ( istr2 , " test.c " ) ;
2009-06-18 22:26:21 +02:00
2010-04-24 16:46:09 +02:00
CheckOther checkOtherCpp ( & tokenizerCpp , & settings , this ) ;
checkOtherCpp . warningOldStylePointerCast ( ) ;
CheckOther checkOtherC ( & tokenizerC , & settings , this ) ;
checkOtherC . warningOldStylePointerCast ( ) ;
2009-06-18 22:26:21 +02:00
}
2011-10-13 20:53:06 +02:00
void oldStylePointerCast ( ) {
2009-06-19 12:01:27 +02:00
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (Base *) derived; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-19 12:01:27 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
2009-07-20 21:52:27 +02:00
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (const Base *) derived; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-07-20 21:52:27 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
2009-06-19 12:01:27 +02:00
2009-09-24 22:46:08 +02:00
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (const Base *) ( new Derived() ); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-24 22:46:08 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (const Base *) new Derived(); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-24 22:46:08 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (const Base *) new short[10]; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-09-24 22:46:08 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
2009-06-18 22:26:21 +02:00
checkOldStylePointerCast ( " class B; \n "
" class A \n "
" { \n "
" virtual void abc(B *) const = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-06-18 22:26:21 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-07-20 21:52:27 +02:00
checkOldStylePointerCast ( " class B; \n "
" class A \n "
" { \n "
" virtual void abc(const B *) const = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-07-20 21:52:27 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-06-18 22:26:21 +02:00
}
2012-03-25 12:55:39 +02:00
void checkInvalidPointerCast ( const char code [ ] , bool portability = false , bool inconclusive = false ) {
2012-02-26 11:56:32 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2013-03-03 11:41:59 +01:00
settings . addEnabled ( " warning " ) ;
2012-02-26 11:56:32 +01:00
if ( portability )
settings . addEnabled ( " portability " ) ;
2012-03-25 12:55:39 +02:00
settings . inconclusive = inconclusive ;
2012-02-26 11:56:32 +01:00
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
CheckOther checkOtherCpp ( & tokenizer , & settings , this ) ;
checkOtherCpp . invalidPointerCast ( ) ;
}
void invalidPointerCast ( ) {
checkInvalidPointerCast ( " void test() { \n "
" float *f = new float[10]; \n "
" delete [] (double*)f; \n "
" delete [] (long double const*)(new float[10]); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:3]: (warning) Casting between float* and double* which have an incompatible binary data representation. \n "
" [test.cpp:4]: (warning) Casting between float* and long double* which have an incompatible binary data representation. \n " ,
" [test.cpp:3]: (warning) Casting between float* and double* which have an incompatible binary data representation. \n "
" [test.cpp:4]: (warning) Casting between float* and double* which have an incompatible binary data representation. \n " , errout . str ( ) ) ;
2012-02-26 11:56:32 +01:00
checkInvalidPointerCast ( " void test(const float* f) { \n "
" double *d = (double*)f; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Casting between float* and double* which have an incompatible binary data representation. \n " , errout . str ( ) ) ;
2012-02-26 11:56:32 +01:00
checkInvalidPointerCast ( " void test(double* d1) { \n "
" long double *ld = (long double*)d1; \n "
" double *d2 = (double*)ld; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Casting between double* and long double* which have an incompatible binary data representation. \n "
" [test.cpp:3]: (warning) Casting between long double* and double* which have an incompatible binary data representation. \n " , errout . str ( ) ) ;
2012-02-26 11:56:32 +01:00
checkInvalidPointerCast ( " char* test(int* i) { \n "
" long double *d = (long double*)(i); \n "
" double *d = (double*)(i); \n "
" float *f = reinterpret_cast<float*>(i); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Casting between integer* and long double* which have an incompatible binary data representation. \n "
" [test.cpp:3]: (warning) Casting between integer* and double* which have an incompatible binary data representation. \n "
" [test.cpp:4]: (warning) Casting between integer* and float* which have an incompatible binary data representation. \n " , errout . str ( ) ) ;
2012-02-26 11:56:32 +01:00
checkInvalidPointerCast ( " float* test(unsigned int* i) { \n "
" return (float*)i; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Casting between integer* and float* which have an incompatible binary data representation. \n " , errout . str ( ) ) ;
2012-02-26 11:56:32 +01:00
checkInvalidPointerCast ( " float* test(unsigned int* i) { \n "
" return (float*)i[0]; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInvalidPointerCast ( " float* test(double& d) { \n "
" return (float*)&d; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Casting between double* and float* which have an incompatible binary data representation. \n " , errout . str ( ) ) ;
2012-02-26 11:56:32 +01:00
2012-05-06 19:37:41 +02:00
checkInvalidPointerCast ( " void test(float* data) { \n "
2012-03-25 12:55:39 +02:00
" f.write((char*)data,sizeof(float)); \n "
" } " , true , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInvalidPointerCast ( " void test(float* data) { \n "
" f.write((char*)data,sizeof(float)); \n "
2012-05-06 19:37:41 +02:00
" } " , true , true ) ; // #3639
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability, inconclusive) Casting from float* to char* is not portable due to different binary data representations on different platforms. \n " , errout . str ( ) ) ;
2012-03-25 12:55:39 +02:00
2012-02-26 11:56:32 +01:00
checkInvalidPointerCast ( " long long* test(float* f) { \n "
" return (long long*)f; \n "
" } " , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInvalidPointerCast ( " long long* test(float* f, char* c) { \n "
" foo((long long*)f); \n "
" return reinterpret_cast<long long*>(c); \n "
" } " , true ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability) Casting from float* to integer* is not portable due to different binary data representations on different platforms. \n " , errout . str ( ) ) ;
2012-09-03 11:32:15 +02:00
checkInvalidPointerCast ( " Q_DECLARE_METATYPE(int*) " ) ; // #4135 - don't crash
2012-02-26 11:56:32 +01:00
}
2011-10-13 20:53:06 +02:00
void dangerousStrolUsage ( ) {
2009-10-04 10:05:58 +02:00
{
sprintfUsage ( " int f(const char *num) \n "
" { \n "
" return strtol(num, NULL, 1); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-10-04 10:05:58 +02:00
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid radix in call to strtol(). It must be 0 or 2-36. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
}
{
sprintfUsage ( " int f(const char *num) \n "
" { \n "
" return strtol(num, NULL, 10); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2009-10-04 10:05:58 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
}
2011-10-13 20:53:06 +02:00
void testPassedByValue ( const char code [ ] ) {
2009-10-04 10:05:58 +02:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2011-09-03 15:30:30 +02:00
settings . addEnabled ( " performance " ) ;
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
2009-10-04 10:05:58 +02:00
CheckOther checkOther ( & tokenizer , & settings , this ) ;
checkOther . checkConstantFunctionParameter ( ) ;
}
2011-10-13 20:53:06 +02:00
void passedByValue ( ) {
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::string str) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'str' should be passed by reference. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " class Foo; \n void f(const Foo foo) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'foo' should be passed by reference. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::string &str) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::vector<int> v) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by reference. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::vector<std::string> v) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2012-07-23 10:41:20 +02:00
testPassedByValue ( " void f(const std::vector<std::string>::size_type s) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::vector<int> &v) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::map<int,int> &v) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::map<int,int> v) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::map<std::string,std::string> v) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::map<int,std::string> v) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2009-10-06 23:04:54 +02:00
testPassedByValue ( " void f(const std::map<std::string,int> v) {} " ) ;
2010-11-27 09:46:34 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by reference. \n " , errout . str ( ) ) ;
2011-04-19 20:07:54 +02:00
testPassedByValue ( " void f(const std::streamoff pos) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
}
2011-10-13 20:53:06 +02:00
void mathfunctionCall1 ( ) {
2010-04-02 20:42:06 +02:00
// log|log10
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(-2) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -2 to log() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(-1.) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -1. to log() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(-1.0) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -1.0 to log() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(-0.1) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -0.1 to log() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(0) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value 0 to log() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(0.) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value 0. to log() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(0.0) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value 0.0 to log() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(1.0E+3) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << log(1.0E-3) << std::endl; \n "
" } " ) ;
2010-04-04 08:01:05 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << log(1E-3) << std::endl; \n "
" } " ) ;
2010-04-04 08:01:05 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
2010-07-07 09:03:40 +02:00
check ( " void foo() \n "
" { \n "
" std::string *log(0); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-15 21:03:49 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << log(2.0) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-01-08 12:07:25 +01:00
// #3473 - no warning if "log" is a variable
check ( " Fred::Fred() : log(0) { } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-07-07 09:03:40 +02:00
2010-04-02 20:42:06 +02:00
// acos
2010-04-02 20:23:37 +02:00
check ( " void foo() \n "
" { \n "
2010-04-04 08:01:05 +02:00
" std::cout << acos(1) << std::endl; \n "
" std::cout << acos(-1) << std::endl; \n "
" std::cout << acos(0.1) << std::endl; \n "
" std::cout << acos(0.0001) << std::endl; \n "
" std::cout << acos(0.01) << std::endl; \n "
" std::cout << acos(1.0E-1) << std::endl; \n "
2010-04-02 20:23:37 +02:00
" std::cout << acos(-1.0E-1) << std::endl; \n "
" std::cout << acos(+1.0E-1) << std::endl; \n "
" std::cout << acos(0.1E-1) << std::endl; \n "
" std::cout << acos(+0.1E-1) << std::endl; \n "
" std::cout << acos(-0.1E-1) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << acos(1.1) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value 1.1 to acos() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 02:19:38 +02:00
2010-04-02 20:23:37 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << acos(-1.1) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -1.1 to acos() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-02 20:23:37 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << acos(-110) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -110 to acos() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-05 19:35:56 +02:00
// atan2
check ( " void foo() \n "
" { \n "
" std::cout << atan2(1,1) << std::endl; \n "
" std::cout << atan2(-1,-1) << std::endl; \n "
" std::cout << atan2(0.1,1) << std::endl; \n "
" std::cout << atan2(0.0001,100) << std::endl; \n "
" std::cout << atan2(0.01m-1) << std::endl; \n "
" std::cout << atan2(1.0E-1,-3) << std::endl; \n "
" std::cout << atan2(-1.0E-1,+2) << std::endl; \n "
" std::cout << atan2(+1.0E-1,0) << std::endl; \n "
" std::cout << atan2(0.1E-1,3) << std::endl; \n "
" std::cout << atan2(+0.1E-1,1) << std::endl; \n "
" std::cout << atan2(-0.1E-1,8) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << atan2(0,0) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing values 0 and 0 to atan2() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-05 19:35:56 +02:00
2010-04-05 20:07:53 +02:00
// fmod
2010-04-05 19:45:33 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << fmod(1.0,0) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing values 1.0 and 0 to fmod() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-05 19:45:33 +02:00
2010-04-05 20:07:53 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << fmod(1.0,1) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// pow
check ( " void foo() \n "
" { \n "
" std::cout << pow(0,-10) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing values 0 and -10 to pow() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-05 20:07:53 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << pow(0,10) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-04-05 20:20:20 +02:00
// sqrt
check ( " void foo() \n "
" { \n "
" std::cout << sqrt(-1) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -1 to sqrt() leads to undefined result. \n " , errout . str ( ) ) ;
2010-04-05 20:20:20 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << sqrt(1) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-04-05 19:35:56 +02:00
2010-04-02 20:23:37 +02:00
}
2010-04-13 19:30:25 +02:00
2011-11-17 16:31:16 +01:00
void cctypefunctionCall ( ) {
// isalnum
check ( " void foo() \n "
" { \n "
" std::cout << isalnum(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isalnum(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isalnum() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << isalpha(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isalpha(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isalpha() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << iscntrl(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << iscntrl(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to iscntrl() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << isdigit(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isdigit(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isdigit() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << isgraph(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isgraph(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isgraph() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << islower(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << islower(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to islower() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << isprint(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isprint(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isprint() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << ispunct(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << ispunct(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to ispunct() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << isspace(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isspace(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isspace() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << isupper(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isupper(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isupper() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
check ( " void foo() \n "
" { \n "
" std::cout << isxdigit(61) << std::endl; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << isxdigit(-61) << std::endl; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Passing value -61 to isxdigit() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-12-29 08:08:37 +01:00
check ( " void f() { \n "
" std::isgraph(-10000, loc); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Passing value -10000 to isgraph() causes undefined behavior which may lead to a crash. \n " , errout . str ( ) ) ;
2011-11-17 16:31:16 +01:00
}
2010-05-15 14:06:45 +02:00
2011-10-13 20:53:06 +02:00
void switchRedundantAssignmentTest ( ) {
2012-05-30 01:30:37 +02:00
2010-06-30 09:10:30 +02:00
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:9]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2010-06-30 09:10:30 +02:00
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" { \n "
" y = 2; \n "
" } \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8] -> [test.cpp:11]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2010-06-30 09:10:30 +02:00
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" case 3: \n "
" if (x) \n "
2010-06-30 09:10:30 +02:00
" { \n "
2012-05-29 06:19:22 +02:00
" y = 3; \n "
2010-06-30 09:10:30 +02:00
" } \n "
2012-05-29 06:19:22 +02:00
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-06-30 09:10:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" { \n "
" y = 2; \n "
2013-04-13 20:17:53 +02:00
" if (z) \n "
2012-05-29 06:19:22 +02:00
" printf( \" %d \" , y); \n "
" } \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-06-30 09:10:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int x = a; \n "
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" x = 2; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-06-30 09:10:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" break; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-06-30 09:10:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-09-04 14:24:45 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" while(xyz()) { \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" continue; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
2011-10-30 11:27:27 +01:00
" bar(y); \n "
2010-09-04 14:24:45 +02:00
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-09-04 14:24:45 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-11 19:27:31 +01:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" while(xyz()) { \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" throw e; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
2011-10-30 11:27:27 +01:00
" bar(y); \n "
2011-03-11 19:27:31 +01:00
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-11 19:27:31 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-06-30 09:10:30 +02:00
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" printf( \" %d \" , y); \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-06-30 09:10:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" bar(); \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-06-30 09:10:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-13 10:27:22 +02:00
2012-09-03 12:03:30 +02:00
check ( " void bar() {} \n " // bar isn't noreturn
" void foo() \n "
" { \n "
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" bar(); \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8] -> [test.cpp:11]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-09-03 12:03:30 +02:00
2011-10-13 10:27:22 +02:00
check ( " void foo(char *str, int a) \n "
" { \n "
2012-05-29 06:19:22 +02:00
" switch (a) \n "
" { \n "
" case 2: \n "
" strcpy(str, \" a' \" ); \n "
" case 3: \n "
" strcpy(str, \" b' \" ); \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:6] -> [test.cpp:8]: (warning) Buffer 'str' is being written before its old content has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2011-10-13 10:27:22 +02:00
check ( " void foo(char *str, int a) \n "
" { \n "
2012-05-29 06:19:22 +02:00
" switch (a) \n "
" { \n "
" case 2: \n "
" strncpy(str, \" a' \" ); \n "
" case 3: \n "
" strncpy(str, \" b' \" ); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:6] -> [test.cpp:8]: (warning) Buffer 'str' is being written before its old content has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2011-10-13 10:27:22 +02:00
check ( " void foo(char *str, int a) \n "
" { \n "
2012-05-29 06:19:22 +02:00
" int z = 0; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" strcpy(str, \" a' \" ); \n "
" z++; \n "
" case 3: \n "
" strcpy(str, \" b' \" ); \n "
" z++; \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:10]: (warning) Buffer 'str' is being written before its old content has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2011-10-13 10:27:22 +02:00
check ( " void foo(char *str, int a) \n "
" { \n "
2012-05-29 06:19:22 +02:00
" switch (a) \n "
" { \n "
" case 2: \n "
" strcpy(str, \" a' \" ); \n "
" break; \n "
" case 3: \n "
" strcpy(str, \" b' \" ); \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-10-13 10:27:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(char *str, int a) \n "
" { \n "
2012-05-29 06:19:22 +02:00
" switch (a) \n "
" { \n "
" case 2: \n "
" strcpy(str, \" a' \" ); \n "
" printf(str); \n "
" case 3: \n "
" strcpy(str, \" b' \" ); \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-05-09 15:25:36 +02:00
}
2012-11-15 07:48:45 +01:00
2013-05-09 15:25:36 +02:00
void switchRedundantOperationTest ( ) {
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
2012-08-22 14:40:57 +02:00
" { \n "
" case 2: \n "
" ++y; \n "
2013-05-09 15:25:36 +02:00
" case 3: \n "
" y = 3; \n "
2012-08-22 14:40:57 +02:00
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:9]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" { \n "
" ++y; \n "
" } \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8] -> [test.cpp:11]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y; \n "
" case 3: \n "
" ++y; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" ++y; \n "
" case 3: \n "
" ++y; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" --y; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:9]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" { \n "
" --y; \n "
" } \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8] -> [test.cpp:11]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y; \n "
" case 3: \n "
" --y; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" --y; \n "
" case 3: \n "
" --y; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y++; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:9]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" { \n "
" y++; \n "
" } \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8] -> [test.cpp:11]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" case 3: \n "
" y++; \n "
" } \n "
" bar(y); \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y++; \n "
" case 3: \n "
" y++; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y--; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:9]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" { \n "
" y--; \n "
" } \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8] -> [test.cpp:11]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing? \n " , errout . str ( ) ) ;
2012-08-22 14:40:57 +02:00
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y = 2; \n "
" case 3: \n "
" y--; \n "
" } \n "
" bar(y); \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y--; \n "
" case 3: \n "
" y--; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y++; \n "
" case 3: \n "
" if (x) \n "
" { \n "
" y = 3; \n "
" } \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" { \n "
" y++; \n "
" if (y) \n "
" printf( \" %d \" , y); \n "
" } \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int x = a; \n "
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" x++; \n "
" case 3: \n "
" y++; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y++; \n "
" break; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" while(xyz()) { \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y++; \n "
" continue; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" while(xyz()) { \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y++; \n "
" throw e; \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y++; \n "
" printf( \" %d \" , y); \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int y = 1; \n "
" switch (x) \n "
" { \n "
" case 2: \n "
" y++; \n "
" bar(); \n "
" case 3: \n "
" y = 3; \n "
" } \n "
" bar(y); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-22 14:40:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-05-29 06:19:22 +02:00
void switchRedundantBitwiseOperationTest ( ) {
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 3; \n "
" case 3: \n "
" y |= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Redundant bitwise operation on 'y' in 'switch' statement. 'break;' missing? \n " , errout . str ( ) ) ;
2012-05-29 06:19:22 +02:00
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 3; \n "
" default: \n "
" y |= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Redundant bitwise operation on 'y' in 'switch' statement. 'break;' missing? \n " , errout . str ( ) ) ;
2012-05-29 06:19:22 +02:00
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 3; \n "
" default: \n "
" if (z) \n "
" y |= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= z; \n "
" z++ \n "
" default: \n "
" y |= z; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 3; \n "
" bar(y); \n "
" case 3: \n "
" y |= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 3; \n "
" y = 4; \n "
" case 3: \n "
" y |= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y &= 3; \n "
" case 3: \n "
" y &= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Redundant bitwise operation on 'y' in 'switch' statement. 'break;' missing? \n " , errout . str ( ) ) ;
2012-05-29 06:19:22 +02:00
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 3; \n "
" break; \n "
" case 3: \n "
" y |= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y ^= 3; \n "
" case 3: \n "
" y ^= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 2; \n "
" case 3: \n "
" y |= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y &= 2; \n "
" case 3: \n "
" y &= 3; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
" int y = 1; \n "
" switch (a) \n "
" { \n "
" case 2: \n "
" y |= 2; \n "
" case 3: \n "
" y &= 2; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-10-13 10:27:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-06-30 09:10:30 +02:00
}
2010-08-14 15:15:12 +02:00
2011-10-13 20:53:06 +02:00
void switchFallThroughCase ( ) {
2011-02-19 09:33:29 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" break; \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-19 09:33:29 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-19 20:02:28 +01:00
check_preprocess_suppress (
2011-03-11 19:27:31 +01:00
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" break; \n "
" case 2: \n "
" continue; \n "
" case 3: \n "
" return; \n "
" case 4: \n "
" exit(1); \n "
" case 5: \n "
" goto end; \n "
" case 6: \n "
" throw e; \n "
" case 7: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-11 19:27:31 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_preprocess_suppress (
2011-02-19 20:02:28 +01:00
" void foo() { \n "
" switch (a) { \n "
" case 0: \n "
" case 1: \n "
" break; \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-19 20:02:28 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-19 09:33:29 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" g(); \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-02-19 09:33:29 +01:00
2011-02-19 20:02:28 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" g(); \n "
" default: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-02-19 20:02:28 +01:00
2011-02-19 09:33:29 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" g(); \n "
" // fall through \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-19 09:33:29 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-19 21:54:01 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" g(); \n "
" /* FALLTHRU */ \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-19 21:54:01 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-19 09:33:29 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" g(); \n "
" break; \n "
" // fall through \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-19 20:02:28 +01:00
ASSERT_EQUALS ( " [test.cpp:7]: (information) Unmatched suppression: switchCaseFallThrough \n " , errout . str ( ) ) ;
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" { \n "
" break; \n "
" } \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-19 09:33:29 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-19 20:02:28 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" for (;;) { \n "
" break; \n "
" } \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-02-19 20:02:28 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" if (b) { \n "
" break; \n "
" } else { \n "
" break; \n "
" } \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-19 20:02:28 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" if (b) { \n "
" break; \n "
" } else { \n "
" } \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-02-19 20:02:28 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" if (b) { \n "
" break; \n "
" } \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-02-19 20:02:28 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" if (b) { \n "
" } else { \n "
" break; \n "
" } \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:8]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-02-19 20:02:28 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" if (b) { \n "
" case 2: \n "
" } else { \n "
" break; \n "
" } \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-02-23 10:45:21 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" int x; \n "
" case 1: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-23 10:45:21 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-04 07:22:17 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" g(); \n "
" switch (b) { \n "
" case 1: \n "
" return; \n "
" default: \n "
" return; \n "
" } \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-04 07:22:17 +01:00
// This fails because the switch parsing code currently doesn't understand
// that all paths after g() actually return. It's a pretty unusual case
// (no pun intended).
TODO_ASSERT_EQUALS ( " " ,
2012-09-16 19:44:02 +02:00
" [test.cpp:11]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-03-04 07:26:48 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" #ifndef A \n "
" g(); \n "
" // fall through \n "
" #endif \n "
" case 2: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-04 07:26:48 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-04 08:26:14 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" goto leave; \n "
" case 2: \n "
" break; \n "
" } \n "
" leave: \n "
" if (x) { \n "
" g(); \n "
" return; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-04 08:26:14 +01:00
// This fails because Tokenizer::simplifyGoto() copies the "leave:" block
// into where the goto is, but because it contains a "return", it omits
// copying a final return after the block.
TODO_ASSERT_EQUALS ( " " ,
2012-09-16 19:44:02 +02:00
" [test.cpp:5]: (style) Switch falls through case without comment. 'break;' missing? \n " , errout . str ( ) ) ;
2011-03-05 06:02:38 +01:00
check_preprocess_suppress (
" void foo() { \n "
" switch (a) { \n "
" case 1: \n "
" g(); \n "
" // fall through \n "
" case 2: \n "
" g(); \n "
" // falls through \n "
" case 3: \n "
" g(); \n "
" // fall-through \n "
" case 4: \n "
" g(); \n "
" // drop through \n "
" case 5: \n "
" g(); \n "
" // pass through \n "
" case 5: \n "
" g(); \n "
" // no break \n "
" case 5: \n "
" g(); \n "
" // fallthru \n "
" case 6: \n "
" g(); \n "
" /* fall */ \n "
" default: \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-05 06:02:38 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-06 00:14:10 +01:00
check_preprocess_suppress (
" void foo() { \n "
" // unrelated comment saying 'fall through' \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-06 00:14:10 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-19 09:33:29 +01:00
}
2011-12-07 18:20:52 +01:00
void unreachableCode ( ) {
2011-12-03 11:43:23 +01:00
check ( " void foo(int a) { \n "
" while(1) { \n "
" if (a++ >= 100) { \n "
" break; \n "
" continue; \n "
" } \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " int foo(int a) { \n "
" return 0; \n "
" return(a-1); \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " int foo(int a) { \n "
" A: "
" return(0); \n "
" goto A; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
2011-07-15 02:12:56 +02:00
check ( " void foo(int a) \n "
" { \n "
" switch(a) { \n "
" case 0: \n "
" printf( \" case 0 \" ); \n "
" break; \n "
" break; \n "
" case 1: \n "
" c++; \n "
" break; \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:7]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-07-15 02:12:56 +02:00
check ( " void foo(int a) \n "
" { \n "
" switch(a) { \n "
" case 0: \n "
" printf( \" case 0 \" ); \n "
" break; \n "
" case 1: \n "
" c++; \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-07-15 02:12:56 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int a) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" while(true) { \n "
2011-07-15 02:12:56 +02:00
" if (a++ >= 100) { \n "
" break; \n "
" break; \n "
" } \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:6]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-07-15 02:12:56 +02:00
check ( " void foo(int a) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" while(true) { \n "
2011-07-15 02:12:56 +02:00
" if (a++ >= 100) { \n "
" continue; \n "
" continue; \n "
" } \n "
" a+=2; \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:6]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-07-15 02:12:56 +02:00
check ( " void foo(int a) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" while(true) { \n "
2011-07-15 02:12:56 +02:00
" if (a++ >= 100) { \n "
" continue; \n "
" } \n "
" a+=2; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-07-15 02:12:56 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " int foo() { \n "
" throw 0; \n "
" return 1; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-03 11:43:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" throw 0; \n "
" return; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " int foo() { \n "
" return 0; \n "
" return 1; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " int foo() { \n "
" return 0; \n "
" foo(); \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Statements following return, break, continue, goto or throw will never be executed. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " int foo() { \n "
" if(bar) \n "
" return 0; \n "
" return 124; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int foo() { \n "
" while(bar) { \n "
" return 0; \n "
" return 0; \n "
" return 0; \n "
" return 0; \n "
" } \n "
" return 124; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " void foo() { \n "
" while(bar) { \n "
" return; \n "
" break; \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-28 21:05:10 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2011-12-03 11:43:23 +01:00
check ( " int foo() { \n "
" return 0; \n "
" label: \n "
" throw 0; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-12-03 11:43:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-07 18:20:52 +01:00
check ( " void foo() { \n "
" wxCHECK2(state < 3 && state >= 0, return); \n "
" _checkboxState = state; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" virtual void foo (P & Val) throw (); \n "
" virtual void foo1 (P & Val) throw (); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-23 12:46:59 +01:00
2011-12-31 23:11:03 +01:00
check ( " int foo() { \n "
" goto label; \n "
" while (true) { \n "
" bar(); \n "
" label: \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // #3457
check ( " int foo() { \n "
" goto label; \n "
" do { \n "
" bar(); \n "
" label: \n "
" } while (true); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // #3457
check ( " int foo() { \n "
" goto label; \n "
" for (;;) { \n "
" bar(); \n "
" label: \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // #3457
2011-12-23 12:46:59 +01:00
check ( " %: return ; () " ) ; // Don't crash. #3441.
2012-04-05 10:38:29 +02:00
// #3383. TODO: Use preprocessor
check ( " int foo() { \n "
" \n " // #ifdef A
" return 0; \n "
" \n " // #endif
" return 1; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-04-05 10:38:29 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int foo() { \n "
" \n " // #ifdef A
" return 0; \n "
" \n " // #endif
" return 1; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , true , false , false ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (style, inconclusive) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2013-05-11 17:50:59 +02:00
// #4711 lambda functions
check ( " int f() { \n "
" return g([](int x){x+1; return x;}); \n "
" } " , 0 , false , false , false , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-07-15 02:12:56 +02:00
}
2012-12-07 21:27:32 +01:00
void suspiciousCase ( ) {
check ( " void foo() { \n "
" switch(a) { \n "
" case A&&B: \n "
" foo(); \n "
" case (A||B): \n "
" foo(); \n "
2013-04-13 20:17:53 +02:00
" case A||B: \n "
2012-12-07 21:27:32 +01:00
" foo(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Found suspicious case label in switch(). Operator '&&' probably doesn't work as intended. \n "
" [test.cpp:5]: (warning, inconclusive) Found suspicious case label in switch(). Operator '||' probably doesn't work as intended. \n "
" [test.cpp:7]: (warning, inconclusive) Found suspicious case label in switch(). Operator '||' probably doesn't work as intended. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
" switch(a) { \n "
" case 1: \n "
" a=A&&B; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-12-07 21:44:30 +01:00
check ( " void foo() { \n "
" switch(a) { \n "
" case A&&B?B:A: \n "
" foo(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-12-07 21:27:32 +01:00
}
2013-01-18 08:03:04 +01:00
void suspiciousEqualityComparison ( ) {
check ( " void foo(int c) { \n "
" if (c == 1) c == 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
check ( " void foo(int* c) { \n "
" if (*c == 1) *c == 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" if (c == 1) { \n "
" c = 0; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" c == 1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" for (int i = 0; i == 10; i ++) { \n "
" a ++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" for (i == 0; i < 10; i ++) { \n "
" c ++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" for (i == 1; i < 10; i ++) { \n "
" c ++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" for (i == 2; i < 10; i ++) { \n "
" c ++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" for (int i = 0; i < 10; i == c) { \n "
" c ++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" for (; running == 1;) { \n "
" c ++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int c) { \n "
" printf( \" %i \n \" , ({x==0;})); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int x) { \n "
" printf( \" %i \n \" , ({int x = do_something(); x == 0;})); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int x) { \n "
" printf( \" %i \n \" , ({x == 0; x > 0 ? 10 : 20})); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? \n " , errout . str ( ) ) ;
2013-01-19 01:34:15 +01:00
check ( " void foo(int x) { \n "
2013-04-13 20:17:53 +02:00
" for (const Token* end = tok->link(); tok != end; tok = (tok == end) ? end : tok->next()) { \n "
2013-01-19 01:34:15 +01:00
" x++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int x) { \n "
" for (int i = (x == 0) ? 0 : 5; i < 10; i ++) { \n "
" x++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int x) { \n "
" for (int i = 0; i < 10; i += (x == 5) ? 1 : 2) { \n "
" x++; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-01-18 08:03:04 +01:00
}
2012-12-07 21:27:32 +01:00
2011-10-13 20:53:06 +02:00
void selfAssignment ( ) {
2010-08-15 06:28:22 +02:00
check ( " void foo() \n "
" { \n "
" int x = 1; \n "
" x = x; \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Redundant assignment of 'x' to itself. \n " , errout . str ( ) ) ;
2010-08-15 06:28:22 +02:00
check ( " void foo() \n "
" { \n "
" int x = x; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Redundant assignment of 'x' to itself. \n " , errout . str ( ) ) ;
2010-08-15 06:28:22 +02:00
check ( " void foo() \n "
" { \n "
" std::string var = var = \" test \" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-05-09 15:25:36 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Redundant assignment of 'var' to itself. \n " , errout . str ( ) ) ;
2010-08-15 06:28:22 +02:00
2012-08-21 20:15:04 +02:00
// #4073 (segmentation fault)
check ( " void Foo::myFunc( int a ) \n "
" { \n "
" if (a == 42) \n "
" a = a; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-05-09 15:25:36 +02:00
check ( " void foo() \n "
" { \n "
" int x = 1; \n "
" x = x + 1; \n "
2010-08-15 06:28:22 +02:00
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-08-15 06:28:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-04 21:08:42 +01:00
check ( " void foo() \n "
" { \n "
" int *x = getx(); \n "
" *x = x; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-04 21:08:42 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-12 14:27:07 +01:00
2012-05-11 19:38:19 +02:00
check ( " void foo() { \n "
" BAR *x = getx(); \n "
" x = x; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Redundant assignment of 'x' to itself. \n " , errout . str ( ) ) ;
2012-05-11 19:38:19 +02:00
2012-06-22 11:23:50 +02:00
// #2502 - non-primitive type -> there might be some side effects
2011-02-12 14:27:07 +01:00
check ( " void foo() \n "
" { \n "
" Fred fred; fred = fred; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-02-12 14:27:07 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-16 12:07:56 +02:00
check ( " void f(int x) { \n "
" x = (x == 0); "
" func(x); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" x = (x != 0); "
" func(x); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-19 07:23:11 +02:00
// ticket #3001 - false positive
check ( " void foo(int x) { \n "
" x = x ? x : 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-06-22 11:23:50 +02:00
// #3800 - false negative when variable is extern
check ( " extern int i; \n "
" void f() { \n "
" i = i; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Redundant assignment of 'i' to itself. \n " , errout . str ( ) ) ;
2012-11-03 21:16:05 +01:00
// #4291 - id for variables accessed through 'this'
check ( " class Foo { \n "
" int var; \n "
" void func(); \n "
" }; \n "
" void Foo::func() { \n "
" this->var = var; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Redundant assignment of 'var' to itself. \n " , errout . str ( ) ) ;
2010-08-15 06:28:22 +02:00
}
2011-10-13 20:53:06 +02:00
void trac1132 ( ) {
2012-07-07 20:21:08 +02:00
check ( " class Lock \n "
2012-05-24 17:40:43 +02:00
" { \n "
" public: \n "
" Lock(int i) \n "
" { \n "
" std::cout << \" Lock \" << i << std::endl; \n "
" } \n "
" ~Lock() \n "
" { \n "
" std::cout << \" ~Lock \" << std::endl; \n "
" } \n "
" }; \n "
" int main() \n "
" { \n "
" Lock(123); \n "
" std::cout << \" hello \" << std::endl; \n "
" return 0; \n "
" } \n "
) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:15]: (error) Instance of 'Lock' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-10-01 17:23:22 +02:00
}
2012-04-12 14:02:09 +02:00
void trac3693 ( ) {
check ( " struct A{ \n "
" enum { \n "
" b = 300 \n "
" }; \n "
" }; \n "
2013-04-13 20:17:53 +02:00
" const int DFLT_TIMEOUT = A::b % 1000000 ; \n " , 0 , false , false , false , false ) ;
2012-04-12 14:02:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickFunction1 ( ) {
2010-10-01 17:23:22 +02:00
check ( " int main ( ) \n "
" { \n "
" CouldBeFunction ( 123 ) ; \n "
" return 0 ; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickFunction2 ( ) {
2011-03-20 09:55:26 +01:00
check ( " struct error { \n "
" error() {} \n "
" }; \n "
" \n "
" class parser { \n "
" public: \n "
" void error() const {} \n "
" \n "
" void foo() const { \n "
" error(); \n "
" } \n "
" }; \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectPicksClass ( ) {
2010-10-01 17:23:22 +02:00
check ( " class NotAFunction ; \n "
" int function ( ) \n "
" { \n "
" NotAFunction ( 123 ); \n "
" return 0 ; \n "
" } \n "
) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Instance of 'NotAFunction' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-10-01 17:23:22 +02:00
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectPicksStruct ( ) {
2010-10-01 17:23:22 +02:00
check ( " struct NotAClass; \n "
" bool func ( ) \n "
" { \n "
" NotAClass ( 123 ) ; \n "
" return true ; \n "
" } \n "
) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Instance of 'NotAClass' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-10-01 17:23:22 +02:00
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickIf ( ) {
2010-10-01 17:23:22 +02:00
check ( " bool func( int a , int b , int c ) \n "
" { \n "
" if ( a > b ) return c == a ; \n "
" return b == a ; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickConstructorDeclaration ( ) {
2013-02-10 07:43:09 +01:00
check ( " class Something : public SomethingElse \n "
2010-10-01 17:23:22 +02:00
" { \n "
" public: \n "
" ~Something ( ) ; \n "
" Something ( ) ; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-10-02 10:43:12 +02:00
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickFunctor ( ) {
2012-07-07 20:21:08 +02:00
check ( " class IncrementFunctor \n "
2010-10-02 10:43:12 +02:00
" { \n "
" public: \n "
" void operator()(int &i) \n "
" { \n "
" ++i; \n "
" } \n "
" }; \n "
" \n "
" int main() \n "
" { \n "
2010-10-02 13:16:50 +02:00
" int a = 1; \n "
" IncrementFunctor()(a); \n "
" return a; \n "
2010-10-02 10:43:12 +02:00
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-10-02 12:12:52 +02:00
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickLocalClassConstructors ( ) {
2010-10-02 12:12:52 +02:00
check ( " void f() { \n "
" class Foo { \n "
" Foo() { } \n "
2010-10-02 14:59:04 +02:00
" Foo(int a) { } \n "
" Foo(int a, int b) { } \n "
2010-10-02 12:12:52 +02:00
" }; \n "
2010-10-02 14:59:04 +02:00
" Foo(); \n "
2010-10-02 12:12:52 +02:00
" } \n "
) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (error) Instance of 'Foo' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-10-02 12:12:52 +02:00
}
2010-10-02 12:26:29 +02:00
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickUsedObject ( ) {
2010-10-02 12:26:29 +02:00
check ( " struct Foo { \n "
" void bar() { \n "
" } \n "
" }; \n "
" \n "
" void fn() { \n "
" Foo().bar(); \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-12-25 08:43:52 +01:00
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickPureC ( ) {
2010-12-25 08:43:52 +01:00
// Ticket #2352
const char code [ ] = " struct cb_watch_bool { \n "
" int a; \n "
" }; \n "
" \n "
" void f() \n "
" { \n "
" cb_watch_bool(); \n "
" } \n " ;
check ( code , " test.cpp " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (error) Instance of 'cb_watch_bool' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-12-25 08:43:52 +01:00
check ( code , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-13 08:38:40 +01:00
// Ticket #2639
check ( " struct stat { int a; int b; }; \n "
" void stat(const char *fn, struct stat *); \n "
" \n "
" void foo() { \n "
" stat( \" file.txt \" , &st); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-13 08:38:40 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-31 20:07:41 +01:00
}
2011-10-13 20:53:06 +02:00
void testMisusedScopeObjectDoesNotPickNestedClass ( ) {
2011-01-31 20:07:41 +01:00
const char code [ ] = " class ios_base { \n "
" public: \n "
" class Init { \n "
" public: \n "
" }; \n "
" }; \n "
" class foo { \n "
" public: \n "
" foo(); \n "
" void Init(int); \n "
" }; \n "
" foo::foo() { \n "
" Init(0); \n "
" } \n " ;
check ( code , " test.cpp " ) ;
2011-03-20 09:55:26 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-02 12:26:29 +02:00
}
2010-10-03 23:16:11 +02:00
2011-10-13 20:53:06 +02:00
void trac2084 ( ) {
2012-07-07 20:21:08 +02:00
check ( " void f() \n "
2010-10-13 11:31:41 +02:00
" { \n "
" struct sigaction sa; \n "
" \n "
" { sigaction(SIGHUP, &sa, 0); }; \n "
" { sigaction(SIGINT, &sa, 0); }; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void trac2071 ( ) {
2010-10-03 23:16:11 +02:00
check ( " void f() { \n "
2010-10-05 20:54:13 +02:00
" struct AB { \n "
" AB(int a) { } \n "
" }; \n "
" \n "
" const AB ab[3] = { AB(0), AB(1), AB(2) }; \n "
" } \n "
) ;
2010-10-03 23:16:11 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-10-10 22:05:06 +02:00
2012-04-26 15:23:47 +02:00
void modulo ( ) {
check ( " bool f(bool& b1, bool& b2, bool& b3) { \n "
" b1 = a % 5 == 4; \n "
" b2 = a % c == 100000; \n "
" b3 = a % 5 == c; \n "
" return a % 5 == 5-p; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(bool& b1, bool& b2, bool& b3, bool& b4, bool& b5) { \n "
" b1 = a % 5 < 5; \n "
" b2 = a % 5 <= 5; \n "
" b3 = a % 5 == 5; \n "
" b4 = a % 5 != 5; \n "
" b5 = a % 5 >= 5; \n "
" return a % 5 > 5; \n "
" } " ) ;
2012-04-26 23:04:55 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n "
" [test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n "
" [test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n "
" [test.cpp:5]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n "
2012-08-24 00:03:22 +02:00
" [test.cpp:6]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n "
" [test.cpp:7]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n " , errout . str ( ) ) ;
2012-08-23 20:27:00 +02:00
2012-08-24 00:03:22 +02:00
check ( " void f(bool& b1, bool& b2) { \n "
" b1 = bar() % 5 < 889; \n "
" if(x[593] % 5 <= 5) \n "
" b2 = x.a % 5 == 5; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n "
2012-04-26 23:04:55 +02:00
" [test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n "
" [test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5. \n " , errout . str ( ) ) ;
2012-04-26 15:23:47 +02:00
}
2011-10-13 20:53:06 +02:00
void incorrectLogicOperator1 ( ) {
2010-11-04 08:10:25 +01:00
check ( " void f(int x) { \n "
2010-10-25 03:14:21 +02:00
" if ((x != 1) || (x != 3)) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x != 1 || x != 3. \n " , errout . str ( ) ) ;
2010-10-25 03:14:21 +02:00
2010-11-04 08:10:25 +01:00
check ( " void f(int x) { \n "
2010-10-25 03:14:21 +02:00
" if (1 != x || 3 != x) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x != 1 || x != 3. \n " , errout . str ( ) ) ;
2010-10-25 03:14:21 +02:00
2010-11-04 08:10:25 +01:00
check ( " void f(int x, int y) { \n "
2010-10-25 03:14:21 +02:00
" if (x != 1 || y != 1) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 08:10:25 +01:00
check ( " void f(int x, int y) { \n "
2010-10-25 03:14:21 +02:00
" if ((y == 1) && (x != 1) || (x != 3)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 08:10:25 +01:00
check ( " void f(int x, int y) { \n "
2010-10-25 03:14:21 +02:00
" if ((x != 1) || (x != 3) && (y == 1)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 08:10:25 +01:00
check ( " void f(int x) { \n "
2010-10-25 03:14:21 +02:00
" if ((x != 1) && (x != 3)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 08:10:25 +01:00
check ( " void f(int x) { \n "
2010-10-25 03:14:21 +02:00
" if ((x == 1) || (x == 3)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 08:10:25 +01:00
check ( " void f(int x, int y) { \n "
2010-10-25 03:14:21 +02:00
" if ((x != 1) || (y != 3)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 08:10:25 +01:00
check ( " void f(int x, int y) { \n "
" if ((x != hotdog) || (y != hotdog)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x, int y) { \n "
" if ((x != 5) || (y != 5)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
2013-04-13 20:17:53 +02:00
" if ((x != 5) || (x != 6)) \n "
2010-11-04 08:10:25 +01:00
" a++; \n "
" } \n "
) ;
2013-04-13 20:17:53 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x != 5 || x != 6. \n " , errout . str ( ) ) ;
2010-11-04 08:10:25 +01:00
2010-11-21 09:06:43 +01:00
check ( " void f(unsigned int a, unsigned int b, unsigned int c) { \n "
" if((a != b) || (c != b) || (c != a)) \n "
" { \n "
" return true; \n "
" } \n "
" return false; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-07-17 04:06:23 +02:00
}
2011-10-13 20:53:06 +02:00
void incorrectLogicOperator2 ( ) {
2011-07-17 05:05:35 +02:00
check ( " void f(float x) { \n "
" if ((x == 1) && (x == 1.0)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x == 1) && (x == 0x00000001)) \n "
" a++; \n "
" } \n "
) ;
2011-11-08 21:22:31 +01:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2011-07-17 05:05:35 +02:00
2011-07-17 04:06:23 +02:00
check ( " void f(int x) { \n "
" if (x == 1 && x == 3) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x == 1 && x == 3. \n " , errout . str ( ) ) ;
2011-07-17 04:06:23 +02:00
2011-07-17 05:05:35 +02:00
check ( " void f(int x) { \n "
" if (x == 1.0 && x == 3.0) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x == 1.0 && x == 3.0. \n " , errout . str ( ) ) ;
2011-07-17 05:05:35 +02:00
check ( " void f(float x) { \n "
" if (x == 1 && x == 1.0) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (x < 1 && x > 1) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 1. \n " , errout . str ( ) ) ;
2011-07-17 05:05:35 +02:00
check ( " void f(int x) { \n "
" if (x < 1.0 && x > 1.0) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1.0 && x > 1.0. \n " , errout . str ( ) ) ;
2011-07-17 05:05:35 +02:00
check ( " void f(int x) { \n "
" if (x < 1 && x > 1.0) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 1.0. \n " , errout . str ( ) ) ;
2011-07-17 05:05:35 +02:00
2011-07-17 04:06:23 +02:00
check ( " void f(int x) { \n "
" if (x < 1 && x > 3) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2011-07-17 04:06:23 +02:00
2011-07-17 05:05:35 +02:00
check ( " void f(float x) { \n "
" if (x < 1.0 && x > 3.0) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1.0 && x > 3.0. \n " , errout . str ( ) ) ;
2011-07-17 05:05:35 +02:00
2011-07-17 04:06:23 +02:00
check ( " void f(int x) { \n "
" if (1 > x && 3 < x) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2011-07-17 04:06:23 +02:00
check ( " void f(int x) { \n "
" if (x < 3 && x > 1) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (x > 3 || x < 10) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x > 3 || x < 10. \n " , errout . str ( ) ) ;
2011-07-17 04:06:23 +02:00
2011-07-17 22:28:00 +02:00
check ( " void f(int x) { \n "
" if (x >= 3 || x <= 10) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x >= 3 || x <= 10. \n " , errout . str ( ) ) ;
2011-07-17 22:28:00 +02:00
check ( " void f(int x) { \n "
" if (x >= 3 || x < 10) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x >= 3 || x < 10. \n " , errout . str ( ) ) ;
2011-07-17 22:28:00 +02:00
check ( " void f(int x) { \n "
" if (x > 3 || x <= 10) \n "
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x > 3 || x <= 10. \n " , errout . str ( ) ) ;
2011-07-17 22:28:00 +02:00
2011-07-17 04:06:23 +02:00
check ( " void f(int x) { \n "
" if (x > 3 || x < 3) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-07-17 22:28:00 +02:00
check ( " void f(int x) { \n "
" if (x >= 3 || x <= 3) \n "
" a++; \n "
2012-03-12 19:06:30 +01:00
" } "
2011-07-17 22:28:00 +02:00
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x >= 3 || x <= 3. \n " , errout . str ( ) ) ;
2011-07-17 22:28:00 +02:00
check ( " void f(int x) { \n "
" if (x >= 3 || x < 3) \n "
" a++; \n "
2012-03-12 19:06:30 +01:00
" } "
2011-07-17 22:28:00 +02:00
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x >= 3 || x < 3. \n " , errout . str ( ) ) ;
2011-07-17 22:28:00 +02:00
check ( " void f(int x) { \n "
" if (x > 3 || x <= 3) \n "
" a++; \n "
2012-03-12 19:06:30 +01:00
" } "
2011-07-17 22:28:00 +02:00
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x > 3 || x <= 3. \n " , errout . str ( ) ) ;
2011-07-17 22:28:00 +02:00
2012-09-01 13:39:32 +02:00
check ( " void f(int x) { \n "
" if((x==3) && (x!=4)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x == 3, the comparison x != 4 is always true. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x!=4) && (x==3)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x == 3, the comparison x != 4 is always true. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x==3) || (x!=4)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x == 3, the comparison x != 4 is always true. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x!=4) || (x==3)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x == 3, the comparison x != 4 is always true. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x==3) && (x!=3)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x != 3 && x == 3. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x==6) || (x!=6)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical disjunction always evaluates to true: x != 6 || x == 6. \n " , errout . str ( ) ) ;
2011-07-17 04:06:23 +02:00
check ( " void f(int x) { \n "
" if (x > 10 || x < 3) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-23 16:08:53 +01:00
2011-08-19 19:28:37 +02:00
check ( " void f(int x) { \n "
2012-03-15 20:38:28 +01:00
" if (x > 5 && x == 1) \n "
2011-08-19 19:28:37 +02:00
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x > 5 && x == 1. \n " , errout . str ( ) ) ;
2011-08-19 19:28:37 +02:00
check ( " void f(int x) { \n "
2012-03-15 20:38:28 +01:00
" if (x > 5 && x == 6) \n "
2011-08-19 19:28:37 +02:00
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-03-15 20:38:28 +01:00
// #3419
check ( " void f() { \n "
" if ( &q != &a && &q != &b ) { } \n "
" } " ) ;
2011-08-19 19:28:37 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-04-02 11:21:04 +02:00
// #3676
check ( " void f(int m_x2, int w, int x) { \n "
" if (x + w - 1 > m_x2 || m_x2 < 0 ) \n "
" m_x2 = x + w - 1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-03-15 20:38:28 +01:00
}
2011-08-19 19:28:37 +02:00
2012-03-15 20:38:28 +01:00
void incorrectLogicOperator3 ( ) {
check ( " void f(int x, bool& b) { \n "
" b = x > 5 && x == 1; \n "
2012-09-02 13:09:32 +02:00
" c = x < 1 && x == 3; \n "
" d = x >= 5 && x == 1; \n "
" e = x <= 1 && x == 3; \n "
2012-03-15 20:38:28 +01:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x > 5 && x == 1. \n "
" [test.cpp:3]: (warning) Logical conjunction always evaluates to false: x < 1 && x == 3. \n "
" [test.cpp:4]: (warning) Logical conjunction always evaluates to false: x >= 5 && x == 1. \n "
" [test.cpp:5]: (warning) Logical conjunction always evaluates to false: x <= 1 && x == 3. \n " , errout . str ( ) ) ;
}
2011-08-19 19:28:37 +02:00
2012-03-15 20:38:28 +01:00
void secondAlwaysTrueFalseWhenFirstTrueError ( ) {
2011-08-19 19:28:37 +02:00
check ( " void f(int x) { \n "
2012-03-15 20:38:28 +01:00
" if (x > 5 && x != 1) \n "
2011-08-19 19:28:37 +02:00
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x > 5, the comparison x != 1 is always true. \n " , errout . str ( ) ) ;
2011-08-19 19:28:37 +02:00
check ( " void f(int x) { \n "
2012-03-15 20:38:28 +01:00
" if (x > 5 && x != 6) \n "
2011-08-19 19:28:37 +02:00
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
2012-03-15 20:38:28 +01:00
" if ((x > 5) && (x != 1)) \n "
2011-08-19 19:28:37 +02:00
" a++; \n "
" } \n "
) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x > 5, the comparison x != 1 is always true. \n " , errout . str ( ) ) ;
2011-08-19 19:28:37 +02:00
check ( " void f(int x) { \n "
2012-03-15 20:38:28 +01:00
" if ((x > 5) && (x != 6)) \n "
2011-08-19 19:28:37 +02:00
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-03-15 20:38:28 +01:00
check ( " void f(int x, bool& b) { \n "
2012-07-13 15:01:19 +02:00
" b = x > 3 || x == 4; \n "
2012-09-02 13:09:32 +02:00
" c = x < 5 || x == 4; \n "
" d = x >= 3 || x == 4; \n "
" e = x <= 5 || x == 4; \n "
2012-07-13 15:01:19 +02:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x == 4, the comparison x > 3 is always true. \n "
" [test.cpp:3]: (style) Redundant condition: If x == 4, the comparison x < 5 is always true. \n "
" [test.cpp:4]: (style) Redundant condition: If x == 4, the comparison x >= 3 is always true. \n "
" [test.cpp:5]: (style) Redundant condition: If x == 4, the comparison x <= 5 is always true. \n " , errout . str ( ) ) ;
check ( " void f(int x, bool& b) { \n "
2012-03-15 20:38:28 +01:00
" b = x > 5 || x != 1; \n "
2012-09-02 13:09:32 +02:00
" c = x < 1 || x != 3; \n "
" d = x >= 5 || x != 1; \n "
" e = x <= 1 || x != 3; \n "
2012-03-15 20:38:28 +01:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x > 5, the comparison x != 1 is always true. \n "
" [test.cpp:3]: (style) Redundant condition: If x < 1, the comparison x != 3 is always true. \n "
" [test.cpp:4]: (style) Redundant condition: If x >= 5, the comparison x != 1 is always true. \n "
" [test.cpp:5]: (style) Redundant condition: If x <= 1, the comparison x != 3 is always true. \n " , errout . str ( ) ) ;
2011-08-19 19:28:37 +02:00
2012-03-15 20:38:28 +01:00
check ( " void f(int x, bool& b) { \n "
" b = x > 6 && x > 5; \n "
2012-09-02 13:09:32 +02:00
" c = x > 5 || x > 6; \n "
" d = x < 6 && x < 5; \n "
" e = x < 5 || x < 6; \n "
2012-03-15 20:38:28 +01:00
" } " ) ;
2012-06-15 16:54:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant condition: If x > 6, the comparison x > 5 is always true. \n "
2012-03-15 20:38:28 +01:00
" [test.cpp:3]: (style) Redundant condition: If x > 5, the comparison x > 6 is always true. \n "
" [test.cpp:4]: (style) Redundant condition: If x < 6, the comparison x < 5 is always true. \n "
" [test.cpp:5]: (style) Redundant condition: If x < 6, the comparison x < 5 is always true. \n " , errout . str ( ) ) ;
2011-08-19 19:28:37 +02:00
}
2012-03-12 19:06:30 +01:00
void incorrectLogicOp_condSwapping ( ) {
check ( " void f(int x) { \n "
" if (x < 1 && x > 3) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
check ( " void f(int x) { \n "
" if (1 > x && x > 3) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
check ( " void f(int x) { \n "
" if (x < 1 && 3 < x) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
check ( " void f(int x) { \n "
" if (1 > x && 3 < x) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
check ( " void f(int x) { \n "
" if (x > 3 && x < 1) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
check ( " void f(int x) { \n "
" if (3 < x && x < 1) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
check ( " void f(int x) { \n "
" if (x > 3 && 1 > x) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
check ( " void f(int x) { \n "
" if (3 < x && 1 > x) \n "
" a++; \n "
" } " ) ;
2012-03-15 20:38:28 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3. \n " , errout . str ( ) ) ;
2012-03-12 19:06:30 +01:00
}
2011-10-10 19:11:17 +02:00
2012-06-23 19:54:15 +02:00
void sameExpression ( ) {
// #3868 - false positive (same expression on both sides of |)
check ( " void f(int x) { \n "
" a = x ? A | B | C \n "
" : A | B; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void memsetZeroBytes ( ) {
2011-01-06 11:31:58 +01:00
check ( " void f() { \n "
2011-10-22 22:34:10 +02:00
" memset(p, 10, 0x0); \n "
2011-01-06 11:31:58 +01:00
" } \n "
) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) memset() called to fill 0 bytes of 'p'. \n " , errout . str ( ) ) ;
2011-01-06 11:31:58 +01:00
check ( " void f() { \n "
2011-10-20 21:46:34 +02:00
" memset(p, sizeof(p), 0); \n "
2011-01-06 11:31:58 +01:00
" } \n "
) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) memset() called to fill 0 bytes of 'p'. \n " , errout . str ( ) ) ;
2011-01-30 12:54:19 +01:00
2011-10-22 22:34:10 +02:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" memset(p, sizeof(p), i); \n "
2011-10-22 22:34:10 +02:00
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-06 11:31:58 +01:00
}
2011-01-22 19:21:56 +01:00
2013-02-11 20:26:27 +01:00
void redundantGetAndSetUserId ( ) {
check ( " seteuid(geteuid()); \n " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (warning) Redundant get and set of user id. \n " , errout . str ( ) ) ;
check ( " setuid(getuid()); \n " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (warning) Redundant get and set of user id. \n " , errout . str ( ) ) ;
check ( " setgid(getgid()); \n " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (warning) Redundant get and set of user id. \n " , errout . str ( ) ) ;
check ( " setegid(getegid()); \n " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (warning) Redundant get and set of user id. \n " , errout . str ( ) ) ;
check ( " seteuid(getuid()); \n " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " seteuid(foo()); \n " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " foo(getuid()); \n " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void clarifyCalculation ( ) {
2011-01-24 21:40:49 +01:00
check ( " int f(char c) { \n "
" return 10 * (c == 0) ? 1 : 2; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Clarify calculation precedence for '*' and '?'. \n " , errout . str ( ) ) ;
2011-01-24 19:04:56 +01:00
2011-01-24 21:40:49 +01:00
check ( " void f(char c) { \n "
" printf( \" %i \" , 10 * (c == 0) ? 1 : 2); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Clarify calculation precedence for '*' and '?'. \n " , errout . str ( ) ) ;
2011-02-17 21:30:59 +01:00
// Ticket #2585 - segmentation fault for invalid code
2013-03-05 10:21:59 +01:00
check ( " abcdef? " " ?< "
" 123456? " " ?> "
" +? " " ?= " ) ;
2011-02-17 21:30:59 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-02 11:43:20 +02:00
check ( " void f(char c) { \n "
2012-03-27 21:29:50 +02:00
" printf( \" %i \" , 1 + 1 ? 1 : 2); \n " // "1+1" is simplified away
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-09-16 19:44:02 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:2]: (style) Clarify calculation precedence for '+' and '?'. \n " , " " , errout . str ( ) ) ; // TODO: Is that really necessary, or is this pattern too unlikely?
2011-04-03 22:12:22 +02:00
check ( " void f() { \n "
" std::cout << x << 1 ? 2 : 3; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Clarify calculation precedence for '<<' and '?'. \n " , errout . str ( ) ) ;
2011-04-04 21:33:16 +02:00
check ( " void f() { \n "
" int ab = a - b ? 2 : 3; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Clarify calculation precedence for '-' and '?'. \n " , errout . str ( ) ) ;
2011-09-06 22:37:19 +02:00
// ticket #195
check ( " int f(int x, int y) { \n "
" return x >> ! y ? 8 : 2; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Clarify calculation precedence for '>>' and '?'. \n " , errout . str ( ) ) ;
2011-12-23 12:13:39 +01:00
check ( " int f() { \n "
2013-04-13 20:17:53 +02:00
" return shift < sizeof(int64_t)*8 ? 1 : 2; \n "
2011-12-23 12:13:39 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-22 19:21:56 +01:00
}
2012-08-24 11:28:50 +02:00
void clarifyStatement ( ) {
check ( " char* f(char* c) { \n "
" *c++; \n "
" return c; \n "
" } " ) ;
2012-09-15 20:19:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'? \n " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
check ( " char* f(char** c) { \n "
" *c[5]--; \n "
" return *c; \n "
" } " ) ;
2012-09-15 20:19:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'? \n " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
check ( " void f(Foo f) { \n "
" *f.a++; \n "
" } " ) ;
2012-09-15 20:19:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'? \n " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
check ( " void f(Foo f) { \n "
" *f.a[5].v[3]++; \n "
" } " ) ;
2012-09-15 20:19:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'? \n " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
check ( " void f(Foo f) { \n "
" *f.a(1, 5).v[x + y]++; \n "
" } " ) ;
2012-09-15 20:19:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'? \n " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
check ( " char* f(char* c) { \n "
" (*c)++; \n "
" return c; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(char* c) { \n "
" bar(*c++); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-03 17:10:27 +02:00
check ( " char*** f(char*** c) { \n "
" ***c++; \n "
" return c; \n "
" } " ) ;
2012-09-15 20:19:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'? \n " , errout . str ( ) ) ;
2012-09-03 17:10:27 +02:00
check ( " char** f(char*** c) { \n "
" **c[5]--; \n "
" return **c; \n "
" } " ) ;
2012-09-15 20:19:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Ineffective statement similar to '*A++;'. Did you intend to write '(*A)++;'? \n " , errout . str ( ) ) ;
2012-09-03 17:10:27 +02:00
check ( " char*** f(char*** c) { \n "
" (***c)++; \n "
" return c; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void *f(char** c) { \n "
" bar(**c++); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
}
2011-03-09 22:20:14 +01:00
// clarify conditions with = and comparison
2011-10-13 20:53:06 +02:00
void clarifyCondition1 ( ) {
2011-03-09 22:20:14 +01:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" if (x = b() < 0) {} \n " // don't simplify and verify this code
" } " , 0 , false , false , false , false ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses. \n " , errout . str ( ) ) ;
2011-08-19 13:54:06 +02:00
check ( " void f(int i) { \n "
" for (i = 0; i < 10; i++) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-19 18:06:28 +02:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" x = a<int>(); if (x) {} \n "
2011-08-19 18:06:28 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-09 22:20:14 +01:00
}
2011-08-19 00:14:15 +02:00
// clarify conditions with bitwise operator and comparison
2011-10-13 20:53:06 +02:00
void clarifyCondition2 ( ) {
2011-08-19 00:14:15 +02:00
check ( " void f() { \n "
" if (x & 2 == 2) {} \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses. \n " , errout . str ( ) ) ;
2011-08-19 00:56:15 +02:00
check ( " void f() { \n "
" if (a & fred1.x == fred2.y) {} \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses. \n " , errout . str ( ) ) ;
2011-08-19 00:14:15 +02:00
}
2011-08-19 13:40:54 +02:00
// clarify condition that uses ! operator and then bitwise operator
2011-10-13 20:53:06 +02:00
void clarifyCondition3 ( ) {
2011-08-19 13:40:54 +02:00
check ( " void f(int w) { \n "
" if(!w & 0x8000) {} \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses. \n " , errout . str ( ) ) ;
2011-08-19 13:40:54 +02:00
check ( " void f() { \n "
" if (x == foo() & 2) {} \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses. \n " , errout . str ( ) ) ;
2011-08-19 17:07:26 +02:00
check ( " void f(std::list<int> &ints) { } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-19 18:06:28 +02:00
check ( " void f() { A<x &> a; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-19 18:55:20 +02:00
2012-06-23 07:29:49 +02:00
check ( " void f() { a(x<y|z,0); } " , " test.c " ) ; // filename is c => there are never templates
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.c:1]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses. \n " , errout . str ( ) ) ;
2012-06-23 07:29:49 +02:00
2011-09-11 09:46:01 +02:00
check ( " class A<B&,C>; " , " test.C " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-19 18:55:20 +02:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" if (result != (char *)&inline_result) { } \n " // don't simplify and verify cast
" } " , 0 , false , false , false , false ) ;
2011-08-19 18:55:20 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-19 13:40:54 +02:00
}
2011-10-13 20:53:06 +02:00
void clarifyCondition4 ( ) { // ticket #3110
2011-09-13 02:46:00 +02:00
check ( " typedef double SomeType; \n "
" typedef std::pair<std::string,SomeType> PairType; \n "
" struct S \n "
" { \n "
" bool operator() \n "
" ( PairType const & left \n "
" , PairType const & right) const \n "
" { \n "
2013-04-13 20:17:53 +02:00
" return left.first < right.first; \n "
2011-09-13 02:46:00 +02:00
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-04-08 18:07:11 +02:00
void clarifyCondition5 ( ) { // ticket #3609 (using | in template instantiation)
check ( " CWinTraits<WS_CHILD|WS_VISIBLE>::GetWndStyle(0); " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2012-08-02 13:03:01 +02:00
void clarifyCondition6 ( ) {
check ( " template<class Y> \n "
" SharedPtr& operator=( SharedPtr<Y> const & r ) { \n "
" px = r.px; \n "
" return *this; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void incorrectStringCompare ( ) {
2011-02-08 19:49:29 +01:00
check ( " int f() { \n "
" return test.substr( 0 , 4 ) == \" Hello \" ? : 0 : 1 ; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) String literal \" Hello \" doesn't match length argument for substr(). \n " , errout . str ( ) ) ;
2011-02-08 19:49:29 +01:00
check ( " int f() { \n "
" return test.substr( 0 , 5 ) == \" Hello \" ? : 0 : 1 ; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" return \" Hello \" == test.substr( 0 , 4 ) ? : 0 : 1 ; \n "
2011-02-08 19:49:29 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) String literal \" Hello \" doesn't match length argument for substr(). \n " , errout . str ( ) ) ;
2011-02-08 19:49:29 +01:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" return \" Hello \" == foo.bar<int>().z[1].substr(i+j*4, 4) ? : 0 : 1 ; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) String literal \" Hello \" doesn't match length argument for substr(). \n " , errout . str ( ) ) ;
check ( " int f() { \n "
" return \" Hello \" == test.substr( 0 , 5 ) ? : 0 : 1 ; \n "
2011-02-08 19:49:29 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" if ( \" Hello \" ) { } \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Conversion of string literal \" Hello \" to bool always evaluates to true. \n " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-13 10:49:17 +02:00
" if ( \" Hello \" && test) { } \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Conversion of string literal \" Hello \" to bool always evaluates to true. \n " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-13 10:49:17 +02:00
" if (test && \" Hello \" ) { } \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Conversion of string literal \" Hello \" to bool always evaluates to true. \n " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" while ( \" Hello \" ) { } \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Conversion of string literal \" Hello \" to bool always evaluates to true. \n " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" assert (test || \" Hello \" ); \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Conversion of string literal \" Hello \" to bool always evaluates to true. \n " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" assert (test && \" Hello \" ); \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" assert ( \" Hello \" || test); \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Conversion of string literal \" Hello \" to bool always evaluates to true. \n " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" assert ( \" Hello \" && test); \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-12-20 20:48:48 +01:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" BOOST_ASSERT ( \" Hello \" && test); \n "
2012-12-20 20:48:48 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-18 21:37:03 +02:00
check ( " int f() { \n "
2013-04-09 18:16:35 +02:00
" return f2( \" Hello \" ); \n "
2011-10-18 21:37:03 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-08 19:49:29 +01:00
}
2011-02-11 23:38:23 +01:00
2011-10-13 20:53:06 +02:00
void duplicateIf ( ) {
2011-04-09 22:34:16 +02:00
check ( " void f(int a, int &b) { \n "
" if (a) { b = 1; } \n "
2013-04-13 20:17:53 +02:00
" else { if (a) { b = 2; } } \n "
2011-04-09 22:34:16 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'. \n " , errout . str ( ) ) ;
2011-04-09 22:34:16 +02:00
2013-02-12 21:43:12 +01:00
check ( " void f(int a, int &b) { \n "
" if (a) { b = 1; } \n "
" else { if (a) { b = 2; } } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'. \n " , errout . str ( ) ) ;
2011-04-09 21:14:01 +02:00
check ( " void f(int a, int &b) { \n "
" if (a == 1) { b = 1; } \n "
2013-04-13 20:17:53 +02:00
" else { if (a == 2) { b = 2; } \n "
" else { if (a == 1) { b = 3; } } } \n "
2011-04-09 21:14:01 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'. \n " , errout . str ( ) ) ;
2011-04-09 21:14:01 +02:00
check ( " void f(int a, int &b) { \n "
" if (a == 1) { b = 1; } \n "
2013-04-13 20:17:53 +02:00
" else { if (a == 2) { b = 2; } \n "
" else { if (a == 2) { b = 3; } } } \n "
2011-04-09 21:14:01 +02:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:3]: (style) Duplicate conditions in 'if' and related 'else if'. \n " , errout . str ( ) ) ;
2011-04-09 21:14:01 +02:00
check ( " void f(int a, int &b) { \n "
" if (a == 1) { \n "
" b = 1; \n "
2013-04-13 20:17:53 +02:00
" if (b == 1) { } \n " // condition is always true. must skip simplifications
2011-04-09 21:14:01 +02:00
" else if (b == 1) { } \n "
" } else if (a == 2) { b = 2; } \n "
" else if (a == 2) { b = 3; } \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:6]: (style) Duplicate conditions in 'if' and related 'else if'. \n "
" [test.cpp:5] -> [test.cpp:4]: (style) Duplicate conditions in 'if' and related 'else if'. \n " , errout . str ( ) ) ;
2011-04-09 21:14:01 +02:00
check ( " void f(int a, int &b) { \n "
" if (a++) { b = 1; } \n "
2013-04-13 20:17:53 +02:00
" else { if (a++) { b = 2; } \n "
" else { if (a++) { b = 3; } } } \n "
2011-04-09 21:14:01 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int a, int &b) { \n "
" if (!strtok(NULL, " " )) { b = 1; } \n "
2013-04-13 20:17:53 +02:00
" else { if (!strtok(NULL, " " )) { b = 2; } } \n "
2011-04-09 21:14:01 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int a, int &b) { \n "
2013-04-13 20:17:53 +02:00
" x = x / 2; \n "
" if (x < 100) { b = 1; } \n "
" else { x = x / 2; if (x < 100) { b = 2; } } \n "
2011-04-09 21:14:01 +02:00
" } " ) ;
2012-02-17 15:47:08 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int i) { \n "
" if(i == 0x02e2000000 || i == 0xa0c6000000) \n "
" foo(i); \n "
" } " ) ;
2011-04-09 21:14:01 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-29 09:22:42 +02:00
check ( " void f(const std::string &token) \n "
" { \n "
" if( token == \" C \" ) \n "
" { \n "
2012-09-17 13:18:19 +02:00
" std::cout << \" C \" ; \n "
2012-05-29 09:22:42 +02:00
" } \n "
2013-04-13 20:17:53 +02:00
" else { if ( token == \" A \" ) \n "
2012-05-29 09:22:42 +02:00
" { \n "
" std::cout << \" A \" ; \n "
" } \n "
2013-04-13 20:17:53 +02:00
" else { if ( token == \" A \" ) \n "
2012-05-29 09:22:42 +02:00
" { \n "
" std::cout << \" A \" ; \n "
" } \n "
2013-04-13 20:17:53 +02:00
" } \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:11] -> [test.cpp:7]: (style) Duplicate conditions in 'if' and related 'else if'. \n " , errout . str ( ) ) ;
2011-04-09 21:14:01 +02:00
}
2011-04-09 23:05:27 +02:00
2011-10-13 20:53:06 +02:00
void duplicateBranch ( ) {
2011-04-09 23:05:27 +02:00
check ( " void f(int a, int &b) { \n "
" if (a) \n "
" b = 1; \n "
" else \n "
" b = 1; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:2]: (style) Found duplicate branches for 'if' and 'else'. \n " , errout . str ( ) ) ;
2011-04-09 23:05:27 +02:00
check ( " void f(int a, int &b) { \n "
" if (a) { \n "
" if (a == 1) \n "
" b = 2; \n "
" else \n "
" b = 2; \n "
" } else \n "
" b = 1; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:3]: (style) Found duplicate branches for 'if' and 'else'. \n " , errout . str ( ) ) ;
2011-04-10 15:57:09 +02:00
2011-12-13 21:42:38 +01:00
check ( " void f(int a, int &b) { \n "
" if (a == 1) \n "
" b = 1; \n "
2013-04-13 20:17:53 +02:00
" else { \n "
" if (a) \n "
" b = 2; \n "
" else \n "
" b = 2; \n "
" } \n "
2011-12-13 21:42:38 +01:00
" } " ) ;
2013-04-13 20:17:53 +02:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:5]: (style) Found duplicate branches for 'if' and 'else'. \n " , errout . str ( ) ) ;
2011-12-13 21:42:38 +01:00
2011-04-10 15:57:09 +02:00
check ( " int f(int signed, unsigned char value) { \n "
" int ret; \n "
" if (signed) \n "
2013-04-13 20:17:53 +02:00
" ret = (signed char)value; \n " // cast must be kept so the simplifications and verification is skipped
2011-04-10 15:57:09 +02:00
" else \n "
" ret = (unsigned char)value; \n "
" return ret; \n "
2013-04-13 20:17:53 +02:00
" } " , 0 , false , false , false , false ) ;
2011-04-10 15:57:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-13 21:42:38 +01:00
check ( " void f() { \n "
" if (b) \n "
" __asm__( \" mov ax, bx \" ); \n "
" else \n "
" __asm__( \" mov bx, bx \" ); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // #3407
check ( " void f() { \n "
" if (b) \n "
" __asm__( \" mov ax, bx \" ); \n "
" else \n "
" __asm__( \" mov ax, bx \" ); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:2]: (style) Found duplicate branches for 'if' and 'else'. \n " , errout . str ( ) ) ;
2011-04-09 23:05:27 +02:00
}
2011-04-10 16:25:02 +02:00
2012-05-30 01:30:37 +02:00
void duplicateBranch1 ( ) {
2012-07-31 23:35:56 +02:00
// tests inspired by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck )
2012-05-30 01:30:37 +02:00
// Errors detected in Quake 3: Arena by PVS-Studio: Fragement 2
check ( " void f() \n "
" { \n "
2012-12-06 19:19:22 +01:00
" if (front < 0) \n "
2013-04-13 20:17:53 +02:00
" frac = front/(front-back); \n "
2012-12-06 19:19:22 +01:00
" else \n "
2013-04-13 20:17:53 +02:00
" frac = front/(front-back); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:3]: (style) Found duplicate branches for 'if' and 'else'. \n " , errout . str ( ) ) ;
2012-05-30 01:30:37 +02:00
check ( " void f() \n "
" { \n "
2012-12-06 19:19:22 +01:00
" if (front < 0) \n "
2013-04-13 20:17:53 +02:00
" { frac = front/(front-back);} \n "
2012-12-06 19:19:22 +01:00
" else \n "
2013-04-13 20:17:53 +02:00
" frac = front/((front-back)); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:3]: (style) Found duplicate branches for 'if' and 'else'. \n " , errout . str ( ) ) ;
2012-05-30 01:30:37 +02:00
}
2013-02-18 17:18:33 +01:00
void duplicateBranch2 ( ) {
Preprocessor : : macroChar = ' $ ' ;
check ( " void f(int x) { \n " // #4329
" if (x) \n "
" $; \n "
" else \n "
" $; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-18 17:18:33 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void duplicateExpression1 ( ) {
2011-10-08 12:45:56 +02:00
check ( " void foo() { \n "
2011-04-10 16:25:02 +02:00
" if (a == a) { } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
check ( " void fun() { \n "
2013-04-13 20:17:53 +02:00
" return a && a || \n "
2011-04-10 16:25:02 +02:00
" b == b && \n "
" c - c && \n "
" d > d && \n "
" e < e && \n "
2013-04-13 20:17:53 +02:00
" f ; \n "
2011-04-10 16:25:02 +02:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'. \n "
" [test.cpp:3] -> [test.cpp:3]: (style) Same expression on both sides of '=='. \n "
" [test.cpp:4] -> [test.cpp:4]: (style) Same expression on both sides of '-'. \n "
" [test.cpp:5] -> [test.cpp:5]: (style) Same expression on both sides of '>'. \n "
" [test.cpp:6] -> [test.cpp:6]: (style) Same expression on both sides of '<'. \n " , errout . str ( ) ) ;
2011-11-05 15:40:43 +01:00
check ( " void foo() { \n "
" return a && a; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2011-11-05 22:17:48 +01:00
check ( " void foo() { \n "
" a = b && b; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
2011-11-08 21:26:33 +01:00
" f(a,b == b); \n "
2011-11-05 22:17:48 +01:00
" } " ) ;
2011-11-08 21:26:33 +01:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
" f(b == b, a); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
2011-11-08 21:22:31 +01:00
check ( " void foo() { \n "
" if (x!=2 || x!=2) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if (x!=2 || x!=3 || x!=2) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if (a && b || a && b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if (a && b || b && c) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-08 21:24:44 +01:00
check ( " void foo() { \n "
" if (a && b | b && c) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if ((a + b) | (a + b)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '|'. \n " , errout . str ( ) ) ;
2011-11-12 10:23:34 +01:00
2011-11-08 21:24:44 +01:00
check ( " void foo() { \n "
" if ((a | b) & (a | b)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&'. \n " , errout . str ( ) ) ;
2011-11-12 10:23:34 +01:00
2011-12-01 17:07:55 +01:00
check ( " void foo() { \n "
" if (a1[a2[c & 0xff] & 0xff]) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-12 10:23:34 +01:00
check ( " void d(const char f, int o, int v) \n "
" { \n "
" if (((f=='R') && (o == 1) && ((v < 2) || (v > 99))) || \n "
" ((f=='R') && (o == 2) && ((v < 2) || (v > 99))) || \n "
" ((f=='T') && (o == 2) && ((v < 200) || (v > 9999)))) {} \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-11-12 10:23:34 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-14 09:57:30 +02:00
}
void duplicateIf1 ( ) { // ticket 3689 ( avoid false positive )
2012-11-30 12:40:22 +01:00
check ( " int fitInt(long long int nValue){ \n "
" if( nValue < 0x7fffffffLL ) \n "
2012-05-14 09:57:30 +02:00
" { \n "
2012-09-17 13:18:19 +02:00
" return 32; \n "
2012-05-14 09:57:30 +02:00
" } \n "
2012-11-30 12:40:22 +01:00
" if( nValue < 0x7fffffffffffLL ) \n "
2012-05-14 09:57:30 +02:00
" { \n "
2012-09-17 13:18:19 +02:00
" return 48; \n "
2012-05-14 09:57:30 +02:00
" } \n "
2013-04-13 20:17:53 +02:00
" else { \n "
" if( nValue < 0x7fffffffffffffffLL ) \n "
" { \n "
" return 64; \n "
" } else \n "
" { \n "
" return -1; \n "
" } \n "
2012-05-14 09:57:30 +02:00
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-14 09:57:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-10 16:25:02 +02:00
}
2011-04-19 01:20:27 +02:00
2013-01-31 17:29:31 +01:00
void duplicateExpression2 ( ) { // check if float is NaN or Inf
2013-04-13 20:17:53 +02:00
check ( " int f(long double ldbl, double dbl, float flt) { \n " // ticket #2730
2011-04-19 01:20:27 +02:00
" if (ldbl != ldbl) have_nan = 1; \n "
" if (!(dbl == dbl)) have_nan = 1; \n "
" if (flt != flt) have_nan = 1; \n "
" return have_nan; \n "
" } " ) ;
2013-04-13 20:17:53 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-01-31 17:29:31 +01:00
check ( " float f(float x) { return x-x; } " ) ; // ticket #4485 (Inf)
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-19 01:20:27 +02:00
}
2011-04-26 07:45:27 +02:00
2011-11-10 21:49:14 +01:00
void duplicateExpression3 ( ) {
check ( " void foo() { \n "
" if (x() || x()) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" void foo() const; \n "
" bool bar() const; \n "
" }; \n "
" void A::foo() const { \n "
" if (bar() && bar()) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6] -> [test.cpp:6]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
check ( " struct A { \n "
" void foo(); \n "
" bool bar(); \n "
" bool bar() const; \n "
" }; \n "
" void A::foo() { \n "
" if (bar() && bar()) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class B { \n "
" void bar(int i); \n "
" }; \n "
" class A { \n "
" void bar(int i) const; \n "
" }; \n "
" void foo() { \n "
" B b; \n "
" A a; \n "
" if (b.bar(1) && b.bar(1)) {} \n "
" if (a.bar(1) && a.bar(1)) {} \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-11-10 21:49:14 +01:00
ASSERT_EQUALS ( " [test.cpp:11] -> [test.cpp:11]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
check ( " class D { void strcmp(); }; \n "
" void foo() { \n "
" D d; \n "
" if (d.strcmp() && d.strcmp()) {} \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-11-10 21:49:14 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if ((strcmp(a, b) == 0) || (strcmp(a, b) == 0)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if (str == \" ( \" || str == \" ( \" ) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
2011-12-01 19:49:04 +01:00
check ( " void foo() { \n "
" if (bar(a) && !strcmp(a, b) && bar(a) && !strcmp(a, b)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-10 21:49:14 +01:00
}
2011-12-01 17:46:33 +01:00
void duplicateExpression4 ( ) {
check ( " void foo() { \n "
" if (*a++ != b || *a++ != b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if (*a-- != b || *a-- != b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-11-10 21:49:14 +01:00
2012-05-17 07:26:57 +02:00
void duplicateExpression5 ( ) { // #3749 - macros with same values
Preprocessor : : macroChar = ' $ ' ;
check ( " void f() { \n "
" if ($a == $a) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2013-03-14 19:11:29 +01:00
void duplicateExpression6 ( ) { // #4639
check ( " float IsNan(float value) { return !(value == value); } \n "
" double IsNan(double value) { return !(value == value); } \n "
2013-03-20 15:36:16 +01:00
" long double IsNan(long double value) { return !(value == value); } " ) ;
2013-03-14 19:11:29 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void alwaysTrueFalseStringCompare ( ) {
2011-04-26 07:45:27 +02:00
check_preprocess_suppress (
" #define MACRO \" 00FF00 \" \n "
" int main() \n "
" { \n "
" if (strcmp(MACRO, \" 00FF00 \" ) == 0) "
" { "
" std::cout << \" Equal \n \" "
" } "
" } " ) ;
2012-04-26 23:04:55 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Unnecessary comparison of static strings. \n " , errout . str ( ) ) ;
2011-04-26 07:45:27 +02:00
check_preprocess_suppress (
" int main() \n "
" { \n "
" if (stricmp( \" hotdog \" , \" HOTdog \" ) == 0) "
" { "
" std::cout << \" Equal \n \" "
" } "
" } " ) ;
2012-04-26 23:04:55 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Unnecessary comparison of static strings. \n " , errout . str ( ) ) ;
2011-04-26 07:45:27 +02:00
check_preprocess_suppress (
" #define MACRO \" Hotdog \" \n "
" int main() \n "
" { \n "
" if (QString::compare( \" Hamburger \" , MACRO) == 0) "
" { "
" std::cout << \" Equal \n \" "
" } "
" } " ) ;
2012-04-26 23:04:55 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Unnecessary comparison of static strings. \n " , errout . str ( ) ) ;
2011-04-26 07:45:27 +02:00
check_preprocess_suppress (
" int main() \n "
" { \n "
" if (QString::compare(argv[2], \" hotdog \" ) == 0) "
" { "
" std::cout << \" Equal \n \" "
" } "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-28 22:03:18 +02:00
check_preprocess_suppress (
" int main() \n "
" { \n "
" if (strncmp( \" hotdog \" , \" hotdog \" , 6) == 0) "
" { "
" std::cout << \" Equal \n \" "
" } "
" } " ) ;
2012-04-26 23:04:55 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Unnecessary comparison of static strings. \n " , errout . str ( ) ) ;
2011-10-28 22:05:11 +02:00
2011-10-29 12:21:22 +02:00
check (
2011-10-28 22:05:11 +02:00
" int foo(const char *buf) \n "
" { \n "
" if (strcmp(buf, buf) == 0) "
" { "
" std::cout << \" Equal \n \" "
" } "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Comparison of identical string variables. \n " , errout . str ( ) ) ;
2012-03-11 11:01:39 +01:00
check_preprocess_suppress (
" int main() { \n "
" if ( \" str \" == \" str \" ) { \n "
" std::cout << \" Equal \n \" \n "
" } \n "
" } " ) ;
2012-04-26 23:04:55 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Unnecessary comparison of static strings. \n " , errout . str ( ) ) ;
2012-03-11 11:01:39 +01:00
check_preprocess_suppress (
" int main() { \n "
" if ( \" str \" != \" str \" ) { \n "
" std::cout << \" Equal \n \" \n "
" } \n "
" } " ) ;
2012-04-26 23:04:55 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Unnecessary comparison of static strings. \n " , errout . str ( ) ) ;
2012-03-11 11:01:39 +01:00
check_preprocess_suppress (
" int main() { \n "
" if (a+ \" str \" != \" str \" +b) { \n "
" std::cout << \" Equal \n \" \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-26 07:45:27 +02:00
}
2011-08-07 01:23:09 +02:00
2012-08-24 14:25:17 +02:00
void suspiciousStringCompare ( ) {
check ( " bool foo(char* c) { \n "
" return c == \" x \" ; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead? \n " , errout . str ( ) ) ;
check ( " bool foo(const char* c) { \n "
" return \" x \" == c; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead? \n " , errout . str ( ) ) ;
check ( " bool foo(char* c) { \n "
" return foo+ \" x \" == c; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool foo(char* c) { \n "
" return \" x \" == c+foo; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool foo(const std::string& c) { \n "
" return \" x \" == c; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-10-07 16:26:03 +02:00
2012-11-03 10:25:32 +01:00
check ( " bool foo(const Foo* c) { \n "
" return \" x \" == c->bar(); \n " // #4314
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-10-07 16:26:03 +02:00
// Ticket #4257
check ( " bool foo() { \n "
" MyString *str=Getter(); \n "
" return *str== \" bug \" ; } \n " , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:3]: (warning) String literal compared with variable 'str'. Did you intend to use strcmp() instead? \n " , errout . str ( ) ) ;
// Ticket #4257
check ( " bool foo() { \n "
" MyString *str=Getter(); \n "
2013-03-20 15:36:16 +01:00
" return *str== \" bug \" ; } " ) ;
2012-10-07 16:26:03 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Ticket #4257
check ( " bool foo() { \n "
" MyString **str=OtherGetter(); \n "
2013-03-20 15:36:16 +01:00
" return *str== \" bug \" ; } " ) ;
2012-10-07 16:26:03 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead? \n " ,
2012-10-08 16:23:05 +02:00
" " ,
errout . str ( ) ) ;
2012-10-07 16:26:03 +02:00
// Ticket #4257
check ( " bool foo() { \n "
" MyString str=OtherGetter2(); \n "
2013-03-20 15:36:16 +01:00
" return &str== \" bug \" ; } " ) ;
2012-10-07 16:26:03 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead? \n " ,
2012-10-08 16:23:05 +02:00
" " ,
errout . str ( ) ) ;
2012-08-24 14:25:17 +02:00
}
2011-11-06 18:24:37 +01:00
void check_signOfUnsignedVariable ( const char code [ ] , bool inconclusive = false ) {
2011-08-07 01:23:09 +02:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2011-11-06 18:24:37 +01:00
settings . inconclusive = inconclusive ;
2011-08-07 01:23:09 +02:00
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Check for redundant code..
CheckOther checkOther ( & tokenizer , & settings , this ) ;
checkOther . checkSignOfUnsignedVariable ( ) ;
}
2011-10-13 20:53:06 +02:00
void checkSignOfUnsignedVariable ( ) {
2011-10-09 20:35:46 +02:00
check_signOfUnsignedVariable (
" void foo() { \n "
" for(unsigned char i = 10; i >= 0; i--) "
" printf( \" %u \" , i); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Unsigned variable 'i' can't be negative so it is unnecessary to test it. \n " , errout . str ( ) ) ;
2011-10-09 20:35:46 +02:00
check_signOfUnsignedVariable (
" void foo(bool b) { \n "
" for(unsigned int i = 10; b || i >= 0; i--) "
" printf( \" %u \" , i); \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Unsigned variable 'i' can't be negative so it is unnecessary to test it. \n " , errout . str ( ) ) ;
2011-10-09 20:35:46 +02:00
2011-08-07 01:23:09 +02:00
check_signOfUnsignedVariable (
" bool foo(unsigned int x) { \n "
" if (x < 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x) { \n "
" if (x < 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x) { \n "
" if (0 > x) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x) { \n "
" if (0 > x) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x) { \n "
" if (x >= 0) "
" return true; \n "
" return false; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Unsigned variable 'x' can't be negative so it is unnecessary to test it. \n " , errout . str ( ) ) ;
2011-08-07 01:23:09 +02:00
check_signOfUnsignedVariable (
" bool foo(int x) { \n "
" if (x >= 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-09-27 03:24:34 +02:00
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (x < 0 && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (x < 0 && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (0 > x && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (0 > x && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (x >= 0 && y) "
" return true; \n "
" return false; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Unsigned variable 'x' can't be negative so it is unnecessary to test it. \n " , errout . str ( ) ) ;
2011-09-27 03:24:34 +02:00
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (x >= 0 && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (y && x < 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (y && x < 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (y && 0 > x) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (y && 0 > x) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (y && x >= 0) "
" return true; \n "
" return false; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Unsigned variable 'x' can't be negative so it is unnecessary to test it. \n " , errout . str ( ) ) ;
2011-09-27 03:24:34 +02:00
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (y && x >= 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (x < 0 || y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (x < 0 || y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (0 > x || y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Checking if unsigned variable 'x' is less than zero. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (0 > x || y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(unsigned int x, bool y) { \n "
" if (x >= 0 || y) "
" return true; \n "
" return false; \n "
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Unsigned variable 'x' can't be negative so it is unnecessary to test it. \n " , errout . str ( ) ) ;
2011-09-27 03:24:34 +02:00
check_signOfUnsignedVariable (
" bool foo(int x, bool y) { \n "
" if (x >= 0 || y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-06 18:24:37 +01:00
// #3233 - FP when template is used (template parameter is numeric constant)
{
const char code [ ] =
" template<int n> void foo(unsigned int x) { \n "
" if (x <= n); \n "
" } \n "
" foo<0>(); " ;
check_signOfUnsignedVariable ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable ( code , true ) ;
2012-05-06 19:37:41 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Checking if unsigned variable 'x' is less than zero. This might be a false warning. \n " , errout . str ( ) ) ;
2011-11-06 18:24:37 +01:00
}
2011-08-07 01:23:09 +02:00
}
2011-10-11 08:41:39 +02:00
2012-08-21 12:28:02 +02:00
void checkSignOfPointer ( ) {
check_signOfUnsignedVariable (
" bool foo(int* x) { \n "
" if (x >= 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) A pointer can not be negative so it is either pointless or an error to check if it is not. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int* x) { \n "
" if (*x >= 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int* x) { \n "
" if (x < 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) A pointer can not be negative so it is either pointless or an error to check if it is. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int* x) { \n "
" if (*x < 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(Bar* x) { \n "
" if (0 <= x) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) A pointer can not be negative so it is either pointless or an error to check if it is not. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int* x) { \n "
" if (0 <= x[0]) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(Bar* x) { \n "
" if (0 <= x.y) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(Bar* x) { \n "
" if (0 > x) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) A pointer can not be negative so it is either pointless or an error to check if it is. \n " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(int* x) { \n "
" if (0 > x[0]) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_signOfUnsignedVariable (
" bool foo(Bar* x) { \n "
" if (0 > x.y) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-13 20:53:06 +02:00
void checkForSuspiciousSemicolon1 ( ) {
2011-10-11 08:41:39 +02:00
check (
" void foo() { \n "
" for(int i = 0; i < 10; ++i); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Empty block
check (
" void foo() { \n "
" for(int i = 0; i < 10; ++i); { \n "
" } \n "
" } " ) ;
2013-03-03 18:54:44 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'for' statement. \n " , errout . str ( ) ) ;
2011-10-11 08:41:39 +02:00
check (
" void foo() { \n "
" while (!quit); { \n "
" do_something(); \n "
" } \n "
" } " ) ;
2013-03-03 18:54:44 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'while' statement. \n " , errout . str ( ) ) ;
2011-10-11 08:41:39 +02:00
}
2011-10-13 20:53:06 +02:00
void checkForSuspiciousSemicolon2 ( ) {
2011-10-11 08:41:39 +02:00
check (
" void foo() { \n "
" if (i == 1); { \n "
" do_something(); \n "
" } \n "
" } " ) ;
2013-03-03 18:54:44 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if' statement. \n " , errout . str ( ) ) ;
2011-10-11 08:41:39 +02:00
// Seen this in the wild
check (
" void foo() { \n "
" if (Match()); \n "
" do_something(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo() { \n "
" if (Match()); \n "
" else \n "
" do_something(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo() { \n "
" if (i == 1) \n "
" ; \n "
" { \n "
" do_something(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo() { \n "
" if (i == 1); \n "
" \n "
" { \n "
" do_something(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-10-22 17:12:52 +02:00
2012-01-15 01:19:34 +01:00
void checkDoubleFree ( ) {
check (
" void foo(char *p) { \n "
" free(p); \n "
" free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(char *p, char *r) { \n "
" free(p); \n "
" free(r); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo() { \n "
" free(p); \n "
" free(r); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" if (x < 3) free(p); \n "
2013-04-13 20:17:53 +02:00
" else { if (x > 9) free(p); } \n "
2012-01-15 01:19:34 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" free(p); \n "
" getNext(&p); \n "
" free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" free(p); \n "
" bar(); \n "
" free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" free(p); \n "
2013-04-13 20:17:53 +02:00
" printf( \" Freed memory at location %x \" , p); \n "
2012-01-15 01:19:34 +01:00
" free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(DIR *p) { \n "
" closedir(p); \n "
" closedir(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Directory handle 'p' closed twice. \n " , errout . str ( ) ) ;
check (
" void foo(DIR *p, DIR *r) { \n "
" closedir(p); \n "
" closedir(r); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(DIR *p) { \n "
" if (x < 3) closedir(p); \n "
2013-04-13 20:17:53 +02:00
" else { if (x > 9) closedir(p); } \n "
2012-01-15 01:19:34 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(DIR *p) { \n "
" closedir(p); \n "
" gethandle(&p); \n "
" closedir(p); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(DIR *p) { \n "
" closedir(p); \n "
" gethandle(); \n "
" closedir(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Directory handle 'p' closed twice. \n " , errout . str ( ) ) ;
check (
" void foo(Data* p) { \n "
" free(p->a); \n "
" free(p->b); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
2012-08-24 00:03:22 +02:00
" void f() { \n "
2013-04-13 20:17:53 +02:00
" char *p; p = malloc(100); \n "
2012-08-24 00:03:22 +02:00
" if (x) { \n "
" free(p); \n "
" exit(); \n "
" } \n "
" free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-08-23 20:27:00 +02:00
2012-08-24 00:03:22 +02:00
check (
" void f() { \n "
2013-04-13 20:17:53 +02:00
" char *p; p = malloc(100); \n "
2012-08-24 00:03:22 +02:00
" if (x) { \n "
" free(p); \n "
2012-09-17 13:18:19 +02:00
" x = 0; \n "
2012-08-24 00:03:22 +02:00
" } \n "
" free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
2012-08-23 20:27:00 +02:00
2012-08-24 00:03:22 +02:00
check (
" void f() { \n "
2013-04-13 20:17:53 +02:00
" char *p; p = do_something(); \n "
2012-08-24 00:03:22 +02:00
" free(p); \n "
" p = do_something(); \n "
" free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" g_free(p); \n "
" g_free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(char *p, char *r) { \n "
" g_free(p); \n "
" g_free(r); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" g_free(p); \n "
" getNext(&p); \n "
" g_free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" g_free(p); \n "
" bar(); \n "
" g_free(p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" delete p; \n "
" delete p; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(char *p, char *r) { \n "
" delete p; \n "
" delete r; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" delete p; \n "
" getNext(&p); \n "
" delete p; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" delete p; \n "
" bar(); \n "
" delete p; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" delete[] p; \n "
" delete[] p; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(char *p, char *r) { \n "
" delete[] p; \n "
" delete[] r; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" delete[] p; \n "
" getNext(&p); \n "
" delete[] p; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
" delete[] p; \n "
" bar(); \n "
" delete[] p; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Memory pointed to by 'p' is freed twice. \n " , errout . str ( ) ) ;
check (
" ~LineMarker() { \n "
" delete pxpm; \n "
" } \n "
" LineMarker &operator=(const LineMarker &) { \n "
" delete pxpm; \n "
" pxpm = NULL; \n "
" return *this; \n "
" } "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" int* ptr; ptr = NULL; \n "
2012-08-24 00:03:22 +02:00
" try \n "
" { \n "
" ptr = new int(4); \n "
" } \n "
" catch(...) \n "
" { \n "
" delete ptr; \n "
" throw; \n "
" } \n "
" delete ptr; \n "
" } "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" int foo() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" int* a; a = new int; \n "
" bool doDelete; doDelete = true; \n "
2012-09-17 13:18:19 +02:00
" if (a != 0) \n "
" { \n "
" doDelete = false; \n "
" delete a; \n "
" } \n "
" if(doDelete) \n "
" delete a; \n "
" return 0; \n "
2012-08-24 00:03:22 +02:00
" } "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(int y) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char * x; x = NULL; \n "
" while(true) { \n "
2012-08-24 00:03:22 +02:00
" x = new char[100]; \n "
" if (y++ > 100) \n "
" break; \n "
" delete[] x; \n "
" } \n "
" delete[] x; \n "
" } "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(int y) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char * x; x = NULL; \n "
2012-08-24 00:03:22 +02:00
" for (int i = 0; i < 10000; i++) { \n "
" x = new char[100]; \n "
" delete[] x; \n "
" } \n "
" delete[] x; \n "
" } "
) ;
ASSERT_EQUALS ( " [test.cpp:8]: (error) Memory pointed to by 'x' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(int y) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char * x; x = NULL; \n "
2012-08-24 00:03:22 +02:00
" while (isRunning()) { \n "
" x = new char[100]; \n "
" delete[] x; \n "
" } \n "
" delete[] x; \n "
" } "
) ;
ASSERT_EQUALS ( " [test.cpp:8]: (error) Memory pointed to by 'x' is freed twice. \n " , errout . str ( ) ) ;
check (
" void foo(int y) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char * x; x = NULL; \n "
2012-08-24 00:03:22 +02:00
" while (isRunning()) { \n "
" x = malloc(100); \n "
" free(x); \n "
" } \n "
" free(x); \n "
" } "
) ;
2013-01-06 04:02:19 +01:00
TODO_ASSERT_EQUALS ( " [test.cpp:8]: (error) Memory pointed to by 'x' is freed twice. \n " , " " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
check (
" void foo(int y) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char * x; x = NULL; \n "
2012-08-24 00:03:22 +02:00
" for (;;) { \n "
" x = new char[100]; \n "
" if (y++ > 100) \n "
" break; \n "
" delete[] x; \n "
" } \n "
" delete[] x; \n "
" } "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(int y) \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char * x; x = NULL; \n "
2012-08-24 00:03:22 +02:00
" do { \n "
" x = new char[100]; \n "
" if (y++ > 100) \n "
" break; \n "
" delete[] x; \n "
2013-04-13 20:17:53 +02:00
" } while (true); \n "
2012-08-24 00:03:22 +02:00
" delete[] x; \n "
" } "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void f() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char *p; p = 0; \n "
2012-08-24 00:03:22 +02:00
" if (x < 100) { \n "
" p = malloc(10); \n "
" free(p); \n "
" } \n "
" free(p); \n "
" } "
) ;
2013-01-06 04:02:19 +01:00
TODO_ASSERT_EQUALS ( " [test.cpp:8]: (error) Memory pointed to by 'p' is freed twice. \n " , " " , errout . str ( ) ) ;
check (
" void MyFuction() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char* data; data = new char[100]; \n "
2013-01-06 04:02:19 +01:00
" try \n "
" { \n "
" } \n "
" catch(err) \n "
" { \n "
" delete[] data; \n "
" MyThrow(err); \n "
" } \n "
" delete[] data; \n "
" } \n "
" void MyThrow(err) \n "
" { \n "
" throw(err); \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void MyFuction() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" char* data; data = new char[100]; \n "
2013-01-06 04:02:19 +01:00
" try \n "
" { \n "
" } \n "
" catch(err) \n "
" { \n "
" delete[] data; \n "
" MyExit(err); \n "
" } \n "
" delete[] data; \n "
" } \n "
" void MyExit(err) \n "
" { \n "
" exit(err); \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
}
2012-09-05 08:31:23 +02:00
void checkInvalidFree ( ) {
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = malloc(1024); \n "
2012-09-05 08:31:23 +02:00
" free(a + 10); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid memory address freed. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = malloc(1024); \n "
2012-09-05 08:31:23 +02:00
" free(a - 10); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid memory address freed. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = malloc(1024); \n "
2012-09-05 08:31:23 +02:00
" free(10 + a); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid memory address freed. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = new char[1024]; \n "
2012-09-05 08:31:23 +02:00
" delete[] (a + 10); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid memory address freed. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = new char; \n "
" delete a + 10; \n "
2012-09-05 08:31:23 +02:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Invalid memory address freed. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = new char; \n "
2012-09-05 08:31:23 +02:00
" bar(a); \n "
2013-04-13 20:17:53 +02:00
" delete a + 10; \n "
2012-09-05 08:31:23 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = new char; \n "
" char *b; b = new char; \n "
2012-09-05 08:31:23 +02:00
" bar(a); \n "
2013-04-13 20:17:53 +02:00
" delete a + 10; \n "
" delete b + 10; \n "
2012-09-05 08:31:23 +02:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (error) Invalid memory address freed. \n " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = new char; \n "
" char *b; b = new char; \n "
2012-09-05 08:31:23 +02:00
" bar(a, b); \n "
2013-04-13 20:17:53 +02:00
" delete a + 10; \n "
" delete b + 10; \n "
2012-09-05 08:31:23 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check (
" void foo(char *p) { \n "
2013-04-13 20:17:53 +02:00
" char *a; a = new char; \n "
2012-09-05 08:31:23 +02:00
" bar() \n "
2013-04-13 20:17:53 +02:00
" delete a + 10; \n "
2012-09-05 08:31:23 +02:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Invalid memory address freed. \n " , errout . str ( ) ) ;
2012-11-06 06:02:51 +01:00
check (
" void foo(size_t xx) { \n "
2013-04-13 20:17:53 +02:00
" char *ptr; ptr = malloc(42); \n "
2012-11-06 06:02:51 +01:00
" ptr += xx; \n "
" free(ptr - xx - 1); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error, inconclusive) Invalid memory address freed. \n " , errout . str ( ) ) ;
check (
" void foo(size_t xx) { \n "
2013-04-13 20:17:53 +02:00
" char *ptr; ptr = malloc(42); \n "
2012-11-06 06:02:51 +01:00
" std::cout << ptr; \n "
" ptr = otherPtr; \n "
2013-04-13 20:17:53 +02:00
" free(otherPtr - xx - 1); \n "
2012-11-06 06:02:51 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-05 08:31:23 +02:00
}
2012-08-24 00:03:22 +02:00
void check_redundant_copy ( const char code [ ] ) {
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
settings . addEnabled ( " performance " ) ;
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
// Simplify token list..
CheckOther checkOther ( & tokenizer , & settings , this ) ;
tokenizer . simplifyTokenList ( ) ;
checkOther . checkRedundantCopy ( ) ;
}
void checkRedundantCopy ( ) {
check_redundant_copy ( " class A{public:A(){}}; \n "
" const A& getA(){static A a;return a;} \n "
" int main() \n "
" { \n "
" const A a = getA(); \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (performance) Use const reference for 'a' to avoid unnecessary data copying. \n " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
check_redundant_copy ( " const int& getA(){static int a;return a;} \n "
" int main() \n "
" { \n "
" const int a = getA(); \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_redundant_copy ( " const int& getA(){static int a;return a;} \n "
" int main() \n "
" { \n "
" int getA = 0; \n "
" const int a = getA + 3; \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_redundant_copy ( " class A{public:A(){}}; \n "
" const A& getA(){static A a;return a;} \n "
" int main() \n "
" { \n "
" const A a(getA()); \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-09-16 19:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (performance) Use const reference for 'a' to avoid unnecessary data copying. \n " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
check_redundant_copy ( " const int& getA(){static int a;return a;} \n "
" int main() \n "
" { \n "
" const int a(getA()); \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_redundant_copy ( " class A{ \n "
" public:A(int a=0){_a = a;} \n "
" A operator+(const A & a){return A(_a+a._a);} \n "
" private:int _a;}; \n "
" const A& getA(){static A a;return a;} \n "
" int main() \n "
" { \n "
" const A a = getA() + 1; \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check_redundant_copy ( " class A{ \n "
" public:A(int a=0){_a = a;} \n "
" A operator+(const A & a){return A(_a+a._a);} \n "
" private:int _a;}; \n "
" const A& getA(){static A a;return a;} \n "
" int main() \n "
" { \n "
" const A a(getA()+1); \n "
" return 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void checkNegativeShift ( ) {
check ( " void foo() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" int a; a = 123; \n "
2012-08-24 00:03:22 +02:00
" a << -1; \n "
2012-08-22 15:44:20 +02:00
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value. \n " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" int a; a = 123; \n "
2012-08-24 00:03:22 +02:00
" a >> -1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value. \n " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" int a; a = 123; \n "
2012-08-24 00:03:22 +02:00
" a <<= -1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value. \n " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
2013-04-13 20:17:53 +02:00
" int a; a = 123; \n "
2012-08-24 00:03:22 +02:00
" a >>= -1; \n "
2012-08-22 15:44:20 +02:00
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value. \n " , errout . str ( ) ) ;
2012-09-04 12:36:00 +02:00
check ( " void foo() \n "
" { \n "
" std::cout << -1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << a << -1 ; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" std::cout << 3 << -1 ; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-08-22 15:44:20 +02:00
}
2012-08-24 11:28:50 +02:00
void incompleteArrayFill ( ) {
check ( " void f() { \n "
" int a[5]; \n "
" memset(a, 123, 5); \n "
" memcpy(a, b, 5); \n "
" memmove(a, b, 5); \n "
" } " ) ;
2013-05-09 15:32:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:5]: (performance) Buffer 'a' is being written before its old content has been used. \n "
2012-09-02 13:09:32 +02:00
" [test.cpp:3]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memset()' with 'sizeof(*a)'? \n "
2012-08-24 11:28:50 +02:00
" [test.cpp:4]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memcpy()' with 'sizeof(*a)'? \n "
" [test.cpp:5]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memmove()' with 'sizeof(*a)'? \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" Foo* a[5]; \n "
" memset(a, 'a', 5); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memset()' with 'sizeof(*a)'? \n " , errout . str ( ) ) ;
check ( " class Foo {int a; int b;}; \n "
" void f() { \n "
" Foo a[5]; \n "
" memset(a, 'a', 5); \n "
" } " ) ;
TODO_ASSERT_EQUALS ( " [test.cpp:3]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memset()' with 'sizeof(*a)'? \n " , " " , errout . str ( ) ) ;
check ( " void f() { \n "
" Foo a[5]; \n " // Size of foo is unknown
" memset(a, 'a', 5); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" char a[5]; \n "
" memset(a, 'a', 5); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int a[5]; \n "
" memset(a+15, 'a', 5); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" bool a[5]; \n "
" memset(a, false, 5); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (portability, inconclusive) Array 'a' might be filled incompletely. Did you forget to multiply the size given to 'memset()' with 'sizeof(*a)'? \n " , errout . str ( ) ) ;
}
2012-09-02 13:09:32 +02:00
void redundantVarAssignment ( ) {
// Simple tests
check ( " void f(int i) { \n "
" i = 1; \n "
" i = 1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (performance) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
check ( " int i; \n "
" void f() { \n "
" i = 1; \n "
" i = 1; \n "
" } " ) ;
2013-02-15 18:01:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance, inconclusive) Variable 'i' is reassigned a value before the old one has been used if variable is no semaphore variable. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
check ( " void f() { \n "
" int i; \n "
" i = 1; \n "
" i = 1; \n "
2013-04-13 20:17:53 +02:00
" } " , NULL , false , false , false , false ) ;
2012-09-02 13:09:32 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2012-12-07 20:45:20 +01:00
check ( " void f() { \n "
" static int i; \n "
" i = 1; \n "
" i = 1; \n "
" } " ) ;
2013-02-15 18:01:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance, inconclusive) Variable 'i' is reassigned a value before the old one has been used if variable is no semaphore variable. \n " , errout . str ( ) ) ;
2012-12-07 20:45:20 +01:00
2012-09-02 13:09:32 +02:00
// Testing different types
check ( " void f() { \n "
" Foo& bar = foo(); \n "
" bar = x; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-02-15 17:09:31 +01:00
2012-09-02 13:09:32 +02:00
check ( " void f() { \n "
" Foo& bar = foo(); \n "
" bar = x; \n "
2013-02-15 18:40:34 +01:00
" bar = y; \n "
2012-09-02 13:09:32 +02:00
" } " ) ;
2013-02-15 18:01:10 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance, inconclusive) Variable 'bar' is reassigned a value before the old one has been used if variable is no semaphore variable. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
2013-02-15 17:09:31 +01:00
check ( " void f() { \n "
" Foo& bar = foo(); \n " // #4425. bar might refer to something global, etc.
" bar = y(); \n "
" foo(); \n "
2012-09-02 13:09:32 +02:00
" bar = y(); \n "
" } " ) ;
2013-02-15 17:09:31 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
// Tests with function call between assignment
2012-09-03 12:03:30 +02:00
check ( " void f(int i) { \n "
2012-09-02 13:09:32 +02:00
" i = 1; \n "
" bar(); \n "
" i = 1; \n "
" } " ) ;
2012-09-03 12:03:30 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:4]: (performance) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
2012-09-03 12:03:30 +02:00
check ( " int i; \n "
2012-09-02 13:09:32 +02:00
" void f() { \n "
" i = 1; \n "
" bar(); \n " // Global variable might be accessed in bar()
" i = 1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-12-07 20:45:20 +01:00
check ( " void f() { \n "
" static int i; \n "
" i = 1; \n "
" bar(); \n " // bar() might call f() recursively. This could be a false positive in more complex examples (when value of i is used somewhere. See #4229)
" i = 2; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-03 12:03:30 +02:00
check ( " void f() { \n "
2012-09-02 13:09:32 +02:00
" int i; \n "
" i = 1; \n "
" bar(); \n "
" i = 1; \n "
2013-04-13 20:17:53 +02:00
" } " , NULL , false , false , false , false ) ;
2012-09-03 12:03:30 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:5]: (performance) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
check ( " void bar(int i) {} \n "
" void f(int i) { \n "
" i = 1; \n "
" bar(i); \n " // Passed as argument
" i = 1; \n "
2013-04-13 20:17:53 +02:00
" } " , NULL , false , false , false , false ) ;
2012-09-02 13:09:32 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Branch tests
check ( " void f(int i) { \n "
" i = 1; \n "
" if(x) \n "
" i = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int i) { \n "
" if(x) \n "
" i = 0; \n "
" i = 1; \n "
" i = 2; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:5]: (performance) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2013-02-15 18:40:34 +01:00
// #4513
check ( " int x; \n "
" int g() { \n "
" return x*x; \n "
" } \n "
" void f() { \n "
" x = 2; \n "
" x = g(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int g() { \n "
" return x*x; \n "
" } \n "
" void f(int x) { \n "
" x = 2; \n "
" x = g(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:6]: (performance) Variable 'x' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" Foo& bar = foo(); \n "
" bar = x; \n "
" bar = y(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class C { \n "
" int x; \n "
" void g() { return x*x; } \n "
" void f(); \n "
" }; \n "
" \n "
" void C::f() { \n "
" x = 2; \n "
" x = g(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class C { \n "
" int x; \n "
" void g() { return x*x; } \n "
" void f(); \n "
" }; \n "
" \n "
" void C::f(Foo z) { \n "
" x = 2; \n "
" x = z.g(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:8] -> [test.cpp:9]: (performance, inconclusive) Variable 'x' is reassigned a value before the old one has been used if variable is no semaphore variable. \n " , errout . str ( ) ) ;
2013-04-03 09:33:33 +02:00
// from #3103 (avoid a false negative)
check ( " int foo(){ \n "
" int x; \n "
" x = 1; \n "
" x = 1; \n "
" return x + 1; \n "
2013-04-13 20:17:53 +02:00
" } " , NULL , false , false , false , false ) ;
2013-05-09 15:25:36 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance) Variable 'x' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
// from #3103 (avoid a false positive)
check ( " int foo(){ \n "
" int x; \n "
" x = 1; \n "
2013-04-03 09:33:33 +02:00
" if (y) \n " // <-- cppcheck does not know anything about 'y'
2013-04-09 09:49:58 +02:00
" x = 2; \n "
2013-04-03 09:33:33 +02:00
" return x + 1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-05-09 15:39:33 +02:00
check ( " void f() { \n " // Ticket #4356
" int x = 0; \n " // <- ignore assignment with 0
" x = 3; \n "
" } " , 0 , false , false , false , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int i = 54; \n "
" i = 0; \n "
" } " , 0 , false , false , false , false ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (performance) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" int i = 54; \n "
" i = 1; \n "
" } " , 0 , false , false , false , false ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (performance) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
}
void redundantMemWrite ( ) {
// Simple tests
check ( " void f(void* a) { \n "
" memcpy(a, foo, bar); \n "
" memset(a, 0, bar); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (performance) Buffer 'a' is being written before its old content has been used. \n " , errout . str ( ) ) ;
check ( " void* a; \n "
" void f() { \n "
" strcpy(a, foo); \n "
" strncpy(a, 0, bar); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance) Buffer 'a' is being written before its old content has been used. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" void* a = foo(); \n "
" sprintf(a, foo); \n "
" memmove(a, 0, bar); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance) Buffer 'a' is being written before its old content has been used. \n " , errout . str ( ) ) ;
// Writing to different parts of a buffer
check ( " void f(void* a) { \n "
" memcpy(a, foo, bar); \n "
" memset(a+5, 0, bar); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Use variable as second argument
check ( " void f(void* a, void* b) { \n "
" memset(a, 0, 5); \n "
" memcpy(b, a, 5); \n "
" memset(a, 1, 5); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// strcat is special
check ( " void f(void* a) { \n "
" strcpy(a, foo); \n "
" strcat(a, bar); \n " // Not redundant
" strcpy(a, x); \n " // Redundant
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:4]: (performance) Buffer 'a' is being written before its old content has been used. \n " , errout . str ( ) ) ;
// Tests with function call between copy
2012-09-03 12:03:30 +02:00
check ( " void f(void* a) { \n "
2012-09-02 13:09:32 +02:00
" snprintf(a, foo, bar); \n "
" bar(); \n "
2013-05-09 15:25:36 +02:00
" memset(a, 0, size); \n "
2012-09-02 13:09:32 +02:00
" } " ) ;
2012-09-03 12:03:30 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:4]: (performance) Buffer 'a' is being written before its old content has been used. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
2012-09-03 12:03:30 +02:00
check ( " void* a; \n "
2012-09-02 13:09:32 +02:00
" void f() { \n "
" memset(a, 0, size); \n "
" bar(); \n " // Global variable might be accessed in bar()
" memset(a, 0, size); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-03 12:03:30 +02:00
check ( " void f() { \n "
2012-09-02 13:09:32 +02:00
" void* a = foo(); \n "
" memset(a, 0, size); \n "
" bar(); \n "
" memset(a, 0, size); \n "
" } " ) ;
2012-09-03 12:03:30 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:5]: (performance) Buffer 'a' is being written before its old content has been used. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
check ( " void bar(void* a) {} \n "
" void f(void* a) { \n "
" memset(a, 0, size); \n "
" bar(a); \n " // Passed as argument
" memset(a, 0, size); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Branch tests
check ( " void f(void* a) { \n "
" memset(a, 0, size); \n "
" if(x) \n "
" memset(a, 0, size); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-05-09 15:32:02 +02:00
// #4455 - initialization of local buffer
check ( " void f(void) { "
" char buf[10]; \n "
" memset(buf, 0, 10); \n "
" strcpy(buf, string); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(void) { \n "
" char buf[10] = {0}; \n "
" memset(buf, 0, 10); \n "
" strcpy(buf, string); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (performance) Buffer 'buf' is being written before its old content has been used. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
}
2013-01-13 12:02:10 +01:00
void varFuncNullUB ( ) { // #4482
check ( " void a(...); \n "
" void b() { a(NULL); } " ) ;
2013-01-15 20:45:53 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability) Passing NULL after the last typed argument to a variadic function leads to undefined behaviour. \n " , errout . str ( ) ) ;
2013-01-14 06:44:52 +01:00
check ( " void a(char *p, ...); \n "
" void b() { a(NULL, 2); } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-01-13 12:02:10 +01:00
}
2013-02-17 17:33:32 +01:00
void checkPipeParameterSize ( ) { // #3521
check ( " void f(){ \n "
" int pipefd[1]; \n " //<-- array of two integers is needed
" if (pipe(pipefd) == -1) { \n "
" return; \n "
" } \n "
" } " , NULL , false , false , true ) ;
2013-03-03 18:54:44 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Buffer 'pipefd' must have size of 2 integers if used as parameter of pipe(). \n " , errout . str ( ) ) ;
2013-02-17 17:33:32 +01:00
check ( " void f(){ \n "
" int pipefd[2]; \n "
" if (pipe(pipefd) == -1) { \n "
" return; \n "
" } \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(){ \n "
" int pipefd[20]; \n "
" if (pipe(pipefd) == -1) { \n "
" return; \n "
" } \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(){ \n "
" int pipefd[1]; \n " //<-- array of two integers is needed
" if (pipe2(pipefd,0) == -1) { \n "
" return; \n "
" } \n "
" } " , NULL , false , false , true ) ;
2013-03-03 18:54:44 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Buffer 'pipefd' must have size of 2 integers if used as parameter of pipe(). \n " , errout . str ( ) ) ;
2013-02-17 17:33:32 +01:00
check ( " void f(){ \n "
" int pipefd[2]; \n "
" if (pipe2(pipefd,0) == -1) { \n "
" return; \n "
" } \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(){ \n "
" int pipefd[20]; \n "
" if (pipe2(pipefd,0) == -1) { \n "
" return; \n "
" } \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-11 14:22:22 +02:00
// avoid crash with pointer variable
check ( " void foo (int* arrayPtr) \n "
" { \n "
" if (pipe (arrayPtr) < 0) \n "
" {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-02-17 17:33:32 +01:00
}
2013-02-27 21:02:12 +01:00
void checkCastIntToCharAndBack ( ) { // #160
// check getchar
2013-03-04 20:14:56 +01:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" unsigned char c; c = getchar(); \n "
" while( c != EOF) \n "
2013-02-27 21:02:12 +01:00
" { \n "
" bar(c); \n "
2013-04-13 20:17:53 +02:00
" c = getchar(); \n "
2013-02-27 21:02:12 +01:00
" } ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Storing getchar() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
2013-03-04 20:14:56 +01:00
check ( " void f() { \n "
2013-02-27 21:02:12 +01:00
" unsigned char c = getchar(); \n "
" while( EOF != c) \n "
" { \n "
" bar(c); \n "
" } ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Storing getchar() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
2013-03-04 20:14:56 +01:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" unsigned char c; c = getchar(); \n "
" while( EOF != c ) \n "
2013-02-27 21:02:12 +01:00
" { \n "
" bar(c); \n "
2013-04-13 20:17:53 +02:00
" c = getchar(); \n "
2013-02-27 21:02:12 +01:00
" } ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Storing getchar() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
2013-03-04 20:14:56 +01:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" int i; i = getchar(); \n "
" while( i != EOF) \n "
2013-02-27 21:02:12 +01:00
" { \n "
" bar(i); \n "
2013-04-13 20:17:53 +02:00
" i = getchar(); \n "
2013-02-27 21:02:12 +01:00
" } ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-03-04 20:14:56 +01:00
check ( " void f() { \n "
2013-04-13 20:17:53 +02:00
" int i; i = getchar(); \n "
" while( EOF != i ) \n "
2013-02-27 21:02:12 +01:00
" { \n "
" bar(i); \n "
2013-04-13 20:17:53 +02:00
" i = getchar(); \n "
2013-02-27 21:02:12 +01:00
" } ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// check getc
check ( " voif f (FILE * pFile){ \n "
" unsigned char c; \n "
" do { \n "
" c = getc (pFile); \n "
" } while (c != EOF) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Storing getc() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
check ( " voif f (FILE * pFile){ \n "
" unsigned char c; \n "
" do { \n "
" c = getc (pFile); \n "
" } while (EOF != c) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Storing getc() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
check ( " voif f (FILE * pFile){ \n "
" int i; \n "
" do { \n "
" i = getc (pFile); \n "
" } while (i != EOF) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " voif f (FILE * pFile){ \n "
" int i; \n "
" do { \n "
" i = getc (pFile); \n "
" } while (EOF != i) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// check fgetc
check ( " voif f (FILE * pFile){ \n "
" unsigned char c; \n "
" do { \n "
" c = fgetc (pFile); \n "
" } while (c != EOF) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Storing fgetc() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
check ( " voif f (FILE * pFile){ \n "
" char c; \n "
" do { \n "
" c = fgetc (pFile); \n "
" } while (EOF != c) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Storing fgetc() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
check ( " voif f (FILE * pFile){ \n "
" signed char c; \n "
" do { \n "
" c = fgetc (pFile); \n "
" } while (EOF != c) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " voif f (FILE * pFile){ \n "
" int i; \n "
" do { \n "
" i = fgetc (pFile); \n "
" } while (i != EOF) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " voif f (FILE * pFile){ \n "
" int i; \n "
" do { \n "
" i = fgetc (pFile); \n "
" } while (EOF != i) "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-02-27 21:02:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-03-04 14:25:35 +01:00
// cin.get()
check ( " void f(){ \n "
2013-04-13 20:17:53 +02:00
" char ch; ch = std::cin.get(); \n "
" while (EOF != ch) { \n "
2013-03-29 09:30:40 +01:00
" std::cout << ch; \n "
2013-04-13 20:17:53 +02:00
" ch = std::cin.get(); \n "
2013-03-04 14:25:35 +01:00
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
check ( " void f(){ \n "
2013-04-13 20:17:53 +02:00
" char ch; ch = std::cin.get(); \n "
" while (ch != EOF) { \n "
2013-03-29 09:30:40 +01:00
" std::cout << ch; \n "
2013-04-13 20:17:53 +02:00
" ch = std::cin.get(); \n "
2013-03-04 14:25:35 +01:00
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Storing cin.get() return value in char variable and then comparing with EOF. \n " , errout . str ( ) ) ;
check ( " void f(){ \n "
2013-04-13 20:17:53 +02:00
" int i; i = std::cin.get(); \n "
" while ( EOF != i ) { \n "
2013-03-29 09:30:40 +01:00
" std::cout << i; \n "
2013-04-13 20:17:53 +02:00
" i = std::cin.get(); \n "
2013-03-04 14:25:35 +01:00
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(){ \n "
2013-04-13 20:17:53 +02:00
" int i; i = std::cin.get(); \n "
" while ( i != EOF ) { \n "
2013-03-29 09:30:40 +01:00
" std::cout << i; \n "
2013-04-13 20:17:53 +02:00
" i = std::cin.get(); \n "
2013-03-04 14:25:35 +01:00
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-03-11 17:04:30 +01:00
}
void checkSleepTimeIntervall ( ) {
2013-03-11 17:38:03 +01:00
// check usleep(), which is allowed to be called with in a range of [0,999999]
2013-03-11 17:04:30 +01:00
check ( " void f(){ \n "
" usleep(10000); \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-03-04 14:25:35 +01:00
2013-03-11 17:38:03 +01:00
check ( " void f(){ \n "
" usleep(999999); \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(){ \n "
" usleep(1000000); \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) The argument of usleep must be less than 1000000. \n " , errout . str ( ) ) ;
2013-03-11 17:04:30 +01:00
check ( " void f(){ \n "
" usleep(1000001); \n "
" } " , NULL , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) The argument of usleep must be less than 1000000. \n " , errout . str ( ) ) ;
2013-02-27 21:02:12 +01:00
}
2008-12-19 22:15:18 +01:00
} ;
2013-05-09 15:39:33 +02:00
REGISTER_TEST ( TestOther )