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
2018-01-14 15:37:52 +01:00
* Copyright ( C ) 2007 - 2018 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
*/
2009-10-25 12:49:06 +01:00
# include "checkother.h"
2017-05-27 04:33:47 +02:00
# include "library.h"
# include "platform.h"
# include "settings.h"
# include "standards.h"
2008-12-19 22:15:18 +01:00
# include "testsuite.h"
2017-05-27 04:33:47 +02:00
# include "tokenize.h"
2017-05-18 21:52:31 +02:00
# include <simplecpp.h>
2017-05-27 04:33:47 +02:00
# include <tinyxml2.h>
# include <map>
# include <string>
# include <vector>
2008-12-19 22:15:18 +01:00
2011-10-13 20:53:06 +02:00
class TestOther : public TestFixture {
2008-12-19 22:15:18 +01:00
public :
2014-11-20 14:20:09 +01:00
TestOther ( ) : TestFixture ( " TestOther " ) {
2013-08-07 16:27:37 +02:00
}
2008-12-19 22:15:18 +01:00
private :
2016-05-04 15:39:56 +02:00
Settings _settings ;
2018-05-15 16:37:40 +02:00
void run ( ) override {
2016-05-04 15:39:56 +02:00
LOAD_LIB_2 ( _settings . library , " std.cfg " ) ;
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 ) ;
2016-02-02 17:17:33 +01: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 ) ;
2013-07-28 12:32:18 +02:00
TEST_CASE ( zeroDiv7 ) ; // #4930
2014-03-20 16:12:58 +01:00
TEST_CASE ( zeroDiv8 ) ;
2015-07-26 06:28:23 +02:00
TEST_CASE ( zeroDiv9 ) ;
2015-08-01 17:28:54 +02:00
TEST_CASE ( zeroDiv10 ) ;
2015-08-02 14:05:09 +02:00
TEST_CASE ( zeroDiv11 ) ;
2009-03-28 07:49:47 +01:00
2013-09-07 07:40:10 +02:00
TEST_CASE ( zeroDivCond ) ; // division by zero / useless condition
2013-06-10 08:13:08 +02:00
TEST_CASE ( nanInArithmeticExpression ) ;
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 ) ;
2013-10-19 17:27:02 +02:00
TEST_CASE ( varScope20 ) ; // Ticket #5103
2014-03-17 12:34:39 +01:00
TEST_CASE ( varScope21 ) ; // Ticket #5382
2014-04-17 21:32:56 +02:00
TEST_CASE ( varScope22 ) ; // Ticket #5684
2014-09-10 20:56:34 +02:00
TEST_CASE ( varScope23 ) ; // Ticket #6154
2018-06-22 22:51:03 +02:00
TEST_CASE ( varScope24 ) ; // pointer / reference
2018-07-01 22:31:45 +02:00
TEST_CASE ( varScope25 ) ; // time_t
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 ( passedByValue ) ;
2016-07-28 13:19:24 +02:00
TEST_CASE ( passedByValue_nonConst ) ;
2010-04-02 02:19:38 +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-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 ) ;
2013-07-22 07:25:53 +02:00
TEST_CASE ( testMisusedScopeObjectInConstructor ) ;
2013-10-22 19:37:11 +02:00
TEST_CASE ( testMisusedScopeObjectNoCodeAfter ) ;
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 ) ;
2011-07-17 04:06:23 +02:00
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-04-09 23:05:27 +02:00
TEST_CASE ( duplicateBranch ) ;
2016-11-04 15:01:05 +01: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: Fragment 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
2018-08-07 09:32:16 +02:00
TEST_CASE ( duplicateExpression7 ) ;
TEST_CASE ( duplicateExpression8 ) ;
TEST_CASE ( duplicateExpressionLoop ) ;
2018-04-03 21:43:55 +02:00
TEST_CASE ( duplicateValueTernary ) ;
2015-01-03 18:01:49 +01:00
TEST_CASE ( duplicateExpressionTernary ) ; // #6391
2018-01-11 09:41:22 +01:00
TEST_CASE ( duplicateExpressionTemplate ) ; // #6930
2018-04-21 11:28:21 +02:00
TEST_CASE ( oppositeExpression ) ;
2018-04-08 14:43:19 +02:00
TEST_CASE ( duplicateVarExpression ) ;
2018-05-13 20:20:55 +02:00
TEST_CASE ( duplicateVarExpressionUnique ) ;
2018-06-10 00:05:23 +02:00
TEST_CASE ( duplicateVarExpressionAssign ) ;
2018-06-14 22:39:26 +02:00
TEST_CASE ( duplicateVarExpressionCrash ) ;
2018-09-06 06:55:36 +02:00
TEST_CASE ( multiConditionSameExpression ) ;
2011-04-26 07:45:27 +02:00
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-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 ) ;
2014-09-10 17:02:18 +02:00
2012-08-24 11:28:50 +02:00
TEST_CASE ( incompleteArrayFill ) ;
2012-09-02 13:09:32 +02:00
TEST_CASE ( redundantVarAssignment ) ;
2015-11-15 19:03:18 +01:00
TEST_CASE ( redundantVarAssignment_7133 ) ;
2017-03-25 20:46:25 +01:00
TEST_CASE ( redundantVarAssignment_stackoverflow ) ;
2012-09-02 13:09:32 +02:00
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
2013-06-15 17:49:10 +02:00
TEST_CASE ( checkCommaSeparatedReturn ) ;
2018-07-22 15:01:18 +02:00
TEST_CASE ( checkPassByReference ) ;
2013-09-26 07:07:48 +02:00
TEST_CASE ( checkComparisonFunctionIsAlwaysTrueOrFalse ) ;
2014-06-05 16:41:10 +02:00
2014-09-27 21:31:04 +02:00
TEST_CASE ( integerOverflow ) ; // #5895
2015-01-17 14:33:14 +01:00
TEST_CASE ( redundantPointerOp ) ;
2015-05-25 08:20:14 +02:00
TEST_CASE ( test_isSameExpression ) ;
2015-08-05 11:20:28 +02:00
TEST_CASE ( raceAfterInterlockedDecrement ) ;
2015-08-30 16:45:47 +02:00
TEST_CASE ( testUnusedLabel ) ;
2015-12-24 09:13:20 +01:00
TEST_CASE ( testEvaluationOrder ) ;
2015-12-28 14:04:14 +01:00
TEST_CASE ( testEvaluationOrderSelfAssignment ) ;
TEST_CASE ( testEvaluationOrderMacro ) ;
TEST_CASE ( testEvaluationOrderSequencePointsFunctionCall ) ;
TEST_CASE ( testEvaluationOrderSequencePointsComma ) ;
TEST_CASE ( testEvaluationOrderSizeof ) ;
2016-09-19 06:13:09 +02:00
TEST_CASE ( testUnsignedLessThanZero ) ;
2016-11-20 15:14:49 +01:00
TEST_CASE ( doubleMove1 ) ;
TEST_CASE ( doubleMoveMemberInitialization1 ) ;
TEST_CASE ( doubleMoveMemberInitialization2 ) ;
TEST_CASE ( moveAndAssign1 ) ;
TEST_CASE ( moveAndAssign2 ) ;
TEST_CASE ( moveAssignMoveAssign ) ;
2016-12-11 22:32:57 +01:00
TEST_CASE ( moveAndReset1 ) ;
TEST_CASE ( moveAndReset2 ) ;
TEST_CASE ( moveResetMoveReset ) ;
2016-11-20 15:14:49 +01:00
TEST_CASE ( moveAndFunctionParameter ) ;
TEST_CASE ( moveAndFunctionParameterReference ) ;
TEST_CASE ( moveAndFunctionParameterConstReference ) ;
TEST_CASE ( moveAndFunctionParameterUnknown ) ;
TEST_CASE ( moveAndReturn ) ;
TEST_CASE ( moveAndClear ) ;
TEST_CASE ( movedPointer ) ;
TEST_CASE ( partiallyMoved ) ;
TEST_CASE ( moveAndLambda ) ;
TEST_CASE ( forwardAndUsed ) ;
2017-01-05 08:52:11 +01:00
TEST_CASE ( funcArgNamesDifferent ) ;
TEST_CASE ( funcArgOrderDifferent ) ;
2017-01-23 12:50:36 +01:00
TEST_CASE ( cpp11FunctionArgInit ) ; // #7846 - "void foo(int declaration = {}) {"
2008-12-19 22:15:18 +01:00
}
2015-07-26 22:21:26 +02:00
void check ( const char code [ ] , const char * filename = nullptr , bool experimental = false , bool inconclusive = true , bool runSimpleChecks = true , Settings * settings = 0 ) {
2010-12-01 18:00:55 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
2013-08-06 11:11:59 +02:00
if ( ! settings ) {
settings = & _settings ;
}
settings - > addEnabled ( " style " ) ;
settings - > addEnabled ( " warning " ) ;
settings - > addEnabled ( " portability " ) ;
settings - > addEnabled ( " performance " ) ;
2016-11-20 15:14:49 +01:00
settings - > standards . c = Standards : : CLatest ;
settings - > standards . cpp = Standards : : CPPLatest ;
2013-08-06 11:11:59 +02:00
settings - > inconclusive = inconclusive ;
settings - > experimental = experimental ;
2010-12-01 18:00:55 +01:00
2008-12-19 22:15:18 +01:00
// Tokenize..
2013-08-06 11:11:59 +02: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..
2013-08-06 11:11:59 +02:00
CheckOther checkOther ( & tokenizer , settings , this ) ;
checkOther . runChecks ( & tokenizer , settings , this ) ;
2010-05-15 14:06:45 +02:00
2013-04-13 20:17:53 +02:00
if ( runSimpleChecks ) {
2013-12-30 17:45:28 +01:00
tokenizer . simplifyTokenList2 ( ) ;
2013-08-06 11:11:59 +02:00
checkOther . runSimplifiedChecks ( & tokenizer , settings , this ) ;
2013-04-13 20:17:53 +02:00
}
2008-12-19 22:15:18 +01:00
}
2017-06-06 08:59:18 +02:00
void check ( const char code [ ] , Settings * s ) {
check ( code , " test.cpp " , false , true , true , s ) ;
}
2017-05-27 04:16:46 +02:00
void checkP ( const char code [ ] , const char * filename = " test.cpp " ) {
2017-05-18 21:52:31 +02:00
// Clear the error buffer..
errout . str ( " " ) ;
2017-05-27 04:16:46 +02:00
Settings * settings = & _settings ;
2017-05-18 21:52:31 +02:00
settings - > addEnabled ( " style " ) ;
settings - > addEnabled ( " warning " ) ;
settings - > addEnabled ( " portability " ) ;
settings - > addEnabled ( " performance " ) ;
settings - > standards . c = Standards : : CLatest ;
settings - > standards . cpp = Standards : : CPPLatest ;
settings - > inconclusive = true ;
settings - > experimental = false ;
// Raw tokens..
2018-04-09 09:41:24 +02:00
std : : vector < std : : string > files ( 1 , filename ) ;
2017-05-18 21:52:31 +02:00
std : : istringstream istr ( code ) ;
const simplecpp : : TokenList tokens1 ( istr , files , files [ 0 ] ) ;
// Preprocess..
simplecpp : : TokenList tokens2 ( files ) ;
std : : map < std : : string , simplecpp : : TokenList * > filedata ;
simplecpp : : preprocess ( tokens2 , tokens1 , files , filedata , simplecpp : : DUI ( ) ) ;
// Tokenizer..
Tokenizer tokenizer ( settings , this ) ;
tokenizer . createTokens ( & tokens2 ) ;
tokenizer . simplifyTokens1 ( " " ) ;
// Check..
CheckOther checkOther ( & tokenizer , settings , this ) ;
checkOther . runChecks ( & tokenizer , settings , this ) ;
tokenizer . simplifyTokenList2 ( ) ;
checkOther . runSimplifiedChecks ( & tokenizer , settings , this ) ;
}
2015-02-16 16:25:27 +01:00
void checkposix ( const char code [ ] ) {
2015-10-08 11:35:51 +02:00
static Settings settings ;
2015-02-16 16:25:27 +01:00
settings . addEnabled ( " warning " ) ;
settings . standards . posix = true ;
check ( code ,
nullptr , // filename
false , // experimental
false , // inconclusive
true , // runSimpleChecks
& settings ) ;
}
2015-08-05 11:20:28 +02:00
void checkInterlockedDecrement ( const char code [ ] ) {
2015-10-08 11:35:51 +02:00
static Settings settings ;
2015-08-05 11:20:28 +02:00
settings . platformType = Settings : : Win32A ;
check ( code , nullptr , false , false , true , & settings ) ;
}
2014-11-20 14:20:09 +01:00
void emptyBrackets ( ) {
2012-12-28 20:16:51 +01:00
check ( " { \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-07-28 07:28:24 +02:00
2015-03-03 15:50:31 +01:00
void zeroDiv1 ( ) { // floating point division by zero => no error
2013-04-13 20:17:53 +02:00
check ( " void foo() { \n "
" cout << 1. / 0; \n "
2009-03-28 07:49:47 +01:00
" } " ) ;
2015-03-03 15:50:31 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-07-26 22:08:36 +02:00
check ( " void foo() { \n "
" cout << 42 / (double)0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" cout << 42 / (float)0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-12-11 21:24:12 +01:00
check ( " void foo() { \n "
" cout << 42 / (int)0; \n "
" } " ) ;
2016-12-20 19:32:21 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n " , errout . str ( ) ) ;
2009-03-28 07:49:47 +01:00
}
2014-11-20 14:20:09 +01: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
2016-02-02 17:17:33 +01:00
void zeroDiv3 ( ) {
check ( " int foo(int i) { \n "
" return i / 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n " , errout . str ( ) ) ;
check ( " int foo(int i) { \n "
" return i % 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n " , errout . str ( ) ) ;
check ( " void foo(int& i) { \n "
" i /= 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n " , errout . str ( ) ) ;
check ( " void foo(int& i) { \n "
" i %= 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n " , errout . str ( ) ) ;
2016-05-04 15:39:56 +02:00
check ( " uint8_t foo(uint8_t i) { \n "
" return i / 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n " , errout . str ( ) ) ;
2016-02-02 17:17:33 +01:00
}
2014-11-20 14:20:09 +01:00
void zeroDiv4 ( ) {
2009-08-30 21:02:09 +02:00
check ( " void f() \n "
" { \n "
2013-09-22 09:52:12 +02:00
" long a = b / 0x6; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2013-09-22 09:52:12 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-08-30 21:02:09 +02:00
check ( " void f() \n "
" { \n "
2013-09-22 09:52:12 +02:00
" long a = b / 0x0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2015-08-01 18:42:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(long b) \n "
" { \n "
" long a = b / 0x0; \n "
" } " ) ;
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 "
2013-09-22 09:52:12 +02:00
" long a = b / 0L; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2015-08-01 18:42:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(long b) \n "
" { \n "
" long a = b / 0L; \n "
" } " ) ;
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 "
2013-09-22 09:52:12 +02:00
" long a = b / 0ul; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2015-08-01 18:42:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(long b) \n "
" { \n "
" long a = b / 0ul; \n "
" } " ) ;
2013-09-22 09:52:12 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Division by zero. \n " , 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-08-30 21:02:09 +02:00
}
2014-11-20 14:20:09 +01: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
" } } } " ) ;
2015-08-01 18:42:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(long b) \n "
" { { { \n "
" long a = b / 0; \n "
" } } } " ) ;
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
}
2014-11-20 14:20:09 +01: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
" } } } " ) ;
2015-08-01 18:42:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int b) \n "
" { { { \n "
" int a = b % 0; \n "
" } } } " ) ;
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
}
2013-07-28 13:17:46 +02:00
2014-11-20 14:20:09 +01:00
void zeroDiv7 ( ) {
2015-08-01 18:42:17 +02:00
// unknown types for x and y --> do not warn
2013-07-28 12:32:18 +02:00
check ( " void f() { \n "
2013-11-07 14:38:08 +01:00
" int a = x/2*3/0; \n "
" int b = y/2*3%0; \n "
2013-07-28 12:32:18 +02:00
" } " ) ;
2015-08-01 18:42:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x, int y) { \n "
" int a = x/2*3/0; \n "
" int b = y/2*3%0; \n "
" } " ) ;
2013-07-28 12:32:18 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n "
" [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
}
2011-09-10 16:12:53 +02:00
2014-11-20 14:20:09 +01:00
void zeroDiv8 ( ) {
2014-03-20 16:12:58 +01:00
// #5584 - FP when function is unknown
check ( " void f() { \n "
" int a = 0; \n "
" do_something(a); \n "
" return 4 / a; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error, inconclusive) Division by zero. \n " , errout . str ( ) ) ;
}
2015-07-26 06:28:23 +02:00
void zeroDiv9 ( ) {
// #6403 FP zerodiv - inside protecting if-clause
check ( " void foo() { \n "
" double fStepHelp = 0; \n "
" if( (rOuterValue >>= fStepHelp) ) { \n "
" if( fStepHelp != 0.0) { \n "
2018-08-07 09:32:16 +02:00
" double fStepMain = 1; \n "
2015-07-26 06:28:23 +02:00
" sal_Int32 nIntervalCount = static_cast< sal_Int32 >(fStepMain / fStepHelp); \n "
" } \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-08-01 17:28:54 +02:00
void zeroDiv10 ( ) {
// #5402 false positive: (error) Division by zero -- with boost::format
check ( " int main() { \n "
" std::cout \n "
" << boost::format( \" %d :: %s <> %s \" ) % 0 % \" a \" % \" b \" \n "
" << std::endl; \n "
" return 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-08-02 14:05:09 +02:00
void zeroDiv11 ( ) {
2015-08-02 18:56:26 +02:00
check ( " void f(int a) { \n "
" int res = (a+2)/0; \n "
" int res = (a*2)/0; \n "
2015-08-02 14:05:09 +02:00
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Division by zero. \n "
" [test.cpp:3]: (error) Division by zero. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" int res = (a+2)/0; \n "
" int res = (a*2)/0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void zeroDivCond ( ) {
2013-09-07 07:40:10 +02:00
check ( " void f(unsigned int x) { \n "
" int y = 17 / x; \n "
" if (x > 0) {} \n "
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'x>0' is redundant or there is division by zero at line 2. \n " , errout . str ( ) ) ;
2013-09-07 07:40:10 +02:00
check ( " void f(unsigned int x) { \n "
" int y = 17 / x; \n "
" if (x >= 1) {} \n "
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'x>=1' is redundant or there is division by zero at line 2. \n " , errout . str ( ) ) ;
2013-09-07 07:40:10 +02:00
check ( " void f(int x) { \n "
" int y = 17 / x; \n "
" if (x == 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'x==0' is redundant or there is division by zero at line 2. \n " , errout . str ( ) ) ;
2013-09-07 07:40:10 +02:00
check ( " void f(unsigned int x) { \n "
" int y = 17 / x; \n "
" if (x != 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'x!=0' is redundant or there is division by zero at line 2. \n " , errout . str ( ) ) ;
2013-09-07 07:40:10 +02:00
2013-09-15 17:46:52 +02:00
// function call
check ( " void f1(int x, int y) { c=x/y; } \n "
" void f2(unsigned int y) { \n "
" f1(123,y); \n "
" if (y>0){} \n "
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:1]: (warning) Either the condition 'y>0' is redundant or there is division by zero at line 1. \n " , errout . str ( ) ) ;
2013-09-15 17:46:52 +02:00
2013-09-07 07:40:10 +02:00
// avoid false positives when variable is changed after division
check ( " void f() { \n "
" unsigned int x = do_something(); \n "
" int y = 17 / x; \n "
" x = some+calculation; \n "
" if (x != 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-09-07 07:40:10 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
{
// function is called that might modify global variable
check ( " void do_something(); \n "
" int x; \n "
" void f() { \n "
" int y = 17 / x; \n "
" do_something(); \n "
" if (x != 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-09-07 07:40:10 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// function is called. but don't care, variable is local
check ( " void do_something(); \n "
" void f() { \n "
" int x = some + calculation; \n "
" int y = 17 / x; \n "
" do_something(); \n "
" if (x != 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'x!=0' is redundant or there is division by zero at line 4. \n " , errout . str ( ) ) ;
2013-09-07 07:40:10 +02:00
}
2013-09-07 16:18:11 +02:00
check ( " void do_something(int value); \n "
" void f(int x) { \n "
" int y = 17 / x; \n "
" do_something(x); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-09-07 07:40:10 +02:00
check ( " int x; \n "
" void f() { \n "
" int y = 17 / x; \n "
" while (y || x == 0) { x--; } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-09-07 07:40:10 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-09-21 17:44:25 +02:00
// ticket 5033 segmentation fault (valid code) in CheckOther::checkZeroDivisionOrUselessCondition
check ( " void f() { \n "
" double* p1= new double[1]; \n "
" double* p2= new double[1]; \n "
" double* p3= new double[1]; \n "
" double* pp[3] = {p1,p2,p3}; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-10 22:23:52 +02:00
2013-10-22 16:18:42 +02:00
// #5105 - FP
check ( " int f(int a, int b) { \n "
" int r = a / b; \n "
" if (func(b)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-30 16:51:00 +01:00
2015-08-01 18:42:17 +02:00
// Unknown types for b and c --> do not warn
2013-10-30 16:51:00 +01:00
check ( " int f(int d) { \n "
" int r = (a?b:c) / d; \n "
" if (d == 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2015-08-01 18:42:17 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-30 16:51:00 +01:00
check ( " int f(int a) { \n "
" int r = a ? 1 / a : 0; \n "
" if (a == 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-10-30 16:51:00 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f(int a) { \n "
" int r = (a == 0) ? 0 : 1 / a; \n "
" if (a == 0) {} \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-10-30 16:51:00 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-09-07 07:40:10 +02:00
}
2014-11-20 14:20:09 +01:00
void nanInArithmeticExpression ( ) {
2013-06-10 08:13:08 +02:00
check ( " void f() \n "
" { \n "
" double x = 3.0 / 0.0 + 1.0 \n "
2017-05-18 21:52:31 +02:00
" printf( \" %f \" , x); \n "
2013-06-10 08:13:08 +02:00
" } " ) ;
ASSERT_EQUALS (
" [test.cpp:3]: (style) Using NaN/Inf in a computation. \n " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" double x = 3.0 / 0.0 - 1.0 \n "
2017-05-18 21:52:31 +02:00
" printf( \" %f \" , x); \n "
2013-06-10 08:13:08 +02:00
" } " ) ;
ASSERT_EQUALS (
" [test.cpp:3]: (style) Using NaN/Inf in a computation. \n " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" double x = 1.0 + 3.0 / 0.0 \n "
2017-05-18 21:52:31 +02:00
" printf( \" %f \" , x); \n "
2013-06-10 08:13:08 +02:00
" } " ) ;
ASSERT_EQUALS (
" [test.cpp:3]: (style) Using NaN/Inf in a computation. \n " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" double x = 1.0 - 3.0 / 0.0 \n "
2017-05-18 21:52:31 +02:00
" printf( \" %f \" , x); \n "
2013-06-10 08:13:08 +02:00
" } " ) ;
ASSERT_EQUALS (
" [test.cpp:3]: (style) Using NaN/Inf in a computation. \n " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" double x = 3.0 / 0.0 \n "
2017-05-18 21:52:31 +02:00
" printf( \" %f \" , x); \n "
2013-06-10 08:13:08 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2009-01-08 22:08:14 +01:00
2014-11-20 14:20:09 +01:00
void varScope1 ( ) {
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
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
2014-11-20 14:20:09 +01:00
void varScope2 ( ) {
2015-10-08 11:35:51 +02:00
check ( " int foo() \n "
" { \n "
" Error e; \n "
" e.SetValue(12); \n "
" throw e; \n "
" } " ) ;
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
2014-11-20 14:20:09 +01:00
void varScope3 ( ) {
2015-10-08 11:35:51 +02:00
check ( " void foo() \n "
" { \n "
" int i; \n "
" int *p = 0; \n "
" if (abc) \n "
" { \n "
" p = &i; \n "
" } \n "
" *p = 1; \n "
" } " ) ;
2009-06-05 02:39:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-05-27 19:38:26 +02:00
}
2014-11-20 14:20:09 +01:00
void varScope4 ( ) {
2015-10-08 11:35:51 +02:00
check ( " void foo() \n "
" { \n "
" int i; \n "
" } " ) ;
2009-06-12 16:17:51 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope5 ( ) {
2015-10-08 11:35:51 +02:00
check ( " void f(int x) \n "
" { \n "
" int i = 0; \n "
" if (x) { \n "
" for ( ; i < 10; ++i) ; \n "
" } \n "
" } " ) ;
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
2015-10-08 11:35:51 +02:00
check ( " void f(int x) { \n "
" const unsigned char i = 0; \n "
" if (x) { \n "
" for ( ; i < 10; ++i) ; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) \n "
" { \n "
" int i = 0; \n "
" if (x) {b()} \n "
" else { \n "
" for ( ; i < 10; ++i) ; \n "
" } \n "
" } " ) ;
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
2014-11-20 14:20:09 +01:00
void varScope6 ( ) {
2015-10-08 11:35:51 +02:00
check ( " void f(int x) \n "
" { \n "
" int i = x; \n "
" if (a) { \n "
" x++; \n "
" } \n "
" if (b) { \n "
" c(i); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-12-26 14:14:40 +01:00
check ( " void f() { \n " // #5398
" bool success = false; \n "
" int notReducable(someClass.getX(&success)); \n "
" if (success) { \n "
" foo(notReducable); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
2017-03-03 19:07:00 +01:00
check ( " void foo(Test &test) { \n "
" int& x = test.getData(); \n "
" if (test.process()) \n "
" x = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int &x) \n "
" { \n "
" int n = 1; \n "
" do \n "
" { \n "
" ++n; \n "
" ++x; \n "
" } while (x); \n "
" } " ) ;
2009-09-29 23:56:43 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2009-03-27 17:19:34 +01:00
2014-11-20 14:20:09 +01:00
void varScope7 ( ) {
2015-10-08 11:35:51 +02:00
check ( " void f(int x) \n "
" { \n "
" int y = 0; \n "
" b(y); \n "
" if (x) { \n "
" y++; \n "
" } \n "
" } " ) ;
2009-10-06 17:45:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope8 ( ) {
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
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
}
2014-11-20 14:20:09 +01:00
void varScope9 ( ) {
2010-04-09 16:53:27 +02:00
// classes may have extra side effects
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2010-04-09 16:53:27 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope10 ( ) {
2015-10-08 11:35:51 +02:00
check ( " int f() \n "
" { \n "
" int x = 0; \n "
" FOR { \n "
" foo(x++); \n "
" } \n "
" } " ) ;
2010-08-26 21:57:48 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope11 ( ) {
2015-10-08 11:35:51 +02:00
check ( " int f() { \n "
" int x = 0; \n "
" AB ab = { x, 0 }; \n "
" } " ) ;
2011-01-17 20:51:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " int f() { \n "
" int x = 0; \n "
" if (a == 0) { ++x; } \n "
" AB ab = { x, 0 }; \n "
" } " ) ;
2011-01-17 20:51:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " int f() { \n "
" int x = 0; \n "
" if (a == 0) { ++x; } \n "
" if (a == 1) { AB ab = { x, 0 }; } \n "
" } " ) ;
2011-01-17 20:51:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope12 ( ) {
2015-10-08 11:35:51 +02:00
check ( " 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
2015-10-08 11:35:51 +02:00
check ( " void f(int x) { \n "
" int i[5]; \n "
" int* j; \n "
" if (x) \n "
" j = i; \n "
" foo(j); \n "
" } " ) ;
2012-05-24 15:34:59 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " void f(int x) { \n "
" const bool b = true; \n "
" x++; \n "
" if (x == 5) \n "
" foo(b); \n "
" } " ) ;
2013-10-19 17:27:02 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-24 15:34:59 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(int x) { \n "
" const bool b = x; \n "
" x++; \n "
" if (x == 5) \n "
" foo(b); \n "
" } " ) ;
2012-05-24 15:34:59 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope13 ( ) {
2011-05-06 21:16:01 +02:00
// #2770
2015-10-08 11:35:51 +02:00
check ( " void f() { \n "
" int i = 0; \n "
" forever { \n "
" if (i++ == 42) { break; } \n "
" } \n "
" } " ) ;
2011-05-06 21:16:01 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-01-21 18:50:56 +01:00
2014-11-20 14:20:09 +01:00
void varScope14 ( ) {
2012-07-29 15:39:43 +02:00
// #3941
2015-10-08 11:35:51 +02:00
check ( " void f() { \n "
" const int i( foo()); \n "
" if(a) { \n "
" for ( ; i < 10; ++i) ; \n "
" } \n "
" } " ) ;
2012-07-29 15:39:43 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope15 ( ) {
2013-02-12 21:16:57 +01:00
// #4573
2015-10-08 11:35:51 +02:00
check ( " void f() { \n "
" int a,b,c; \n "
" if (a); \n "
" else if(b); \n "
" else if(c); \n "
" else; \n "
" } " , nullptr , false , false ) ;
2013-02-12 21:16:57 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope16 ( ) {
2015-10-08 11:35:51 +02:00
check ( " void f() { \n "
" int a = 0; \n "
" while((++a) < 56) { \n "
" foo(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int a = 0; \n "
" do { \n "
" foo(); \n "
" } while((++a) < 56); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int a = 0; \n "
" do { \n "
" a = 64; \n "
" foo(a); \n "
" } while((++a) < 56); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int a = 0; \n "
" do { \n "
" a = 64; \n "
" foo(a); \n "
" } while(z()); \n "
" } " ) ;
2013-03-03 19:35:33 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'a' can be reduced. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope17 ( ) {
2015-10-08 11:35:51 +02:00
check ( " void f() { \n "
" int x; \n "
" if (a) { \n "
" x = stuff(x); \n "
" morestuff(x); \n "
" } \n "
" } " ) ;
2013-03-15 13:00:51 +01:00
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
2015-10-08 11:35:51 +02:00
check ( " void f() { \n "
" int x; \n "
" if (a) { \n "
" x = stuff(x); \n "
" morestuff(x); \n "
" } \n "
" if (b) {} \n "
" } " ) ;
2013-03-18 16:17:53 +01:00
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
}
2014-11-20 14:20:09 +01:00
void varScope18 ( ) {
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2013-03-23 11:28:33 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'x' can be reduced. \n " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2013-03-23 11:28:33 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " 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 ( ) ) ;
check ( " 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 "
" } " ) ;
2013-08-31 14:22:06 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope20 ( ) { // Ticket #5103 - constant variable only used in inner scope
2015-10-08 11:35:51 +02:00
check ( " int f(int a) { \n "
" const int x = 234; \n "
" int b = a; \n "
" if (b > 32) b = x; \n "
" return b; \n "
" } " ) ;
2013-10-19 17:27:02 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope21 ( ) { // Ticket #5382 - initializing two-dimensional array
2015-10-08 11:35:51 +02:00
check ( " int test() { \n "
" int test_value = 3; \n "
" int test_array[1][1] = { { test_value } }; \n "
" return sizeof(test_array); \n "
" } " ) ;
2014-03-17 12:34:39 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope22 ( ) { // Ticket #5684 - "The scope of the variable 'p' can be reduced" - But it can not.
2015-10-08 11:35:51 +02:00
check ( " void foo() { \n "
" int* p( 42 ); \n "
" int i = 0; \n "
" while ( i != 100 ) { \n "
" *p = i; \n "
" ++p; \n "
" ++i; \n "
" } \n "
" } " ) ;
2014-04-17 21:32:56 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// try to avoid an obvious false negative after applying the fix for the example above:
2015-10-08 11:35:51 +02:00
check ( " void foo() { \n "
" int* p( 42 ); \n "
" int i = 0; \n "
" int dummy = 0; \n "
" while ( i != 100 ) { \n "
" p = & dummy; \n "
" *p = i; \n "
" ++p; \n "
" ++i; \n "
" } \n "
" } " ) ;
2014-04-17 21:32:56 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'p' can be reduced. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void varScope23 ( ) { // #6154: Don't suggest to reduce scope if inner scope is a lambda
2015-10-08 11:35:51 +02:00
check ( " int main() { \n "
" size_t myCounter = 0; \n "
" Test myTest([&](size_t aX){ \n "
" std::cout << myCounter += aX << std::endl; \n "
" }); \n "
" } " ) ;
2014-09-10 20:56:34 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-06-22 22:51:03 +02:00
void varScope24 ( ) {
check ( " void f(Foo x) { \n "
" Foo &r = x; \n "
" if (cond) { \n "
" r.dostuff(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) The scope of the variable 'r' can be reduced. \n " , errout . str ( ) ) ;
check ( " void f(Foo x) { \n "
" Foo foo = x; \n "
" if (cond) { \n "
" foo.dostuff(); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-07-01 22:31:45 +02:00
void varScope25 ( ) {
check ( " void f() { \n "
" time_t currtime; \n "
" if (a) { \n "
" currtime = time(&dummy); \n "
" if (currtime > t) {} \n "
" } \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:2]: (style) The scope of the variable 'currtime' can be reduced. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void checkOldStylePointerCast ( const char code [ ] ) {
2010-12-01 18:00:55 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
2015-10-08 11:35:51 +02:00
static Settings settings ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2014-04-29 06:21:30 +02:00
settings . standards . cpp = Standards : : CPP03 ; // #5560
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 " ) ;
CheckOther checkOtherCpp ( & tokenizerCpp , & settings , this ) ;
checkOtherCpp . warningOldStylePointerCast ( ) ;
2009-06-18 22:26:21 +02:00
}
2014-11-20 14:20:09 +01: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
2014-09-01 14:40:28 +02:00
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (const Base * const) derived; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (volatile Base *) derived; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (volatile Base * const) derived; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (const volatile Base *) derived; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
checkOldStylePointerCast ( " class Base; \n "
" void foo() \n "
" { \n "
" Base * b = (const volatile Base * const) derived; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
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 ( ) ) ;
2014-04-29 06:21:30 +02:00
2014-08-04 11:20:48 +02:00
// #3630
checkOldStylePointerCast ( " class SomeType; \n "
" class X : public Base { \n "
" X() : Base((SomeType*)7) {} \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
checkOldStylePointerCast ( " class SomeType; \n "
" class X : public Base { \n "
" X() : Base((SomeType*)var) {} \n "
" }; " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
2014-08-04 11:42:14 +02:00
checkOldStylePointerCast ( " class SomeType; \n "
" class X : public Base { \n "
" X() : Base((SomeType*)0) {} \n "
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-04-29 06:21:30 +02:00
// #5560
checkOldStylePointerCast ( " class C; \n "
" \n "
" class B \n "
" { virtual G* createGui(S*, C*) const = 0; }; \n "
" \n "
" class MS : public M \n "
" { virtual void addController(C*) override {} }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-12 21:56:13 +02:00
// #6164
checkOldStylePointerCast ( " class Base {}; \n "
" class Derived: public Base {}; \n "
" void testCC() { \n "
" std::vector<Base*> v; \n "
" v.push_back((Base*)new Derived); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (style) C-style pointer casting \n " , errout . str ( ) ) ;
2009-06-18 22:26:21 +02:00
}
2014-11-20 14:20:09 +01:00
void checkInvalidPointerCast ( const char code [ ] , bool portability = true , 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 ( ) ;
}
2014-11-20 14:20:09 +01:00
void invalidPointerCast ( ) {
2012-02-26 11:56:32 +01:00
checkInvalidPointerCast ( " void test() { \n "
" float *f = new float[10]; \n "
" delete [] (double*)f; \n "
" delete [] (long double const*)(new float[10]); \n "
" } " ) ;
2016-02-05 19:51:45 +01:00
TODO_ASSERT_EQUALS ( " [test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation. \n "
" [test.cpp:4]: (portability) Casting between float* and const long double* which have an incompatible binary data representation. \n " ,
" [test.cpp:3]: (portability) 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 "
" } " ) ;
2016-02-05 19:51:45 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability) Casting between const 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 "
" } " ) ;
2014-09-06 13:09:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability) Casting between double* and long double* which have an incompatible binary data representation. \n "
" [test.cpp:3]: (portability) 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 "
" } " ) ;
2014-09-06 13:09:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability) Casting between integer* and long double* which have an incompatible binary data representation. \n "
" [test.cpp:3]: (portability) Casting between integer* and double* which have an incompatible binary data representation. \n "
" [test.cpp:4]: (portability) 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 "
" } " ) ;
2014-09-06 13:09:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability) 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 "
" } " ) ;
2014-09-06 13:09:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (portability) 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
}
2009-10-04 10:05:58 +02:00
2014-11-20 14:20:09 +01:00
void passedByValue ( ) {
2015-10-08 11:35:51 +02:00
check ( " void f(const std::string str) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2017-07-25 11:17:56 +02:00
check ( " void f(std::unique_ptr<std::string> ptr) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-06-07 22:39:21 +02:00
check ( " void f(const std::shared_ptr<std::string> ptr) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-06-08 08:40:14 +02:00
check ( " void f(const std::function<F> ptr) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-06-08 10:19:02 +02:00
{
check ( " void f(const std::pair<int,int> x) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(const std::pair<std::string,std::string> x) {} " ) ;
TODO_ASSERT_EQUALS ( " error " , " " , errout . str ( ) ) ;
}
2015-10-08 11:35:51 +02:00
check ( " void f(const std::string::size_type x) {} " ) ;
2014-07-18 15:51:59 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class Foo; \n void f(const Foo foo) {} " ) ; // Unknown class
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance, inconclusive) Function parameter 'foo' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class Foo { std::vector<int> v; }; \n void f(const Foo foo) {} " ) ; // Large class (STL member)
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class Foo { int i; }; \n void f(const Foo foo) {} " ) ; // Small class
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Foo { int i[6]; }; \n void f(const Foo foo) {} " ) ; // Large class (array)
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class Foo { std::string* s; }; \n void f(const Foo foo) {} " ) ; // Small class (pointer)
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class Foo { static std::string s; }; \n void f(const Foo foo) {} " ) ; // Small class (static member)
2016-07-28 20:59:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class X { std::string s; }; class Foo : X { }; \n void f(const Foo foo) {} " ) ; // Large class (inherited)
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class X { std::string s; }; class Foo { X x; }; \n void f(const Foo foo) {} " ) ; // Large class (inherited)
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::string &str) {} " ) ;
2009-10-06 23:04:54 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::vector<int> v) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::vector<std::string> v) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::vector<std::string>::size_type s) {} " ) ;
2012-07-23 10:41:20 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " void f(const std::vector<int> &v) {} " ) ;
2009-10-06 23:04:54 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::map<int,int> &v) {} " ) ;
2009-10-06 23:04:54 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::map<int,int> v) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::map<std::string,std::string> v) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::map<int,std::string> v) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-05 10:59:28 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::map<std::string,int> v) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference. \n " , errout . str ( ) ) ;
2011-04-19 20:07:54 +02:00
2015-10-08 11:35:51 +02:00
check ( " void f(const std::streamoff pos) {} " ) ;
2014-08-06 12:08:18 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-01-19 11:14:55 +01:00
check ( " void f(std::initializer_list<int> i) {} " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-08-06 12:06:36 +02:00
// #5824
2015-10-08 11:35:51 +02:00
check ( " void log(const std::string& file, int line, const std::string& function, const std::string str, ...) {} " ) ;
2011-04-19 20:07:54 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-08-31 19:58:03 +02:00
// #5534
2015-10-08 11:35:51 +02:00
check ( " struct float3 { }; \n "
" typedef float3 vec; \n "
" class Plane { \n "
" vec Refract(vec &vec) const; \n "
" bool IntersectLinePlane(const vec &planeNormal); \n "
" }; " ) ;
2014-08-31 19:58:03 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class X { \n "
" virtual void func(const std::string str) {} \n "
" }; " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2009-10-04 10:05:58 +02:00
}
2016-07-28 13:19:24 +02:00
void passedByValue_nonConst ( ) {
check ( " void f(std::string str) {} " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " void f(std::string str) { \n "
" return str + x; \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " void f(std::string str) { \n "
" std::cout << str; \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " void f(std::string str) { \n "
" std::cin >> str; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-03-07 21:49:28 +01:00
check ( " void f(std::string str) { \n "
" std::string s2 = str; \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2017-03-07 21:49:28 +01:00
check ( " void f(std::string str) { \n "
" std::string& s2 = str; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(std::string str) { \n "
" const std::string& s2 = str; \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2017-03-07 21:49:28 +01:00
check ( " void f(std::string str) { \n "
" str = \" \" ; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " void f(std::string str) { \n "
" foo(str); \n " // It could be that foo takes str as non-const-reference
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(const std::string& str); \n "
" void f(std::string str) { \n "
" foo(str); \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2012-05-30 01:30:37 +02:00
2016-07-28 13:19:24 +02:00
check ( " void foo(std::string str); \n "
" void f(std::string str) { \n "
" foo(str); \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " void foo(std::string& str); \n "
" void f(std::string str) { \n "
" foo(str); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-06-23 23:25:06 +02:00
check ( " void foo(std::string* str); \n "
" void f(std::string str) { \n "
" foo(&str); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " void foo(int& i1, const std::string& str, int& i2); \n "
" void f(std::string str) { \n "
" foo((a+b)*c, str, x); \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " std::string f(std::string str) { \n "
" str += x; \n "
" return str; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class X { \n "
" std::string s; \n "
" void func() const; \n "
" }; \n "
" Y f(X x) { \n "
" x.func(); \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (performance) Function parameter 'x' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class X { \n "
" void func(); \n "
" }; \n "
" Y f(X x) { \n "
" x.func(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class X { \n "
" void func(std::string str) {} \n "
" }; " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference. \n " , errout . str ( ) ) ;
2016-07-28 13:19:24 +02:00
check ( " class X { \n "
" virtual void func(std::string str) {} \n " // Do not warn about virtual functions, if 'str' is not declared as const
" }; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " class X { \n "
2017-06-06 08:48:11 +02:00
" char a[1024]; \n "
2016-07-28 13:19:24 +02:00
" }; \n "
" class Y : X { \n "
2017-06-06 08:48:11 +02:00
" char b; \n "
2016-07-28 13:19:24 +02:00
" }; \n "
2017-06-06 08:48:11 +02:00
" void f(Y y) { \n "
2016-07-28 13:19:24 +02:00
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (performance) Function parameter 'y' should be passed by const reference. \n " , errout . str ( ) ) ;
2017-06-06 08:48:11 +02:00
check ( " class X { \n "
" void* a; \n "
" void* b; \n "
" }; \n "
" class Y { \n "
" void* a; \n "
" void* b; \n "
" char c; \n "
" }; \n "
" void f(X x, Y y) { \n "
" } " ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:10]: (performance) Function parameter 'y' should be passed by const reference. \n " , errout . str ( ) ) ;
2017-06-06 08:48:11 +02:00
2017-06-06 08:59:18 +02:00
{
2018-04-20 17:33:42 +02:00
// 8-byte data should be passed by const reference on 32-bit platform but not on 64-bit platform
2017-06-06 08:59:18 +02:00
const char code [ ] = " class X { \n "
" uint64_t a; \n "
" uint64_t b; \n "
" }; \n "
" void f(X x) {} " ;
Settings s32 ( _settings ) ;
s32 . platform ( cppcheck : : Platform : : Unix32 ) ;
check ( code , & s32 ) ;
2018-04-20 17:33:42 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (performance) Function parameter 'x' should be passed by const reference. \n " , errout . str ( ) ) ;
2017-06-06 08:59:18 +02:00
Settings s64 ( _settings ) ;
s64 . platform ( cppcheck : : Platform : : Unix64 ) ;
check ( code , & s64 ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2016-07-28 13:19:24 +02:00
}
void switchRedundantAssignmentTest ( ) {
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
2016-08-08 21:43:00 +02:00
check ( " void foo(int a) { \n "
" char str[10]; \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 "
2015-02-16 16:25:27 +01:00
" } " , 0 , 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
2016-08-08 21:43:00 +02:00
check ( " void foo(int a) { \n "
" char str[10]; \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
2016-08-08 21:43:00 +02:00
check ( " void foo(int a) { \n "
" char str[10]; \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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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
2016-08-08 21:43:00 +02:00
check ( " void foo(int a) { \n "
" char str[10]; \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 ( ) ) ;
2016-08-08 21:43:00 +02:00
check ( " void foo(int a) { \n "
" char str[10]; \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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2012-05-29 06:19:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-10 16:22:52 +01:00
// Ticket #5158 "segmentation fault (valid code)"
check ( " typedef struct ct_data_s { \n "
" union { \n "
" char freq; \n "
" } fc; \n "
" } ct_data; \n "
" typedef struct internal_state { \n "
" struct ct_data_s dyn_ltree[10]; \n "
" } deflate_state; \n "
" void f(deflate_state *s) { \n "
" s->dyn_ltree[0].fc.freq++; \n "
2015-02-16 16:25:27 +01:00
" } \n " , nullptr , false , false , false ) ;
2013-11-10 16:22:52 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-05 20:03:34 +02:00
// Ticket #6132 "crash: daca: kvirc CheckOther::checkRedundantAssignment()"
check ( " void HttpFileTransfer :: transferTerminated ( bool bSuccess@1 ) { \n "
" if ( m_szCompletionCallback . isNull ( ) ) { \n "
" KVS_TRIGGER_EVENT ( KviEvent_OnHTTPGetTerminated , out ? out : ( g_pApp . activeConsole ( ) ) , & vParams ) \n "
" } else { \n "
" KviKvsScript :: run ( m_szCompletionCallback , out ? out : ( g_pApp . activeConsole ( ) ) , & vParams ) ; \n "
" } \n "
2015-02-16 16:25:27 +01:00
" } \n " , nullptr , false , false , true ) ;
2014-09-05 20:03:34 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-05-09 15:25:36 +02:00
}
2012-11-15 07:48:45 +01:00
2014-11-20 14:20:09 +01:00
void switchRedundantOperationTest ( ) {
2013-05-09 15:25:36 +02:00
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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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
2014-11-20 14:20:09 +01:00
void switchRedundantBitwiseOperationTest ( ) {
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 "
" 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
2014-11-20 14:20:09 +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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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
2013-08-06 11:11:59 +02:00
Settings settings ;
settings . library . setnoreturn ( " exit " , true ) ;
2016-12-06 12:31:16 +01:00
settings . library . functions [ " exit " ] . argumentChecks [ 1 ] = Library : : ArgumentChecks ( ) ;
2013-08-06 11:11:59 +02:00
check ( " void foo() { \n "
" exit(0); \n "
" break; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false , & settings ) ;
2013-08-06 11:11:59 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary. \n " , errout . str ( ) ) ;
2014-03-22 18:56:47 +01:00
check ( " class NeonSession { \n "
" void exit(); \n "
" }; \n "
" void NeonSession::exit() \n "
" { \n "
" SAL_INFO( \" ucb.ucp.webdav \" , \" neon commands cannot be aborted \" ); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false , & settings ) ;
2014-03-22 18:56:47 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-03-22 19:55:13 +01:00
check ( " void NeonSession::exit() \n "
" { \n "
" SAL_INFO( \" ucb.ucp.webdav \" , \" neon commands cannot be aborted \" ); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false , & settings ) ;
2014-03-22 19:55:13 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-02-16 16:25:27 +01:00
check ( " void foo() { xResAccess->exit(); } " , nullptr , false , false , false , & settings ) ;
2014-03-22 22:35:20 +01:00
ASSERT_EQUALS ( " " , 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 "
" break; \n "
" case 1: \n "
" c++; \n "
" break; \n "
" } \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2011-12-03 11:43:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" throw 0; \n "
" return; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 ( ) ) ;
2014-12-22 11:15:22 +01:00
check ( " int foo(int unused) { \n "
" return 0; \n "
" (void)unused; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2014-12-22 11:15:22 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int foo(int unused1, int unused2) { \n "
" return 0; \n "
" (void)unused1; \n "
" (void)unused2; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2014-12-22 11:15:22 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int foo(int unused1, int unused2) { \n "
" return 0; \n "
" (void)unused1; \n "
" (void)unused2; \n "
" foo(); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2014-12-22 11:15:22 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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
2014-04-23 07:58:43 +02:00
// #5707
2015-02-24 06:11:31 +01:00
check ( " extern int i,j; \n "
2014-04-23 07:58:43 +02:00
" int foo() { \n "
" switch(i) { \n "
" default: j=1; break; \n "
" } \n "
" return 0; \n "
" j=2; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2014-04-23 07:58:43 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (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 "
" return 0; \n "
" label: \n "
" throw 0; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2015-08-30 16:45:47 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Label 'label' is not used. \n " , 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
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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , true , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2013-05-11 17:50:59 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-06-18 06:40:43 +02:00
// #4756
check ( " template <> \n "
" inline uint16_t htobe(uint16_t value) { \n "
" return ( __extension__ ({ \n "
" register unsigned short int __v, __x = (unsigned short int) (value); \n "
" if (__builtin_constant_p (__x)) \n "
" __v = ((unsigned short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff) << 8))); \n "
" else \n "
" __asm__ ( \" rorw $8, %w0 \" : \" =r \" (__v) : \" 0 \" (__x) : \" cc \" ); \n "
" __v; \n "
" })); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2013-06-18 06:40:43 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-01 11:10:42 +02:00
// #6008
check ( " static std::function< int ( int, int ) > GetFunctor() { \n "
" return [](int a_, int b_) -> int { \n "
" int sum = a_ + b_; \n "
" return sum; \n "
" }; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2014-09-01 11:10:42 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-01 11:29:39 +02:00
// #5789
check ( " struct per_state_info { \n "
" uint64_t enter, exit; \n "
" uint64_t events; \n "
" per_state_info() : enter(0), exit(0), events(0) {} \n "
2015-02-16 16:25:27 +01:00
" }; " , nullptr , false , false , false ) ;
2014-09-01 11:29:39 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-26 08:29:43 +01:00
// #6664
check ( " void foo() { \n "
" (beat < 100) ? (void)0 : exit(0); \n "
" bar(); \n "
" } " , nullptr , false , false , false , & settings ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" (beat < 100) ? exit(0) : (void)0; \n "
" bar(); \n "
" } " , nullptr , false , false , false , & settings ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-07-15 02:12:56 +02:00
}
2014-11-20 14:20:09 +01:00
void suspiciousCase ( ) {
2012-12-07 21:27:32 +01:00
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
}
2014-11-20 14:20:09 +01:00
void suspiciousEqualityComparison ( ) {
2013-01-18 08:03:04 +01:00
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 "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-01-18 08:03:04 +01:00
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 "
2017-05-18 21:52:31 +02:00
" printf( \" %i \" , ({x==0;})); \n "
2013-01-18 08:03:04 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int x) { \n "
2017-05-18 21:52:31 +02:00
" printf( \" %i \" , ({int x = do_something(); x == 0;})); \n "
2013-01-18 08:03:04 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int x) { \n "
2017-05-18 21:52:31 +02:00
" printf( \" %i \" , ({x == 0; x > 0 ? 10 : 20})); \n "
2013-01-18 08:03:04 +01:00
" } " ) ;
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 "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-01-19 01:34:15 +01:00
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
2014-11-20 14:20:09 +01:00
void selfAssignment ( ) {
2010-08-15 06:28:22 +02:00
check ( " void foo() \n "
" { \n "
2014-05-21 18:19:42 +02:00
" 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 "
2014-05-21 18:19:42 +02:00
" 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
2014-05-21 18:19:42 +02:00
check ( " struct A { int b; }; \n "
" void foo(A* a1, A* a2) { \n "
" a1->b = a1->b; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-04-30 14:22:18 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Redundant assignment of 'a1->b' 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 "
2014-05-21 18:19:42 +02:00
" int x = 1; \n "
" x = x + 1; \n "
" 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 "
2014-05-21 18:19:42 +02:00
" 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 "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-04-16 12:07:56 +02:00
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 "
" } " ) ;
2017-04-30 14:22:18 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Redundant assignment of 'this->var' to itself. \n " , errout . str ( ) ) ;
2014-05-21 18:19:42 +02:00
check ( " class Foo { \n "
" int var; \n "
" void func(int var); \n "
" }; \n "
" void Foo::func(int var) { \n "
" this->var = var; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-01-14 23:05:33 +01:00
2015-01-15 14:45:08 +01:00
// #6406 - designated initializer doing bogus self assignment
check ( " struct callbacks { \n "
" void (*something)(void); \n "
" }; \n "
" void something(void) {} \n "
" void f() { \n "
" struct callbacks ops = { .something = ops.something }; \n "
" } \n " ) ;
TODO_ASSERT_EQUALS ( " [test.cpp:6]: (warning) Redundant assignment of 'something' to itself. \n " , " " , errout . str ( ) ) ;
2015-07-13 16:39:21 +02:00
check ( " class V \n "
" { \n "
" public: \n "
" V() \n "
" { \n "
" x = y = z = 0.0; \n "
" } \n "
" V( double x, const double y, const double &z ) \n "
" { \n "
" x = x; y = y; z = z; \n "
" } \n "
" double x, y, z; \n "
" }; " ) ;
2015-07-13 21:31:24 +02:00
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Redundant assignment of 'x' to itself. \n "
" [test.cpp:10]: (warning) Redundant assignment of 'y' to itself. \n "
" [test.cpp:10]: (warning) Redundant assignment of 'z' to itself. \n " , errout . str ( ) ) ;
2015-07-13 16:39:21 +02:00
2018-07-15 11:30:02 +02:00
check ( " void f(int i) { i = !!i; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-08-15 06:28:22 +02:00
}
2014-11-20 14:20:09 +01: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 "
) ;
2015-06-20 11:22:15 +02:00
ASSERT_EQUALS ( " [test.cpp:15]: (style) Instance of 'Lock' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-10-01 17:23:22 +02:00
}
2014-11-20 14:20:09 +01:00
void trac3693 ( ) {
2012-04-12 14:02:09 +02:00
check ( " struct A{ \n "
" enum { \n "
" b = 300 \n "
" }; \n "
" }; \n "
2015-02-16 16:25:27 +01:00
" const int DFLT_TIMEOUT = A::b % 1000000 ; \n " , nullptr , false , false , false ) ;
2012-04-12 14:02:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01: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 ( ) ) ;
}
2014-11-20 14:20:09 +01: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 "
2013-10-22 19:37:11 +02:00
" do_something(); \n "
2011-03-20 09:55:26 +01:00
" } \n "
" }; \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01: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 "
) ;
2015-06-20 11:22:15 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) Instance of 'NotAFunction' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-10-01 17:23:22 +02:00
}
2014-11-20 14:20:09 +01: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 "
) ;
2015-06-20 11:22:15 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) Instance of 'NotAClass' object is destroyed immediately. \n " , errout . str ( ) ) ;
2010-10-01 17:23:22 +02:00
}
2014-11-20 14:20:09 +01: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 ( ) ) ;
}
2014-11-20 14:20:09 +01: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
2014-11-20 14:20:09 +01: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
2014-11-20 14:20:09 +01: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 "
2013-10-22 19:37:11 +02:00
" do_something(); \n "
2010-10-02 12:12:52 +02:00
" } \n "
) ;
2015-06-20 11:22:15 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (style) 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
2014-11-20 14:20:09 +01: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
}
2014-11-20 14:20:09 +01: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 "
2013-10-22 19:37:11 +02:00
" do_something(); \n "
2010-12-25 08:43:52 +01:00
" } \n " ;
check ( code , " test.cpp " ) ;
2015-06-20 11:22:15 +02:00
ASSERT_EQUALS ( " [test.cpp:7]: (style) 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-10-22 19:37:11 +02:00
" do_something(); \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
}
2014-11-20 14:20:09 +01: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 "
2013-10-22 19:37:11 +02:00
" do_something(); \n "
2011-01-31 20:07:41 +01:00
" } \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
2014-11-20 14:20:09 +01:00
void testMisusedScopeObjectInConstructor ( ) {
2013-07-22 07:25:53 +02:00
const char code [ ] = " class Foo { \n "
" public: \n "
" Foo(char x) { \n "
" Foo(x, 0); \n "
2013-10-22 19:37:11 +02:00
" do_something(); \n "
2013-07-22 07:25:53 +02:00
" } \n "
" Foo(char x, int y) { } \n "
" }; \n " ;
check ( code , " test.cpp " ) ;
2015-06-20 11:22:15 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) Instance of 'Foo' object is destroyed immediately. \n " , errout . str ( ) ) ;
2013-07-22 07:25:53 +02:00
}
2014-11-20 14:20:09 +01:00
void testMisusedScopeObjectNoCodeAfter ( ) {
2013-10-22 19:37:11 +02:00
check ( " class Foo {}; \n "
" void f() { \n "
" Foo(); \n " // No code after class => don't warn
" } " , " test.cpp " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01: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 ( ) ) ;
}
2014-11-20 14:20:09 +01: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
2014-11-20 14:20:09 +01:00
void clarifyCalculation ( ) {
2011-01-24 21:40:49 +01:00
check ( " int f(char c) { \n "
" return 10 * (c == 0) ? 1 : 2; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
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 "
2015-07-25 14:17:55 +02:00
" } " ) ;
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
2013-12-24 10:07:20 +01:00
check ( " void f() { \n "
" return (2*a)?b:c; \n "
" } " ) ;
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
2015-02-16 16:25:27 +01:00
" } " , 0 , 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
2014-12-30 17:55:29 +01:00
check ( " void f() { \n "
" int ab = a | b ? 2 : 3; \n "
" } " ) ;
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 ( ) ) ;
2013-12-28 11:02:39 +01:00
check ( " void f() { a = *p ? 1 : 2; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-22 19:21:56 +01:00
}
2014-11-20 14:20:09 +01:00
void clarifyStatement ( ) {
2012-08-24 11:28:50 +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-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 ( ) ) ;
2014-02-27 18:24:51 +01:00
2015-05-07 20:34:57 +02:00
check ( " void *f(char* p) { \n "
" for (p = path; *p++;) ; \n "
" } " ) ;
2014-02-27 18:24:51 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
}
2014-11-20 14:20:09 +01: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 "
" } " ) ;
2014-02-16 11:04:27 +01:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:2]: (style, inconclusive) 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 "
" } " ) ;
2014-02-16 11:04:27 +01:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:3]: (style, inconclusive) 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
" } " ) ;
2014-02-16 11:04:27 +01:00
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:5]: (style, inconclusive) 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , 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 "
" } " ) ;
2014-02-16 11:04:27 +01:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:2]: (style, inconclusive) 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
2014-11-20 14:20:09 +01:00
void duplicateBranch1 ( ) {
2012-05-30 01:30:37 +02:00
2012-07-31 23:35:56 +02:00
// tests inspired by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck )
2016-11-04 15:01:05 +01:00
// Errors detected in Quake 3: Arena by PVS-Studio: Fragment 2
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
" } " ) ;
2014-02-16 11:04:27 +01:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:3]: (style, inconclusive) 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
" } " ) ;
2014-02-16 11:04:27 +01:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:3]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. \n " , errout . str ( ) ) ;
2014-09-01 10:19:14 +02:00
// No message about empty branches (#5354)
check ( " void f() \n "
" { \n "
" if (front < 0) \n "
" {} \n "
" else \n "
" {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-30 01:30:37 +02:00
}
2014-11-20 14:20:09 +01:00
void duplicateBranch2 ( ) {
2017-05-18 21:52:31 +02:00
checkP ( " #define DOSTUFF1 ; \n "
" #define DOSTUFF2 ; \n "
" void f(int x) { \n " // #4329
" if (x) \n "
" DOSTUFF1 \n "
" else \n "
" DOSTUFF2 \n "
" } " ) ;
2013-02-18 17:18:33 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void duplicateExpression1 ( ) {
2014-01-02 16:58:07 +01:00
check ( " void foo(int a) { \n "
2011-04-10 16:25:02 +02:00
" if (a == a) { } \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
2011-04-10 16:25:02 +02:00
2014-01-02 16:58:07 +01:00
check ( " void fun(int b) { \n "
2013-04-13 20:17:53 +02:00
" return a && a || \n "
2011-04-10 16:25:02 +02:00
" b == b && \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
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&&'. \n "
" [test.cpp:3]: (style) Same expression on both sides of '=='. \n "
" [test.cpp:4]: (style) Same expression on both sides of '>'. \n "
" [test.cpp:5]: (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 "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [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 "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2011-11-05 22:17:48 +01:00
2014-01-02 16:58:07 +01:00
check ( " void foo(int b) { \n "
2011-11-08 21:26:33 +01:00
" f(a,b == b); \n "
2011-11-05 22:17:48 +01:00
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
2011-11-08 21:26:33 +01:00
2014-01-02 16:58:07 +01:00
check ( " void foo(int b) { \n "
2011-11-08 21:26:33 +01:00
" f(b == b, a); \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [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 "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
2011-11-08 21:22:31 +01:00
2015-02-23 16:38:55 +01:00
check ( " void foo(int a, int b) { \n "
" if ((a < b) && (b > a)) { } \n "
" } " ) ;
2018-08-25 14:25:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&&' because 'a<b' and 'b>a' represent the same value. \n " , errout . str ( ) ) ;
2015-02-23 16:38:55 +01:00
check ( " void foo(int a, int b) { \n "
" if ((a <= b) && (b >= a)) { } \n "
" } " ) ;
2018-08-25 14:25:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&&' because 'a<=b' and 'b>=a' represent the same value. \n " , errout . str ( ) ) ;
2015-02-23 16:38:55 +01:00
2011-11-08 21:22:31 +01:00
check ( " void foo() { \n "
2013-11-07 14:38:08 +01:00
" if (x!=2 || y!=3 || x!=2) {} \n "
2011-11-08 21:22:31 +01:00
" } " ) ;
2014-04-16 16:04:46 +02:00
TODO_ASSERT_EQUALS ( " error " , " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" if (x!=2 && (x=y) && x!=2) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-08 21:22:31 +01:00
check ( " void foo() { \n "
" if (a && b || a && b) {} \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [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 (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 "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '|'. \n " , errout . str ( ) ) ;
2011-11-08 21:24:44 +01:00
check ( " void foo() { \n "
" if ((a + b) | (a + b)) {} \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [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 "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&'. \n " , errout . str ( ) ) ;
2011-11-12 10:23:34 +01:00
2014-05-22 21:46:48 +02:00
check ( " void foo(int a, int b) { \n "
" if ((a | b) == (a | b)) {} \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
2014-05-22 21:46:48 +02: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 ( ) ) ;
2013-11-11 20:03:59 +01:00
check ( " int f(int x) { return x+x; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-15 06:35:46 +01:00
2015-08-07 20:28:25 +02:00
check ( " void f(int x) { while (x+=x) ; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-15 06:35:46 +01:00
check ( " void foo() { \n "
" if (a && b && b) {} \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2013-11-15 06:35:46 +01:00
2013-11-17 17:21:39 +01:00
check ( " void foo() { \n "
" if (a || b || b) {} \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
2013-11-17 17:21:39 +01:00
check ( " void foo() { \n "
" if (a / 1000 / 1000) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-20 16:18:09 +01:00
2014-03-17 18:35:36 +01:00
check ( " int foo(int i) { \n "
" return i/i; \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '/'. \n " , errout . str ( ) ) ;
2014-03-17 18:35:36 +01:00
2013-11-20 16:18:09 +01:00
check ( " void foo() { \n "
" if (a << 1 << 1) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() { return !!y; } " ) ; // No FP
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-24 15:17:08 +01:00
// make sure there are not "same expression" fp when there are different casts
check ( " void f(long x) { if ((int32_t)x == (int64_t)x) {} } " ,
2014-02-16 10:32:10 +01:00
nullptr , // filename
2013-11-24 15:17:08 +01:00
false , // experimental
false , // inconclusive
false , // runSimpleChecks
2014-02-16 10:32:10 +01:00
nullptr // settings
2013-11-24 15:17:08 +01:00
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-25 04:08:28 +01:00
// make sure there are not "same expression" fp when there are different ({}) expressions
2016-07-22 16:54:24 +02:00
check ( " void f(long x) { if (({ 1+2; }) == ({3+4;})) {} } " ) ;
2013-11-25 04:08:28 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-03-17 12:01:39 +01:00
// #5535: Reference named like its type
check ( " void foo() { UMSConfig& UMSConfig = GetUMSConfiguration(); } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-04-12 12:01:54 +02:00
// #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 ( ) ) ;
2018-07-08 15:58:04 +02:00
check ( " void f(const Bar &bar) { \n "
2014-04-12 12:01:54 +02:00
" bool a = bar.isSet() && bar->isSet(); \n "
" bool b = bar.isSet() && bar.isSet(); \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2014-05-22 21:46:48 +02:00
2017-03-15 19:15:05 +01:00
check ( " void foo(int a, int b) { \n "
2014-05-22 21:46:48 +02:00
" if ((b + a) | (a + b)) {} \n "
" } " ) ;
2018-08-25 14:25:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '|' because 'b+a' and 'a+b' represent the same value. \n " , errout . str ( ) ) ;
2014-05-22 21:46:48 +02:00
2016-07-28 13:19:24 +02:00
check ( " void foo(const std::string& a, const std::string& b) { \n "
2015-10-03 14:56:24 +02:00
" return a.find(b+ \" & \" ) || a.find( \" & \" +b); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-03-15 19:15:05 +01:00
check ( " void foo(int a, int b) { \n "
2014-05-22 21:46:48 +02:00
" if ((b > a) | (a > b)) {} \n " // > is not commutative
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-03-15 19:15:05 +01:00
check ( " void foo(double a, double b) { \n "
2014-05-22 21:46:48 +02:00
" if ((b + a) > (a + b)) {} \n "
" } " ) ;
2018-08-25 14:25:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) The expression 'b+a > a+b' is always false because 'b+a' and 'a+b' represent the same value. \n " , errout . str ( ) ) ;
2014-08-29 17:06:46 +02:00
check ( " void f(int x) { \n "
" if ((x == 1) && (x == 0x00000001)) \n "
" a++; \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2014-11-13 16:04:34 +01:00
check ( " void f() { \n "
" enum { Four = 4 }; \n "
" if (Four == 4) {} "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , true , false ) ;
2016-01-10 11:21:43 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-11-13 16:04:34 +01:00
check ( " void f() { \n "
" enum { Four = 4 }; \n "
" static_assert(Four == 4, " " ); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" enum { Four = 4 }; \n "
" static_assert(4 == Four, " " ); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" enum { FourInEnumOne = 4 }; \n "
" enum { FourInEnumTwo = 4 }; \n "
" if (FourInEnumOne == FourInEnumTwo) {} \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , true , false ) ;
2016-01-10 11:21:43 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-11-13 16:04:34 +01:00
check ( " void f() { \n "
" enum { FourInEnumOne = 4 }; \n "
" enum { FourInEnumTwo = 4 }; \n "
" static_assert(FourInEnumOne == FourInEnumTwo, " " ); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-07-29 14:27:53 +02:00
check ( " void foo(int a, int b) { \n "
" if (sizeof(a) == sizeof(a)) { } \n "
" if (sizeof(a) == sizeof(b)) { } \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
2015-07-29 14:27:53 +02:00
check ( " float bar(int) __attribute__((pure)); \n "
" char foo(int) __attribute__((pure)); \n "
" int test(int a, int b) { \n "
" if (bar(a) == bar(a)) { } \n "
" if (unknown(a) == unknown(a)) { } \n "
" if (foo(a) == foo(a)) { } \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
2012-05-14 09:57:30 +02:00
}
2014-11-20 14:20:09 +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 ( ) ) ;
2013-11-25 04:26:15 +01:00
2015-02-16 16:25:27 +01:00
check ( " float f(float x) { return (X double)x == (X double)x; } " , nullptr , false , false , false ) ;
2013-11-25 04:26:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct X { float f; }; \n "
" float f(struct X x) { return x.f == x.f; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-01-02 16:58:07 +01:00
2014-08-15 16:48:53 +02:00
check ( " struct X { int i; }; \n "
" int f(struct X x) { return x.i == x.i; } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '=='. \n " , errout . str ( ) ) ;
2014-08-15 16:48:53 +02:00
2014-01-02 16:58:07 +01:00
// #5284 - when type is unknown, assume it's float
check ( " int f() { return x==x; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-19 01:20:27 +02:00
}
2011-04-26 07:45:27 +02:00
2014-11-20 14:20:09 +01:00
void duplicateExpression3 ( ) {
2015-02-17 15:52:45 +01:00
Settings settings ;
const char xmldata [ ] = " <?xml version= \" 1.0 \" ?> \n "
" <def> \n "
" <function name= \" mystrcmp \" > \n "
" <pure/> \n "
" <arg nr= \" 1 \" /> \n "
" <arg nr= \" 2 \" /> \n "
" </function> \n "
" </def> " ;
tinyxml2 : : XMLDocument doc ;
doc . Parse ( xmldata , sizeof ( xmldata ) ) ;
settings . library . load ( doc ) ;
2011-11-10 21:49:14 +01:00
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 "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:6]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2011-11-10 21:49:14 +01:00
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
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:11]: (style) Same expression on both sides of '&&'. \n " , errout . str ( ) ) ;
2011-11-10 21:49:14 +01:00
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 ( ) ) ;
2014-09-27 21:31:04 +02:00
check ( " void foo() { \n "
2015-02-17 15:52:45 +01:00
" if ((mystrcmp(a, b) == 0) || (mystrcmp(a, b) == 0)) {} \n "
2015-07-25 14:17:55 +02:00
" } " , " test.cpp " , false , false , true , & settings ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
2014-03-14 06:38:45 +01:00
2014-03-14 19:06:05 +01:00
check ( " void GetValue() { return rand(); } \n "
" void foo() { \n "
" if ((GetValue() == 0) || (GetValue() == 0)) { dostuff(); } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void __attribute__((const)) GetValue() { return X; } \n "
2014-03-14 06:38:45 +01:00
" void foo() { \n "
" if ((GetValue() == 0) || (GetValue() == 0)) { dostuff(); } \n "
2011-11-10 21:49:14 +01:00
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
2011-11-10 21:49:14 +01:00
2014-03-14 19:06:05 +01:00
check ( " void GetValue() __attribute__((const)); \n "
" void GetValue() { return X; } \n "
" void foo() { \n "
" if ((GetValue() == 0) || (GetValue() == 0)) { dostuff(); } \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
2014-03-14 19:06:05 +01:00
2011-11-10 21:49:14 +01:00
check ( " void foo() { \n "
" if (str == \" ( \" || str == \" ( \" ) {} \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [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 ( ) ) ;
2014-01-12 07:40:56 +01:00
// #5334
check ( " void f(C *src) { \n "
" if (x<A*>(src) || x<B*>(src)) \n "
" a++; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(A *src) { \n "
" if (dynamic_cast<B*>(src) || dynamic_cast<B*>(src)) {} \n "
2015-02-16 16:25:27 +01:00
" } \n " , " test.cpp " , false , false , false ) ; // don't run simplifications
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '||'. \n " , errout . str ( ) ) ;
2014-01-12 07:40:56 +01:00
2014-05-21 18:19:42 +02:00
// #5819
check ( " Vector func(Vector vec1) { \n "
" return fabs(vec1 & vec1 & vec1); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " Vector func(int vec1) { \n "
" return fabs(vec1 & vec1 & vec1); \n "
" } " ) ;
2018-08-05 10:48:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression on both sides of '&'. \n " , errout . str ( ) ) ;
2014-05-21 18:19:42 +02:00
2011-11-10 21:49:14 +01:00
}
2014-11-20 14:20:09 +01:00
void duplicateExpression4 ( ) {
2011-12-01 17:46:33 +01:00
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 ( ) ) ;
2014-05-09 06:16:27 +02:00
// assignment
check ( " void f() { \n "
" while (*(a+=2)==*(b+=2) && *(a+=2)==*(b+=2)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-01 17:46:33 +01:00
}
2011-11-10 21:49:14 +01:00
2014-11-20 14:20:09 +01:00
void duplicateExpression5 ( ) { // #3749 - macros with same values
2012-05-17 07:26:57 +02:00
check ( " void f() { \n "
" if ($a == $a) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void duplicateExpression6 ( ) { // #4639
2013-03-14 19:11:29 +01:00
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 ( ) ) ;
}
2018-08-07 09:32:16 +02:00
void duplicateExpression7 ( ) {
check ( " void f() { \n "
" const int i = sizeof(int); \n "
" if ( i != sizeof (int)){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f() { \n "
" const int i = sizeof(int); \n "
" if ( sizeof (int) != i){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f(int a = 1) { if ( a != 1){}} \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int a = 1; \n "
" if ( a != 1){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f() { \n "
" int a = 1; \n "
" int b = 1; \n "
" if ( a != b){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f() { \n "
" int a = 1; \n "
" int b = a; \n "
" if ( a != b){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
2018-08-07 18:06:51 +02:00
check ( " void use(int); \n "
2018-08-07 09:32:16 +02:00
" void f() { \n "
" int a = 1; \n "
" int b = 1; \n "
" use(b); \n "
" if ( a != 1){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:6]: (style) The expression 'a != 1' is always false. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
2018-08-07 18:06:51 +02:00
check ( " void use(int); \n "
2018-08-07 09:32:16 +02:00
" void f() { \n "
" int a = 1; \n "
" use(a); \n "
" a = 2; \n "
" if ( a != 1){} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-08-07 18:06:51 +02:00
check ( " void use(int); \n "
2018-08-07 09:32:16 +02:00
" void f() { \n "
" int a = 2; \n "
" use(a); \n "
" a = 1; \n "
" if ( a != 1){} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " const int a = 1; \n "
" void f() { \n "
" if ( a != 1){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:3]: (style) The expression 'a != 1' is always false. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " int a = 1; \n "
" void f() { \n "
" if ( a != 1){} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" static const int a = 1; \n "
" if ( a != 1){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f() { \n "
" static int a = 1; \n "
" if ( a != 1){} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int a = 1; \n "
" if ( a != 1){ \n "
" a++; \n "
" }} \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f(int b) { \n "
" int a = 1; \n "
" while (b) { \n "
" if ( a != 1){} \n "
" a++; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void duplicateExpression8 ( ) {
check ( " void f() { \n "
" int a = 1; \n "
" int b = a; \n "
" a = 2; \n "
" if ( b != a){} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int * a, int i) { int b = a[i]; a[i] = 2; if ( b != a[i]){}} \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int * a, int i) { int b = *a; *a = 2; if ( b != *a){}} \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { int f() const; }; \n "
" A g(); \n "
" void foo() { \n "
" for (const A x = A();;) { \n "
" const int a = x.f(); \n "
" x = g(); \n "
" if (x.f() == a) break; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f(int i); \n "
" struct A { \n "
" enum E { B, C }; \n "
" bool f(E); \n "
" }; \n "
" void foo() { \n "
" A a; \n "
" const bool x = a.f(A::B); \n "
" const bool y = a.f(A::C); \n "
" if(!x && !y) return; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" const bool x = a.f(A::B); \n "
" const bool y = a.f(A::C); \n "
" if (!x && !y) return; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(bool * const b); \n "
" void foo() { \n "
" bool x = true; \n "
" bool y = true; \n "
" f(&x); \n "
" if (!x && !y) return; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" const int a = {}; \n "
" if(a == 1) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-03 19:51:48 +02:00
check ( " volatile const int var = 42; \n "
" void f() { if(var == 42) {} } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
}
void duplicateExpressionLoop ( ) {
check ( " void f() { \n "
" int a = 1; \n "
" while ( a != 1){} \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f() { int a = 1; while ( a != 1){ a++; }} \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { int a = 1; for ( int i=0; i < 3 && a != 1; i++){ a++; }} \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int b) { int a = 1; while (b) { if ( a != 1){} b++; } a++; } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-08-13 12:55:41 +02:00
check ( " void f() { \n "
" for(int i = 0; i < 10;) { \n "
" if( i != 0 ) {} \n "
" } \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != 0' is always false. \n " , errout . str ( ) ) ;
2018-08-13 12:55:41 +02:00
check ( " void f() { \n "
" for(int i = 0; i < 10;) { \n "
" if( i != 0 ) {} \n "
" i++; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" for(int i = 0; i < 10;) { \n "
" if( i != 0 ) { i++; } \n "
" i++; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" for(int i = 0; i < 10;) { \n "
" if( i != 0 ) { i++; } \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int i = 0; \n "
" while(i < 10) { \n "
" if( i != 0 ) {} \n "
" i++; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
check ( " void f(int b) { \n "
" while (b) { \n "
" int a = 1; \n "
" if ( a != 1){} \n "
" b++; \n "
" } \n "
" } \n " ) ;
2018-09-05 05:53:33 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != 1' is always false. \n " , errout . str ( ) ) ;
2018-08-07 09:32:16 +02:00
}
2015-01-03 18:01:49 +01:00
void duplicateExpressionTernary ( ) { // #6391
check ( " void f() { \n "
" return A ? x : x; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same expression in both branches of ternary operator. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" return A ? x : z; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-08-01 21:53:43 +02:00
check ( " void f(unsigned char c) { \n "
" x = y ? (signed char)c : (unsigned char)c; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-03-15 19:15:05 +01:00
check ( " std::string stringMerge(std::string const& x, std::string const& y) { \n " // #7938
" return ((x > y) ? (y + x) : (x + y)); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-04-04 14:31:48 +02:00
// #6426
check ( " void foo(bool flag) { \n "
" bar( (flag) ? ~0u : ~0ul); \n "
" } " ) ;
2018-04-04 16:07:07 +02:00
ASSERT_EQUALS ( ( _settings . sizeof_int = = _settings . sizeof_long ) ? " [test.cpp:2]: (style) Same value in both branches of ternary operator. \n " : " " , errout . str ( ) ) ;
2015-01-03 18:01:49 +01:00
}
2018-04-03 21:43:55 +02:00
void duplicateValueTernary ( ) {
check ( " void f() { \n "
" if( a ? (b ? false:false): false ) ; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
check ( " int f1(int a) {return (a == 1) ? (int)1 : 1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
check ( " int f2(int a) {return (a == 1) ? (int)1 : (int)1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
2018-04-04 10:04:40 +02:00
2018-04-03 21:43:55 +02:00
check ( " int f3(int a) {return (a == 1) ? 1 : (int)1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
2018-04-04 10:04:40 +02:00
2018-04-03 21:43:55 +02:00
check ( " int f4(int a) {return (a == 1) ? 1 : 1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
2018-04-04 10:04:40 +02:00
2018-04-03 21:43:55 +02:00
check ( " int f5(int a) {return (a == (int)1) ? (int)1 : 1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
2018-04-04 10:04:40 +02:00
2018-04-03 21:43:55 +02:00
check ( " int f6(int a) {return (a == (int)1) ? (int)1 : (int)1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
2018-04-04 10:04:40 +02:00
2018-04-03 21:43:55 +02:00
check ( " int f7(int a) {return (a == (int)1) ? 1 : (int)1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
2018-04-04 10:04:40 +02:00
2018-04-03 21:43:55 +02:00
check ( " int f8(int a) {return (a == (int)1) ? 1 : 1; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (style) Same value in both branches of ternary operator. \n " , errout . str ( ) ) ;
}
2018-01-11 09:41:22 +01:00
void duplicateExpressionTemplate ( ) { // #6930
check ( " template <int I> void f() { \n "
" if (I >= 0 && I < 3) {} \n "
" } \n "
" \n "
" static auto a = f<0>(); " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-04-21 11:28:21 +02:00
void oppositeExpression ( ) {
check ( " void f(bool a) { if(a && !a) {} } " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '&&'. \n " , errout . str ( ) ) ;
check ( " void f(bool a) { if(a != !a) {} } " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '!='. \n " , errout . str ( ) ) ;
2018-04-21 13:28:31 +02:00
2018-04-21 11:28:21 +02:00
check ( " void f(bool a) { if( a == !(a) ) {}} " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '=='. \n " , errout . str ( ) ) ;
2018-04-21 13:28:31 +02:00
2018-04-21 11:28:21 +02:00
check ( " void f(bool a) { if( a != !(a) ) {}} " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '!='. \n " , errout . str ( ) ) ;
2018-04-21 13:28:31 +02:00
2018-04-21 11:28:21 +02:00
check ( " void f(bool a) { if( !(a) == a ) {}} " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '=='. \n " , errout . str ( ) ) ;
2018-04-21 13:28:31 +02:00
2018-04-21 11:28:21 +02:00
check ( " void f(bool a) { if( !(a) != a ) {}} " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '!='. \n " , errout . str ( ) ) ;
2018-04-21 13:28:31 +02:00
2018-04-21 11:28:21 +02:00
check ( " void f(bool a) { if( !(!a) == !(a) ) {}} " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '=='. \n " , errout . str ( ) ) ;
2018-04-21 13:28:31 +02:00
2018-04-21 11:28:21 +02:00
check ( " void f(bool a) { if( !(!a) != !(a) ) {}} " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '!='. \n " , errout . str ( ) ) ;
2018-04-21 13:28:31 +02:00
2018-04-21 11:28:21 +02:00
check ( " void f(bool a) { a = !a; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-05-02 06:32:33 +02:00
check ( " void f(int a) { if( a < -a ) {}} " ) ;
ASSERT_EQUALS ( " [test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '<'. \n " , errout . str ( ) ) ;
check ( " void f(int a) { a -= -a; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int a) { a = a / (-a); } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-05-04 07:58:30 +02:00
check ( " bool f(int i){ return !((i - 1) & i); } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-05-08 20:43:57 +02:00
check ( " bool f(unsigned i){ return (x > 0) && (x & (x-1)) == 0; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-05 18:07:01 +02:00
check ( " void A::f(bool a, bool c) \n "
" { \n "
" const bool b = a; \n "
" if(c) { a = false; } \n "
" if(b && !a) { } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-09 07:08:32 +02:00
2018-09-12 17:30:18 +02:00
check ( " void f(bool c) { \n "
" const bool b = a; \n "
" if(c) { a = false; } \n "
" if(b && !a) { } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" bool x = a; \n "
" dostuff(); \n "
" if (x && a) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-09 07:08:32 +02:00
check ( " void f() { \n "
" const bool b = g(); \n "
" if (!b && g()) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-04-21 11:28:21 +02:00
}
2018-04-08 14:43:19 +02:00
void duplicateVarExpression ( ) {
check ( " int f() __attribute__((pure)); \n "
2018-06-09 08:05:19 +02:00
" int g() __attribute__((pure)); \n "
2018-04-08 14:43:19 +02:00
" void test() { \n "
" int i = f(); \n "
" int j = f(); \n "
" } " ) ;
2018-06-09 08:05:19 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
2018-05-13 20:20:55 +02:00
check ( " struct Foo { int f() const; int g() const; }; \n "
2018-04-08 14:43:19 +02:00
" void test() { \n "
" Foo f = Foo{}; \n "
" int i = f.f(); \n "
" int j = f.f(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
2018-05-13 20:20:55 +02:00
check ( " struct Foo { int f() const; int g() const; }; \n "
" void test() { \n "
" Foo f = Foo{}; \n "
" Foo f2 = Foo{}; \n "
" int i = f.f(); \n "
" int j = f.f(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6] -> [test.cpp:5]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
check ( " int f() __attribute__((pure)); \n "
2018-06-09 08:05:19 +02:00
" int g() __attribute__((pure)); \n "
2018-04-08 14:43:19 +02:00
" void test() { \n "
" int i = 1 + f(); \n "
" int j = 1 + f(); \n "
" } " ) ;
2018-06-09 08:05:19 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
2018-08-08 08:31:31 +02:00
check ( " int f() __attribute__((pure)); \n "
" int g() __attribute__((pure)); \n "
" void test() { \n "
" int i = f() + 1; \n "
" int j = 1 + f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() __attribute__((pure)); \n "
" int g() __attribute__((pure)); \n "
" void test() { \n "
" int x = f(); \n "
" int i = x + 1; \n "
" int j = f() + 1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
check ( " int f() __attribute__((pure)); \n "
2018-06-09 08:05:19 +02:00
" int g() __attribute__((pure)); \n "
2018-04-08 14:43:19 +02:00
" void test() { \n "
" int i = f() + f(); \n "
" int j = f() + f(); \n "
" } " ) ;
2018-06-09 08:05:19 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
check ( " int f(int) __attribute__((pure)); \n "
2018-05-13 20:20:55 +02:00
" int g(int) __attribute__((pure)); \n "
2018-04-08 14:43:19 +02:00
" void test() { \n "
" int i = f(0); \n "
" int j = f(0); \n "
" } " ) ;
2018-05-13 20:20:55 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:4]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
2018-08-08 08:31:31 +02:00
check ( " int f(int) __attribute__((pure)); \n "
" int g(int) __attribute__((pure)); \n "
" void test() { \n "
" const int x = 0; \n "
" int i = f(0); \n "
" int j = f(x); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-05-13 20:20:55 +02:00
check ( " void test(int * p, int * q) { \n "
2018-04-08 14:43:19 +02:00
" int i = *p; \n "
" int j = *p; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
2018-05-13 20:20:55 +02:00
check ( " struct A { int x; int y; }; "
2018-04-08 14:43:19 +02:00
" void test(A a) { \n "
" int i = a.x; \n "
" int j = a.x; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (style) Same expression used in consecutive assignments of 'i' and 'j'. \n " , errout . str ( ) ) ;
check ( " void test() { \n "
" int i = 0; \n "
" int j = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void test() { \n "
" int i = -1; \n "
" int j = -1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f(int); \n "
" void test() { \n "
" int i = f(0); \n "
" int j = f(1); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f(); \n "
2018-06-09 08:05:19 +02:00
" int g(); \n "
2018-04-08 14:43:19 +02:00
" void test() { \n "
" int i = f() || f(); \n "
" int j = f() && f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo {}; \n "
" void test() { \n "
" Foo i = Foo(); \n "
" Foo j = Foo(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo {}; \n "
" void test() { \n "
" Foo i = Foo{}; \n "
" Foo j = Foo{}; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-06-09 08:05:19 +02:00
check ( " struct Foo { int f() const; float g() const; }; \n "
" void test() { \n "
" Foo f = Foo{}; \n "
" int i = f.f(); \n "
" int j = f.f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo { int f(); int g(); }; \n "
" void test() { \n "
" Foo f = Foo{}; \n "
" int i = f.f(); \n "
" int j = f.f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
check ( " void test() { \n "
" int i = f(); \n "
" int j = f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void test(int x) { \n "
" int i = ++x; \n "
" int j = ++x; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void test(int x) { \n "
" int i = x++; \n "
" int j = x++; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void test(int x) { \n "
" int i = --x; \n "
" int j = --x; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void test(int x) { \n "
" int i = x--; \n "
" int j = x--; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-08-08 08:31:31 +02:00
check ( " void test(int x) { \n "
" int i = x + 1; \n "
" int j = 1 + x; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-04-08 14:43:19 +02:00
}
2018-05-13 20:20:55 +02:00
void duplicateVarExpressionUnique ( ) {
check ( " struct SW { int first; }; \n "
" void foo(SW* x) { \n "
" int start = x->first; \n "
" int end = x->first; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct SW { int first; }; \n "
" void foo(SW* x, int i, int j) { \n "
" int start = x->first; \n "
" int end = x->first; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo { int f() const; }; \n "
" void test() { \n "
" Foo f = Foo{}; \n "
" int i = f.f(); \n "
" int j = f.f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void test(int * p) { \n "
" int i = *p; \n "
" int j = *p; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo { int f() const; int g(int) const; }; \n "
" void test() { \n "
" Foo f = Foo{}; \n "
" int i = f.f(); \n "
" int j = f.f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Foo { int f() const; }; \n "
" void test() { \n "
" Foo f = Foo{}; \n "
" int i = f.f(); \n "
" int j = f.f(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-06-10 00:05:23 +02:00
void duplicateVarExpressionAssign ( ) {
check ( " struct A { int x; int y; }; "
" void use(int); \n "
" void test(A a) { \n "
" int i = a.x; \n "
" int j = a.x; \n "
" use(i); \n "
" i = j; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { int x; int y; }; "
" void use(int); \n "
" void test(A a) { \n "
" int i = a.x; \n "
" int j = a.x; \n "
" use(j); \n "
" j = i; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Issue #8612
check ( " struct P \n "
" { \n "
" void func(); \n "
" bool operator==(const P&) const; \n "
" }; \n "
" struct X \n "
" { \n "
" P first; \n "
" P second; \n "
" }; \n "
" bool bar(); \n "
" void baz(const P&); \n "
" void foo(const X& x) \n "
" { \n "
" P current = x.first; \n "
" P previous = x.first; \n "
" while (true) \n "
" { \n "
" baz(current); \n "
" if (bar() && previous == current) \n "
" { \n "
" current.func(); \n "
" } \n "
" previous = current; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2018-06-14 22:39:26 +02:00
void duplicateVarExpressionCrash ( ) {
// Issue #8624
check ( " struct X { \n "
" X(); \n "
" int f() const; \n "
" }; \n "
" void run() { \n "
" X x; \n "
" int a = x.f(); \n "
" int b = x.f(); \n "
" (void)a; \n "
" (void)b; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-02 08:28:53 +02:00
// Issue #8712
check ( " void f() { \n "
" unsigned char d; \n "
" d = d % 5; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " template <typename T> \n "
" T f() { \n "
" T a = T(); \n "
" } \n "
" int &a = f<int&>(); \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Issue #8713
check ( " class A { \n "
" int64_t B = 32768; \n "
" P<uint8_t> m = MakeP<uint8_t>(B); \n "
" }; \n "
" void f() { \n "
" uint32_t a = 42; \n "
" uint32_t b = uint32_t(A ::B / 1024); \n "
" int32_t c = int32_t(a / b); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-03 19:54:14 +02:00
// Issue #8709
check ( " a b; \n "
" void c() { \n "
" switch (d) { case b:; } \n "
" double e(b); \n "
" if(e <= 0) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-06-14 22:39:26 +02:00
}
2018-09-06 06:55:36 +02:00
void multiConditionSameExpression ( ) {
check ( " void f() { \n "
" int val = 0; \n "
" if (val < 0) continue; \n "
" if ((val > 0)) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false. \n "
" [test.cpp:2] -> [test.cpp:4]: (style) The expression 'val > 0' is always false. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" int val = 0; \n "
" if (val < 0) { \n "
" if ((val > 0)) {} \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false. \n "
" [test.cpp:2] -> [test.cpp:4]: (style) The expression 'val > 0' is always false. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" int val = 0; \n "
" if (val < 0) { \n "
" if ((val < 0)) {} \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false. \n "
" [test.cpp:2] -> [test.cpp:4]: (style) The expression 'val < 0' is always false. \n " , errout . str ( ) ) ;
2018-09-07 15:02:34 +02:00
check ( " void f() { \n "
" int activate = 0; \n "
" int foo = 0; \n "
" if (activate) {} \n "
" else if (foo) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-06 06:55:36 +02:00
}
2014-11-20 14:20:09 +01:00
void checkSignOfUnsignedVariable ( ) {
2015-10-08 11:35:51 +02:00
check (
2011-10-09 20:35:46 +02:00
" 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
2015-10-08 11:35:51 +02:00
check (
2011-10-09 20:35:46 +02:00
" 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
2015-10-08 11:35:51 +02:00
check (
2011-08-07 01:23:09 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-08-07 01:23:09 +02:00
" bool foo(int x) { \n "
" if (x < 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-08-07 01:23:09 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-08-07 01:23:09 +02:00
" bool foo(int x) { \n "
" if (0 > x) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-08-07 01:23:09 +02:00
" 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
2015-10-08 11:35:51 +02:00
check (
2011-08-07 01:23:09 +02:00
" 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
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (x < 0 && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (0 > x && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (x >= 0 && y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (y && x < 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (y && 0 > x) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (y && x >= 0) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (x < 0 || y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" bool foo(int x, bool y) { \n "
" if (0 > x || y) "
" return true; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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
2015-10-08 11:35:51 +02:00
check (
2011-09-27 03:24:34 +02:00
" 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>(); " ;
2015-10-08 11:35:51 +02:00
check ( code , nullptr , false , false ) ;
2011-11-06 18:24:37 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( code , nullptr , false , 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
2014-11-20 14:20:09 +01:00
void checkSignOfPointer ( ) {
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" bool foo(int* x) { \n "
" if (*x >= 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" bool foo(int* x) { \n "
" if (*x < 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2014-09-04 13:51:18 +02:00
" bool foo(int* x, int* y) { \n "
" if (x - y < 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2014-09-04 13:51:18 +02:00
" bool foo(int* x, int* y) { \n "
" if (x - y <= 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2014-09-04 13:51:18 +02:00
" bool foo(int* x, int* y) { \n "
" if (x - y > 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2014-09-04 13:51:18 +02:00
" bool foo(int* x, int* y) { \n "
" if (x - y >= 0) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first) { \n "
" if (first.ptr >= 0) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (style) A pointer can not be negative so it is either pointless or an error to check if it is not. \n " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first, S* second) { \n "
" if((first.ptr - second.ptr) >= 0) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first) { \n "
" if((first.ptr) >= 0) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (style) A pointer can not be negative so it is either pointless or an error to check if it is not. \n " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first, S* second) { \n "
" if(0 <= first.ptr - second.ptr) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first, S* second) { \n "
" if(0 <= (first.ptr - second.ptr)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first, S* second) { \n "
" if(first.ptr - second.ptr < 0) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first, S* second) { \n "
" if((first.ptr - second.ptr) < 0) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first, S* second) { \n "
" if(0 > first.ptr - second.ptr) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2015-03-19 14:46:00 +01:00
" struct S { \n "
" int* ptr; \n "
" }; \n "
" void foo(S* first, S* second) { \n "
" if(0 > (first.ptr - second.ptr)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" bool foo(int* x) { \n "
" if (0 <= x[0]) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" bool foo(Bar* x) { \n "
" if (0 <= x.y) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2014-09-04 13:51:18 +02:00
" bool foo(Bar* x) { \n "
" if (0 <= x->y) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2014-09-04 13:51:18 +02:00
" bool foo(Bar* x, Bar* y) { \n "
" if (0 <= x->y - y->y ) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" 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 ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" bool foo(int* x) { \n "
" if (0 > x[0]) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2012-08-21 12:28:02 +02:00
" bool foo(Bar* x) { \n "
" if (0 > x.y) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-07-23 17:19:20 +02:00
2015-10-08 11:35:51 +02:00
check (
2014-09-04 13:51:18 +02:00
" bool foo(Bar* x) { \n "
" if (0 > x->y) "
" bar(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2013-07-23 17:19:20 +02:00
" void foo() { \n "
" int (*t)(void *a, void *b); \n "
" if (t(a, b) < 0) \n "
" bar(); \n "
2013-07-24 11:20:28 +02:00
" } " ) ;
2013-07-23 17:19:20 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2013-07-23 17:19:20 +02:00
" void foo() { \n "
" int (*t)(void *a, void *b); \n "
" if (0 > t(a, b)) \n "
" bar(); \n "
2013-07-24 11:20:28 +02:00
" } " ) ;
2013-07-23 17:19:20 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-06 16:52:27 +02:00
2015-10-08 11:35:51 +02:00
check (
2013-10-06 16:52:27 +02:00
" struct object_info { int *typep; }; \n "
" void packed_object_info(struct object_info *oi) { \n "
" if (oi->typep < 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) A pointer can not be negative so it is either pointless or an error to check if it is. \n " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2013-10-06 16:52:27 +02:00
" struct object_info { int typep[10]; }; \n "
" void packed_object_info(struct object_info *oi) { \n "
" if (oi->typep < 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) A pointer can not be negative so it is either pointless or an error to check if it is. \n " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check (
2013-10-06 16:52:27 +02:00
" struct object_info { int *typep; }; \n "
" void packed_object_info(struct object_info *oi) { \n "
" if (*oi->typep < 0); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-08-21 12:28:02 +02:00
}
2014-11-20 14:20:09 +01: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
}
2014-11-20 14:20:09 +01: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-09-05 08:31:23 +02:00
2014-11-20 14:20:09 +01:00
void checkInvalidFree ( ) {
2012-09-05 08:31:23 +02:00
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
}
2014-11-20 14:20:09 +01:00
void checkRedundantCopy ( ) {
2015-10-08 11:35:51 +02:00
check ( " const std::string& getA(){static std::string a;return a;} \n "
" void foo() { \n "
" const std::string a = getA(); \n "
" } " ) ;
2014-05-02 20:09:38 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying. \n " , errout . str ( ) ) ;
2014-03-27 11:15:17 +01:00
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2014-05-02 20:09:38 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying. \n " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
2015-10-08 11:35:51 +02:00
check ( " const int& getA(){static int a;return a;} \n "
" int main() \n "
" { \n "
" const int a = getA(); \n "
" return 0; \n "
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2014-05-02 20:09:38 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying. \n " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
2015-10-08 11:35:51 +02:00
check ( " const int& getA(){static int a;return a;} \n "
" int main() \n "
" { \n "
" const int a(getA()); \n "
" return 0; \n "
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( " 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 "
" } " ) ;
2012-08-24 00:03:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-12-14 08:46:18 +01:00
// #5190 - FP when creating object with constructor that takes a reference
2015-10-08 11:35:51 +02:00
check ( " class A {}; \n "
" class B { B(const A &a); }; \n "
" const A &getA(); \n "
" void f() { \n "
" const B b(getA()); \n "
" } " ) ;
2013-12-14 08:46:18 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-05-02 20:09:38 +02:00
// #5618
2014-09-11 19:45:52 +02:00
const char * code5618 = " class Token { \n "
" public: \n "
" const std::string& str(); \n "
" }; \n "
" void simplifyArrayAccessSyntax() { \n "
" for (Token *tok = list.front(); tok; tok = tok->next()) { \n "
" const std::string temp = tok->str(); \n "
" tok->str(tok->strAt(2)); \n "
" } \n "
" } " ;
2015-10-08 11:35:51 +02:00
check ( code5618 , nullptr , false , true ) ;
2014-05-02 20:09:38 +02:00
TODO_ASSERT_EQUALS ( " " , " [test.cpp:7]: (performance, inconclusive) Use const reference for 'temp' to avoid unnecessary data copying. \n " , errout . str ( ) ) ;
2015-10-08 11:35:51 +02:00
check ( code5618 , nullptr , false , false ) ;
2014-09-11 19:45:52 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-05-29 02:58:18 +02:00
// #5890 - crash: wesnoth desktop_util.cpp / unicode.hpp
2015-10-08 11:35:51 +02:00
check ( " typedef std::vector<char> X; \n "
" X f<X>(const X &in) { \n "
" const X s = f<X>(in); \n "
" return f<X>(s); \n "
" } " ) ;
2014-05-29 02:58:18 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
}
2014-11-20 14:20:09 +01:00
void checkNegativeShift ( ) {
2012-08-24 00:03:22 +02:00
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
" } " ) ;
2014-03-29 13:01:30 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value is undefined behaviour \n " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
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 "
" } " ) ;
2014-03-29 13:01:30 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value is undefined behaviour \n " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
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 "
" } " ) ;
2014-03-29 13:01:30 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value is undefined behaviour \n " , errout . str ( ) ) ;
2012-08-24 00:03:22 +02:00
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
" } " ) ;
2014-03-29 13:01:30 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Shifting by a negative value is undefined behaviour \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 ( ) ) ;
2016-12-22 09:40:19 +01:00
check ( " void foo() { \n "
" x = (-10+2) << 3; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (portability) Shifting a negative value is technically undefined behaviour \n " , errout . str ( ) ) ;
2015-01-05 13:23:38 +01:00
2015-01-05 10:01:04 +01:00
check ( " x = y ? z << $-1 : 0; \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-11-29 16:28:55 +01:00
// Negative LHS
check ( " const int x = -1 >> 2; " ) ;
2016-12-22 09:40:19 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (portability) Shifting a negative value is technically undefined behaviour \n " , errout . str ( ) ) ;
2015-12-14 10:55:23 +01:00
// #6383 - unsigned type
check ( " const int x = (unsigned int)(-1) >> 2; " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-11-23 23:26:18 +01:00
// #7814 - UB happening in valueflowcode when it tried to compute shifts.
check ( " int shift1() { return 1 >> -1 ;} \n "
" int shift2() { return 1 << -1 ;} \n "
" int shift3() { return -1 >> 1 ;} \n "
" int shift4() { return -1 << 1 ;} \n " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) Shifting by a negative value is undefined behaviour \n "
" [test.cpp:2]: (error) Shifting by a negative value is undefined behaviour \n "
2016-12-22 09:40:19 +01:00
" [test.cpp:3]: (portability) Shifting a negative value is technically undefined behaviour \n "
" [test.cpp:4]: (portability) Shifting a negative value is technically undefined behaviour \n " , errout . str ( ) ) ;
2012-08-22 15:44:20 +02:00
}
2012-08-24 11:28:50 +02:00
2014-11-20 14:20:09 +01:00
void incompleteArrayFill ( ) {
2012-08-24 11:28:50 +02:00
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 "
" } " ) ;
2015-08-25 21:19:19 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:4]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memset()' with 'sizeof(*a)'? \n " , " " , errout . str ( ) ) ;
2012-08-24 11:28:50 +02:00
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
2014-11-20 14:20:09 +01:00
void redundantVarAssignment ( ) {
2012-09-02 13:09:32 +02:00
// Simple tests
check ( " void f(int i) { \n "
" i = 1; \n "
" i = 1; \n "
" } " ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
2013-10-23 06:42:52 +02:00
{
// non-local variable => only show warning when inconclusive is used
const char code [ ] = " int i; \n "
" void f() { \n "
" i = 1; \n "
" i = 1; \n "
" } " ;
check ( code , " test.cpp " , false , false ) ; // inconclusive = false
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( code , " test.cpp " , false , true ) ; // inconclusive = true
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style, inconclusive) Variable 'i' is reassigned a value before the old one has been used if variable is no semaphore variable. \n " , errout . str ( ) ) ;
2013-10-23 06:42:52 +02:00
}
2012-09-02 13:09:32 +02:00
check ( " void f() { \n "
" int i; \n "
" i = 1; \n "
" i = 1; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style) 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-12-07 20:45:20 +01:00
check ( " void f() { \n "
" static int i; \n "
" i = 1; \n "
" i = 1; \n "
" } " ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style, 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
2016-07-18 12:43:23 +02:00
check ( " void f() { \n "
" int i[10]; \n "
" i[2] = 1; \n "
" i[2] = 1; \n "
" } " , nullptr , false , false , false ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style) Variable 'i[2]' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2016-07-25 07:35:33 +02:00
check ( " void f(int x) { \n "
" int i[10]; \n "
" i[x] = 1; \n "
" x=1; \n "
" i[x] = 1; \n "
" } " , nullptr , false , false , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(const int x) { \n "
" int i[10]; \n "
" i[x] = 1; \n "
" i[x] = 1; \n "
" } " , nullptr , false , false , false ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style) Variable 'i[x]' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
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
" } " ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style, 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 "
" } " ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:4]: (style) 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:5]: (style) 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2012-09-02 13:09:32 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-04-10 21:56:30 +02:00
check ( " void f() { \n "
" Foo bar = foo(); \n "
" bar(); \n " // #5568. operator() called
" bar = y(); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
// 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 "
" } " ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:5]: (style) 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 "
" } " ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:6]: (style) Variable 'x' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2013-02-15 18:40:34 +01:00
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 "
2015-12-06 08:14:04 +01:00
" void f(Foo z); \n "
2013-02-15 18:40:34 +01:00
" }; \n "
" \n "
" void C::f(Foo z) { \n "
" x = 2; \n "
" x = z.g(); \n "
" } " ) ;
2016-07-18 12:43:23 +02:00
ASSERT_EQUALS ( " " , 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 "
2015-02-16 16:25:27 +01:00
" } " , nullptr , false , false , false ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style) Variable 'x' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2013-05-09 15:25:36 +02:00
// 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 "
2015-02-16 16:25:27 +01:00
" } " , 0 , false , false , false ) ;
2013-05-09 15:39:33 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int i = 54; \n "
" i = 0; \n "
2015-02-16 16:25:27 +01:00
" } " , 0 , false , false , false ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2013-05-09 15:39:33 +02:00
check ( " void f() { \n "
" int i = 54; \n "
" i = 1; \n "
2015-02-16 16:25:27 +01:00
" } " , 0 , false , false , false ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (style) Variable 'i' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2013-06-10 22:02:02 +02:00
check ( " int foo() { \n " // #4420
" int x; \n "
" bar(++x); \n "
" x = 5; \n "
" return bar(x); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-08-15 16:13:58 +02:00
// struct member..
check ( " struct AB { int a; int b; }; \n "
" \n "
" int f() { \n "
" struct AB ab; \n "
" ab.a = 1; \n "
" ab.a = 2; \n "
" return ab.a; \n "
" } " ) ;
2016-07-18 12:43:23 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:6]: (style) Variable 'ab.a' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2013-08-15 16:13:58 +02:00
check ( " struct AB { int a; int b; }; \n "
" \n "
" int f() { \n "
" struct AB ab; \n "
" ab.a = 1; \n "
" ab = do_something(); \n "
" return ab.a; \n "
" } " ) ;
TODO_ASSERT_EQUALS ( " error " , " " , errout . str ( ) ) ;
check ( " struct AB { int a; int b; }; \n "
" \n "
" int f() { \n "
" struct AB ab; \n "
" ab.a = 1; \n "
" do_something(&ab); \n "
" ab.a = 2; \n "
" return ab.a; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-04 23:46:07 +01:00
2014-02-23 11:02:39 +01:00
check ( " struct AB { int a; int b; }; \n "
" \n "
" int f(DO_SOMETHING do_something) { \n "
" struct AB ab; \n "
" ab.a = 1; \n "
" do_something(&ab); \n "
" ab.a = 2; \n "
" return ab.a; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-04 23:46:07 +01:00
check ( " struct AB { int a; int b; }; \n "
" \n "
" int f(struct AB *ab) { \n "
" ab->a = 1; \n "
" ab->b = 2; \n "
" ab++; \n "
" ab->a = 1; \n "
" ab->b = 2; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct AB { int a; int b; }; \n "
" \n "
" int f(struct AB *ab) { \n "
" ab->a = 1; \n "
" ab->b = 2; \n "
" ab = x; \n "
" ab->a = 1; \n "
" ab->b = 2; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-10 17:20:40 +01:00
2013-11-14 16:10:00 +01:00
check ( " void f(struct AB *ab) { \n " // #
" ab->data->x = 1; \n "
" ab = &ab1; \n "
" ab->data->x = 2; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-01 23:16:52 +02:00
// #5964
check ( " void func(char *buffer, const char *format, int precision, unsigned value) { \n "
" (precision < 0) ? sprintf(buffer, format, value) : sprintf(buffer, format, precision, value); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-11-10 17:20:40 +01:00
// don't crash
check ( " struct data { \n "
" struct { int i; } fc; \n "
" }; \n "
" struct state { \n "
" struct data d[123]; \n "
" }; \n "
" void func(struct state *s) { \n "
" s->foo[s->x++] = 2; \n "
" s->d[1].fc.i++; \n "
" } " ) ;
2015-02-22 12:03:53 +01:00
// #6525 - inline assembly
check ( " void f(int i) { \n "
" i = 1; \n "
" asm( \" foo \" ); \n "
" i = 1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-11-11 17:21:55 +01:00
// #6555
check ( " void foo() { \n "
" char *p = 0; \n "
" try { \n "
" p = fred(); \n "
" p = wilma(); \n "
" } \n "
" catch (...) { \n "
" barney(p); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() { \n "
" char *p = 0; \n "
" try { \n "
" p = fred(); \n "
" p = wilma(); \n "
" } \n "
" catch (...) { \n "
" barney(x); \n "
" } \n "
" } " ) ;
2016-01-15 22:01:40 +01:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:5]: (style) Variable 'p' is reassigned a value before the old one has been used. \n "
2015-11-11 17:21:55 +01:00
" [test.cpp:2]: (style) The scope of the variable 'p' can be reduced. \n " , errout . str ( ) ) ;
check ( " void foo() { \n "
" char *p = 0; \n "
" try { \n "
" if(z) { \n "
" p = fred(); \n "
" p = wilma(); \n "
" } \n "
" } \n "
" catch (...) { \n "
" barney(p); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-11-11 18:24:00 +01:00
// Member variable pointers
check ( " void podMemPtrs() { \n "
" int POD::*memptr; \n "
" memptr = &POD::a; \n "
" memptr = &POD::b; \n "
" if (memptr) \n "
" memptr = 0; \n "
" } " ) ;
2016-08-29 03:48:08 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style, inconclusive) Variable 'memptr' is reassigned a value before the old one has been used if variable is no semaphore variable. \n " , errout . str ( ) ) ;
2015-11-15 19:03:18 +01:00
}
2015-11-15 19:01:29 +01:00
2015-11-15 19:03:18 +01:00
void redundantVarAssignment_7133 ( ) {
2015-11-15 19:01:29 +01:00
// #7133
2015-11-15 19:03:18 +01:00
check ( " sal_Int32 impl_Export() { \n "
2015-11-15 19:01:29 +01:00
" try { \n "
" try { \n "
" uno::Sequence< uno::Any > aArgs(2); \n "
" beans::NamedValue aValue; \n "
" aValue.Name = \" DocumentHandler \" ; \n "
" aValue.Value <<= xDocHandler; \n "
" aArgs[0] <<= aValue; \n "
" aValue.Name = \" Model \" ; \n "
" aValue.Value <<= xDocumentComp; \n "
" aArgs[1] <<= aValue; \n "
" } \n "
" catch (const uno::Exception&) { \n "
" } \n "
" } \n "
" catch (const uno::Exception&) { \n "
" } \n "
2015-11-15 19:03:18 +01:00
" } " , " test.cpp " , false , true ) ;
2016-07-18 12:43:23 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-11-15 20:14:20 +01:00
check ( " void ConvertBitmapData(sal_uInt16 nDestBits) { \n "
2016-08-29 03:48:08 +02:00
" BitmapBuffer aSrcBuf; \n "
2015-11-15 20:14:20 +01:00
" aSrcBuf.mnBitCount = nSrcBits; \n "
" BitmapBuffer aDstBuf; \n "
" aSrcBuf.mnBitCount = nDestBits; \n "
" bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects ); \n "
" } " , " test.c " ) ;
2016-07-18 12:43:23 +02:00
ASSERT_EQUALS ( " [test.c:3] -> [test.c:5]: (style) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used. \n " , errout . str ( ) ) ;
2015-11-15 20:14:20 +01:00
check ( " void ConvertBitmapData(sal_uInt16 nDestBits) { \n "
2016-08-29 03:48:08 +02:00
" BitmapBuffer aSrcBuf; \n "
2015-11-15 20:14:20 +01:00
" aSrcBuf.mnBitCount = nSrcBits; \n "
" BitmapBuffer aDstBuf; \n "
" aSrcBuf.mnBitCount = nDestBits; \n "
" bConverted = ::ImplFastBitmapConversion( aDstBuf, aSrcBuf, aTwoRects ); \n "
" } " ) ;
2016-08-29 03:48:08 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:5]: (style, inconclusive) Variable 'aSrcBuf.mnBitCount' is reassigned a value before the old one has been used if variable is no semaphore variable. \n " ,
errout . str ( ) ) ;
2015-11-15 20:14:20 +01:00
2012-09-02 13:09:32 +02:00
}
2017-03-25 20:46:25 +01:00
void redundantVarAssignment_stackoverflow ( ) {
check ( " typedef struct message_node { \n "
" char code; \n "
" size_t size; \n "
" struct message_node *next, *prev; \n "
" } *message_list; \n "
" static message_list remove_message_from_list(message_list m) { \n "
" m->prev->next = m->next; \n "
" m->next->prev = m->prev; \n "
" return m->next; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void redundantMemWrite ( ) {
2012-09-02 13:09:32 +02:00
// Simple tests
2016-08-08 21:43:00 +02:00
check ( " void f() { \n "
" char a[10]; \n "
2012-09-02 13:09:32 +02:00
" memcpy(a, foo, bar); \n "
" memset(a, 0, bar); \n "
" } " ) ;
2016-08-08 21:43:00 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [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
2016-08-08 21:43:00 +02:00
check ( " void f() { \n "
" char a[10]; \n "
2012-09-02 13:09:32 +02:00
" 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 "
2016-08-08 21:43:00 +02:00
" char a[10]; \n "
" sprintf(a, \" foo \" ); \n "
2012-09-02 13:09:32 +02:00
" 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 ( ) ) ;
2016-08-08 21:43:00 +02:00
check ( " void f(char *filename) { \n "
" char *p = strrchr(filename,'.'); \n "
" strcpy(p, \" foo \" ); \n "
" dostuff(filename); \n "
" strcpy(p, \" foo \" ); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
// 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
2016-08-08 21:43:00 +02:00
check ( " void f() { \n "
" char a[10]; \n "
2012-09-02 13:09:32 +02:00
" strcpy(a, foo); \n "
" strcat(a, bar); \n " // Not redundant
" strcpy(a, x); \n " // Redundant
" } " ) ;
2016-08-08 21:43:00 +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
// Tests with function call between copy
2016-08-08 21:43:00 +02:00
check ( " void f() { \n "
" char a[10]; \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
" } " ) ;
2016-08-08 21:43:00 +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
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 "
2016-08-08 21:43:00 +02:00
" char a[10]; \n "
2012-09-02 13:09:32 +02:00
" memset(a, 0, size); \n "
" bar(); \n "
" memset(a, 0, size); \n "
" } " ) ;
2016-08-08 21:43:00 +02:00
TODO_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 ( ) ) ;
2014-04-27 11:00:47 +02:00
// #5689 - use return value of strcpy
check ( " int f(void* a) { \n "
" int i = atoi(strcpy(a, foo)); \n "
" strncpy(a, 0, bar); \n "
" return i; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-06-13 15:46:43 +02:00
// #7175 - read+write
check ( " void f() { \n "
" char buf[100]; \n "
" strcpy(buf, x); \n "
" strcpy(buf, dostuff(buf)); \n " // <- read + write
" strcpy(buf, x); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" char buf[100]; \n "
" strcpy(buf, x); \n "
" strcpy(buf, dostuff(buf)); \n "
" strcpy(buf, x); \n "
" } " ) ;
TODO_ASSERT_EQUALS ( " error " , " " , errout . str ( ) ) ;
2012-09-02 13:09:32 +02:00
}
2013-01-13 12:02:10 +01:00
2014-11-20 14:20:09 +01:00
void varFuncNullUB ( ) { // #4482
2013-01-13 12:02:10 +01:00
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
2014-11-20 14:20:09 +01:00
void checkPipeParameterSize ( ) { // #3521
2015-02-16 16:25:27 +01:00
checkposix ( " void f(){ \n "
" int pipefd[1]; \n " // <-- array of two integers is needed
" if (pipe(pipefd) == -1) { \n "
" return; \n "
" } \n "
" } " ) ;
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
2015-02-16 16:25:27 +01:00
checkposix ( " void f(){ \n "
" int pipefd[2]; \n "
" if (pipe(pipefd) == -1) { \n "
" return; \n "
" } \n "
" } " ) ;
2013-02-17 17:33:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-02-16 16:25:27 +01:00
checkposix ( " void f(){ \n "
" int pipefd[20]; \n "
" if (pipe(pipefd) == -1) { \n "
" return; \n "
" } \n "
" } " ) ;
2013-02-17 17:33:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-02-16 16:25:27 +01:00
checkposix ( " void f(){ \n "
" int pipefd[1]; \n " // <-- array of two integers is needed
" if (pipe2(pipefd,0) == -1) { \n "
" return; \n "
" } \n "
" } " ) ;
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
2015-02-16 16:25:27 +01:00
checkposix ( " void f(){ \n "
" int pipefd[2]; \n "
" if (pipe2(pipefd,0) == -1) { \n "
" return; \n "
" } \n "
" } " ) ;
2013-02-17 17:33:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-02-16 16:25:27 +01:00
checkposix ( " void f(){ \n "
" int pipefd[20]; \n "
" if (pipe2(pipefd,0) == -1) { \n "
" return; \n "
" } \n "
" } " ) ;
2013-02-17 17:33:32 +01:00
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-07-29 10:57:41 +02:00
// avoid crash with pointer variable - for local variable on stack as well - see #4801
2014-10-16 20:46:44 +02:00
check ( " void foo() { \n "
2013-07-29 10:57:41 +02:00
" int *cp; \n "
" if ( pipe (cp) == -1 ) { \n "
" return; \n "
" } \n "
2014-10-16 20:46:44 +02:00
" } " ) ;
2013-07-29 10:57:41 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// test with unknown variable
2014-10-16 20:46:44 +02:00
check ( " void foo() { \n "
2013-07-29 10:57:41 +02:00
" if ( pipe (cp) == -1 ) { \n "
" return; \n "
" } \n "
2014-10-16 20:46:44 +02:00
" } " ) ;
2013-07-29 10:57:41 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-11 14:22:22 +02:00
2013-08-22 21:33:02 +02:00
// avoid crash with pointer variable - for local variable on stack as well - see #4801
2014-10-16 20:46:44 +02:00
check ( " void foo() { \n "
2013-08-22 21:33:02 +02:00
" int *cp; \n "
" if ( pipe (cp) == -1 ) { \n "
" return; \n "
" } \n "
2014-10-16 20:46:44 +02:00
" } " ) ;
2013-08-22 21:33:02 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// test with unknown variable
2014-10-16 20:46:44 +02:00
check ( " void foo() { \n "
2013-08-22 21:33:02 +02:00
" if ( pipe (cp) == -1 ) { \n "
" return; \n "
" } \n "
2014-10-16 20:46:44 +02:00
" } " ) ;
2013-08-22 21:33:02 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-02-17 17:33:32 +01:00
}
2013-02-27 21:02:12 +01:00
2014-11-20 14:20:09 +01:00
void checkCastIntToCharAndBack ( ) { // #160
2013-02-27 21:02:12 +01:00
// 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
}
2014-11-20 14:20:09 +01:00
void checkCommaSeparatedReturn ( ) {
2013-06-15 17:49:10 +02:00
check ( " int fun(int a) { \n "
" if (a < 0) \n "
2013-06-18 00:13:45 +02:00
" return a++, \n "
" do_something(); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , true , false , false ) ;
2013-06-15 17:49:10 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Comma is used in return statement. The comma can easily be misread as a ';'. \n " , errout . str ( ) ) ;
check ( " int fun(int a) { \n "
" if (a < 0) \n "
2013-06-18 00:13:45 +02:00
" return a++, do_something(); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , true , false , false ) ;
2013-06-18 00:13:45 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int fun(int a) { \n "
" if (a < 0) \n "
" return a+5, \n "
" do_something(); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , true , false , false ) ;
2013-06-15 17:49:10 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Comma is used in return statement. The comma can easily be misread as a ';'. \n " , errout . str ( ) ) ;
2013-06-18 00:13:45 +02:00
check ( " int fun(int a) { \n "
" if (a < 0) \n "
" return a+5, do_something(); \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , true , false , false ) ;
2013-06-18 00:13:45 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int fun(int a) { \n "
" if (a < 0) \n "
2013-08-06 20:27:09 +02:00
" return c<int, \n int>::b; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , true , false , false ) ;
2013-06-18 00:13:45 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-07-28 10:39:58 +02:00
2013-08-06 20:27:09 +02:00
// #4943 take care of C++11 initializer lists
check ( " std::vector<Foo> Bar() { \n "
" return \n "
" { \n "
" { \" 1 \" }, \n "
" { \" 2 \" }, \n "
" { \" 3 \" } \n "
" }; \n "
2015-02-16 16:25:27 +01:00
" } " , nullptr , true , false , false ) ;
2013-08-06 20:27:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-06-15 17:49:10 +02:00
}
2013-09-26 07:07:48 +02:00
2018-07-25 07:44:06 +02:00
void checkPassByReference ( ) {
// #8570 passByValue when std::move is used
check ( " struct A \n "
" { \n "
" std::vector<int> x; \n "
" }; \n "
" \n "
" struct B \n "
" { \n "
" explicit B(A a) : a(std::move(a)) {} \n "
" void Init(A _a) { a = std::move(_a); } \n "
" A a; "
" }; " , nullptr , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A \n "
" { \n "
" std::vector<int> x; \n "
" }; \n "
" \n "
" struct B \n "
" { \n "
" explicit B(A a) : a{std::move(a)} {} \n "
" void Init(A _a) { a = std::move(_a); } \n "
" A a; "
" }; " , nullptr , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A \n "
" { \n "
" std::vector<int> x; \n "
" }; \n "
" \n "
" struct B \n "
" { \n "
" B(A a, A a2) : a{std::move(a)}, a2{std::move(a2)} {} \n "
" void Init(A _a) { a = std::move(_a); } \n "
" A a; "
" A a2; "
" }; " , nullptr , false , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A \n "
" { \n "
" std::vector<int> x; \n "
" }; \n "
" \n "
" struct B \n "
" { \n "
" B(A a, A a2) : a{std::move(a)}, a2{a2} {} \n "
" void Init(A _a) { a = std::move(_a); } \n "
" A a; "
" A a2; "
" }; " , nullptr , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference. \n " , errout . str ( ) ) ;
check ( " struct A \n "
" { \n "
" std::vector<int> x; \n "
" }; \n "
" \n "
" struct B \n "
" { \n "
" B(A a, A a2) : a{std::move(a)}, a2(a2) {} \n "
" void Init(A _a) { a = std::move(_a); } \n "
" A a; "
" A a2; "
" }; " , nullptr , false , false , true ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference. \n " , errout . str ( ) ) ;
}
2018-07-22 15:01:18 +02:00
2014-11-20 14:20:09 +01:00
void checkComparisonFunctionIsAlwaysTrueOrFalse ( ) {
2013-09-26 07:07:48 +02:00
// positive test
check ( " bool f(int x){ \n "
" return isless(x,x); \n "
" } " ) ;
2015-08-11 13:17:38 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of two identical variables with isless(x,x) always evaluates to false. \n " , errout . str ( ) ) ;
2013-09-26 07:07:48 +02:00
check ( " bool f(int x){ \n "
" return isgreater(x,x); \n "
" } " ) ;
2015-08-11 13:17:38 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of two identical variables with isgreater(x,x) always evaluates to false. \n " , errout . str ( ) ) ;
2013-09-26 07:07:48 +02:00
check ( " bool f(int x){ \n "
" return islessgreater(x,x); \n "
" } " ) ;
2015-08-11 13:17:38 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of two identical variables with islessgreater(x,x) always evaluates to false. \n " , errout . str ( ) ) ;
2013-09-26 07:07:48 +02:00
check ( " bool f(int x){ \n "
" return islessequal(x,x); \n "
" } " ) ;
2015-08-11 13:17:38 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of two identical variables with islessequal(x,x) always evaluates to true. \n " , errout . str ( ) ) ;
2013-09-26 07:07:48 +02:00
check ( " bool f(int x){ \n "
" return isgreaterequal(x,x); \n "
" } " ) ;
2015-08-11 13:17:38 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of two identical variables with isgreaterequal(x,x) always evaluates to true. \n " , errout . str ( ) ) ;
2013-09-26 07:07:48 +02:00
// no warning should be reported for
check ( " bool f(int x, int y){ \n "
" return isgreaterequal(x,y) && islessequal(x,y) && islessgreater(x,y) && isgreater(x,y) && isless(x,y); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-06-05 16:41:10 +02:00
2014-11-20 14:20:09 +01:00
void integerOverflow ( ) { // 5895
2014-06-05 16:41:10 +02:00
// no signed integer overflow should happen
2014-06-07 13:16:02 +02:00
check ( " void f(unsigned long long ull) { \n "
" if (ull == 0x89504e470d0a1a0a || ull == 0x8a4d4e470d0a1a0a) ; \n "
2014-06-05 16:41:10 +02:00
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-09-27 21:31:04 +02:00
2015-01-17 14:33:14 +01:00
void redundantPointerOp ( ) {
check ( " int *f(int *x) { \n "
" return &*x; \n "
" } \n " , nullptr , false , true ) ;
2016-09-16 22:18:24 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant pointer operation on 'x' - it's already a pointer. \n " , errout . str ( ) ) ;
2015-01-17 14:33:14 +01:00
check ( " int *f(int *y) { \n "
" return &(*y); \n "
" } \n " , nullptr , false , true ) ;
2016-09-16 22:18:24 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant pointer operation on 'y' - it's already a pointer. \n " , errout . str ( ) ) ;
2015-01-17 14:33:14 +01:00
// no warning for bitwise AND
check ( " void f(int *b) { \n "
" int x = 0x20 & *b; \n "
" } \n " , nullptr , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// No message for double pointers to structs
check ( " void f(struct foo **my_struct) { \n "
" char **pass_to_func = &(*my_struct)->buf; \n "
" } \n " , nullptr , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// another double pointer to struct - with an array
check ( " void f(struct foo **my_struct) { \n "
" char **pass_to_func = &(*my_struct)->buf[10]; \n "
" } \n " , nullptr , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// double pointer to array
check ( " void f(char **ptr) { \n "
" int *x = &(*ptr)[10]; \n "
" } \n " , nullptr , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// function calls
check ( " void f(Mutex *mut) { \n "
" pthread_mutex_lock(&*mut); \n "
" } \n " , nullptr , false , false ) ;
2016-09-16 22:18:24 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant pointer operation on 'mut' - it's already a pointer. \n " , errout . str ( ) ) ;
2015-01-17 14:33:14 +01:00
// make sure we got the AST match for "(" right
check ( " void f(char *ptr) { \n "
" if (&*ptr == NULL) \n "
" return; \n "
" } \n " , nullptr , false , true ) ;
2016-09-16 22:18:24 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Redundant pointer operation on 'ptr' - it's already a pointer. \n " , errout . str ( ) ) ;
2015-01-17 14:33:14 +01:00
// no warning for macros
check ( " #define MUTEX_LOCK(m) pthread_mutex_lock(&(m)) \n "
" void f(struct mutex *mut) { \n "
" MUTEX_LOCK(*mut); \n "
" } \n " , nullptr , false , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-05-25 08:20:14 +02:00
}
void test_isSameExpression ( ) { // see #5738
check ( " bool isInUnoIncludeFile(StringRef name) { "
" return name.startswith(SRCDIR \" /com/ \" ) || name.startswith(SRCDIR \" /uno/ \" ); \n "
" }; " , " test.cpp " , false , false ) ;
2018-07-08 15:58:04 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-01-17 14:33:14 +01:00
}
2015-08-05 11:20:28 +02:00
void raceAfterInterlockedDecrement ( ) {
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" whatever(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (counter) \n "
" return; \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (!counter) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (counter > 0) \n "
" return; \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (0 < counter) \n "
" return; \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (counter == 0) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (0 == counter) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (0 != counter) \n "
" return; \n "
" destroy() \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (counter != 0) \n "
" return; \n "
" destroy() \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (counter <= 0) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" InterlockedDecrement(&counter); \n "
" if (0 >= counter) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (newCount) \n "
" return; \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (!newCount) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (newCount > 0) \n "
" return; \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (0 < newCount) \n "
" return; \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (newCount == 0) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (0 == newCount) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (0 != newCount) \n "
" return; \n "
" destroy() \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (newCount != 0) \n "
" return; \n "
" destroy() \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (newCount <= 0) \n "
" destroy(); \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
checkInterlockedDecrement (
2015-08-07 20:27:32 +02:00
" void f() { \n "
" int counter = 0; \n "
" int newCount = InterlockedDecrement(&counter); \n "
" if (0 >= newCount) \n "
" destroy; \n "
" } \n " ) ;
2015-08-05 11:20:28 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-12-02 08:58:57 +01:00
checkInterlockedDecrement (
" int f() { \n "
" int counter = 0; \n "
" if (InterlockedDecrement(&counter) == 0) { \n "
" destroy(); \n "
" return 0; \n "
" } else { \n "
" return counter; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
" int f() { \n "
" int counter = 0; \n "
" if (::InterlockedDecrement(&counter) == 0) { \n "
" destroy(); \n "
" return 0; \n "
" } else { \n "
" return counter; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
" int f() { \n "
" int counter = 0; \n "
" if (InterlockedDecrement(&counter) == 0) { \n "
" destroy(); \n "
" return 0; \n "
" } \n "
" return counter; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
" int f() { \n "
" int counter = 0; \n "
" if (::InterlockedDecrement(&counter) == 0) { \n "
" destroy(); \n "
" return 0; \n "
" } \n "
" return counter; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
" int f() { \n "
" int counter = 0; \n "
" if (InterlockedDecrement(&counter) == 0) { \n "
" destroy(); \n "
" return 0; \n "
" } else \n "
" return counter; \n "
" \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
checkInterlockedDecrement (
" int f() { \n "
" int counter = 0; \n "
" if (::InterlockedDecrement(&counter) == 0) { \n "
" destroy(); \n "
" return 0; \n "
" } else \n "
" return counter; \n "
" \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Race condition: non-interlocked access after InterlockedDecrement(). Use InterlockedDecrement() return value instead. \n " , errout . str ( ) ) ;
2015-08-05 11:20:28 +02:00
}
2015-08-30 16:45:47 +02:00
void testUnusedLabel ( ) {
check ( " void f() { \n "
" label: \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Label 'label' is not used. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" label: \n "
" foo(); \n "
" goto label; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" label: \n "
" foo(); \n "
" goto label; \n "
" } \n "
" void g() { \n "
" label: \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (style) Label 'label' is not used. \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" switch(a) { \n "
" default: \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" class X { \n "
" protected: \n "
" }; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" class X { \n "
" my_protected: \n "
" }; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-01-31 12:04:28 +01:00
check ( " int test(char art) { \n "
" switch (art) { \n "
" caseZERO: \n "
" return 0; \n "
" case1: \n "
" return 1; \n "
" case 2: \n "
" return 2; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Label 'caseZERO' is not used. Should this be a 'case' of the enclosing switch()? \n "
" [test.cpp:5]: (warning) Label 'case1' is not used. Should this be a 'case' of the enclosing switch()? \n " , errout . str ( ) ) ;
2016-11-20 14:43:54 +01:00
check ( " int test(char art) { \n "
" switch (art) { \n "
" case 2: \n "
" return 2; \n "
" } \n "
" label: \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (style) Label 'label' is not used. \n " , errout . str ( ) ) ;
2015-08-30 16:45:47 +02:00
}
2015-12-24 09:13:20 +01:00
void testEvaluationOrder ( ) {
check ( " void f() { \n "
" int x = dostuff(); \n "
" return x + x++; \n "
2015-12-25 19:01:32 +01:00
" } " , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:3]: (error) Expression 'x+x++' depends on order of evaluation of side effects \n " , errout . str ( ) ) ;
2015-12-24 09:13:20 +01:00
// #7226
check ( " long int f1(const char *exp) { \n "
" return strtol(++exp, (char **)&exp, 10); \n "
2015-12-25 19:01:32 +01:00
" } " , " test.c " ) ;
2015-12-26 15:48:43 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " long int f1(const char *exp) { \n "
" return dostuff(++exp, exp, 10); \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:2]: (error) Expression '++exp,exp' depends on order of evaluation of side effects \n " , errout . str ( ) ) ;
2016-01-18 16:10:51 +01:00
check ( " void f() { \n "
" int a; \n "
" while (a=x(), a==123) {} \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-09-02 08:28:53 +02:00
// # 8717
check ( " void f(int argc, char *const argv[]) { \n "
" char **local_argv = safe_malloc(sizeof (*local_argv)); \n "
" int local_argc = 0; \n "
" local_argv[local_argc++] = argv[0]; \n "
" } \n " , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-12-28 14:04:14 +01:00
}
2015-12-25 16:45:11 +01:00
2015-12-28 14:04:14 +01:00
void testEvaluationOrderSelfAssignment ( ) {
2015-12-25 18:00:19 +01:00
// self assignment
check ( " void f() { \n "
" int x = x = y + 1; \n "
2015-12-25 19:01:32 +01:00
" } " , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:2]: (warning) Redundant assignment of 'x' to itself. \n " , errout . str ( ) ) ;
2015-12-28 14:04:14 +01:00
}
2015-12-25 18:00:19 +01:00
2015-12-28 14:04:14 +01:00
void testEvaluationOrderMacro ( ) {
2016-11-27 11:40:42 +01:00
// macro, don't bailout (#7233)
2017-05-18 21:52:31 +02:00
checkP ( " #define X x \n "
" void f(int x) { \n "
" return x + X++; \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:3]: (error) Expression 'x+x++' depends on order of evaluation of side effects \n " , errout . str ( ) ) ;
2015-12-28 14:04:14 +01:00
}
2015-12-25 18:31:21 +01:00
2015-12-28 14:04:14 +01:00
void testEvaluationOrderSequencePointsFunctionCall ( ) {
// FP
check ( " void f(int id) { \n "
" id = dostuff(id += 42); \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-12-25 17:48:15 +01:00
2015-12-28 14:04:14 +01:00
// FN
check ( " void f(int id) { \n "
" id = id + dostuff(id += 42); \n "
" } " , " test.c " ) ;
TODO_ASSERT_EQUALS ( " error " , " " , errout . str ( ) ) ;
}
2015-12-27 14:08:16 +01:00
2015-12-28 14:04:14 +01:00
void testEvaluationOrderSequencePointsComma ( ) {
2015-12-28 13:45:55 +01:00
check ( " int f(void) { \n "
" int t; \n "
" return (unsigned char)(t=1,t^c); \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(void) { \n "
" int t; \n "
" dostuff(t=1,t^c); \n "
" } " , " test.c " ) ;
2015-12-28 13:58:41 +01:00
ASSERT_EQUALS ( " [test.c:3]: (error) Expression 't=1,t^c' depends on order of evaluation of side effects \n " , errout . str ( ) ) ;
2015-12-29 12:06:42 +01:00
check ( " void f(void) { \n "
" int t; \n "
" dostuff((t=1,t),2); \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-12-28 14:04:14 +01:00
}
2015-12-28 13:45:55 +01:00
2015-12-28 14:04:14 +01:00
void testEvaluationOrderSizeof ( ) {
2015-12-27 14:08:16 +01:00
check ( " void f(char *buf) { \n "
" dostuff(buf++, sizeof(*buf)); "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-12-24 09:13:20 +01:00
}
2016-09-19 06:13:09 +02:00
void testUnsignedLessThanZero ( ) {
check ( " struct d { \n "
" unsigned n; \n "
" }; \n "
" void f(void) { \n "
" struct d d; \n "
" d.n = 3; \n "
" \n "
" if (d.n < 0) { \n "
" return; \n "
" } \n "
" \n "
" if (0 > d.n) { \n "
" return; \n "
" } \n "
" } " , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:8]: (style) Checking if unsigned variable 'd.n' is less than zero. \n "
" [test.c:12]: (style) Checking if unsigned variable 'd.n' is less than zero. \n " ,
errout . str ( ) ) ;
}
2016-11-20 15:14:49 +01:00
void doubleMove1 ( ) {
check ( " void g(A a); \n "
" void f() { \n "
" A a; \n "
" g(std::move(a)); \n "
" g(std::move(a)); \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void doubleMoveMemberInitialization1 ( ) {
check ( " class A \n "
" { \n "
" A(B && b) \n "
" :b1(std::move(b)) \n "
" { \n "
" b2 = std::move(b); \n "
" } \n "
" B b1; \n "
" B b2; \n "
" }; " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:6]: (warning) Access of moved variable 'b'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void doubleMoveMemberInitialization2 ( ) {
check ( " class A \n "
" { \n "
" A(B && b) \n "
" :b1(std::move(b)), \n "
" b2(std::move(b)) \n "
" {} \n "
" B b1; \n "
" B b2; \n "
" }; " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Access of moved variable 'b'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void moveAndAssign1 ( ) {
check ( " A g(A a); \n "
" void f() { \n "
" A a; \n "
" a = g(std::move(a)); \n "
" a = g(std::move(a)); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void moveAndAssign2 ( ) {
check ( " A g(A a); \n "
" void f() { \n "
" A a; \n "
" B b = g(std::move(a)); \n "
" C c = g(std::move(a)); \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void moveAssignMoveAssign ( ) {
check ( " void h(A a); \n "
" void f() { "
" A a; \n "
" g(std::move(a)); \n "
" h(a); \n "
" a = b; \n "
" h(a); \n "
" g(std::move(a)); \n "
" h(a); \n "
" a = b; \n "
" h(a); \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Access of moved variable 'a'. \n "
" [test.cpp:8]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
2016-12-11 22:32:57 +01:00
void moveAndReset1 ( ) {
check ( " A g(A a); \n "
" void f() { \n "
" A a; \n "
" a.reset(g(std::move(a))); \n "
" a.reset(g(std::move(a))); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void moveAndReset2 ( ) {
check ( " A g(A a); \n "
" void f() { \n "
" A a; \n "
" A b; \n "
" A c; \n "
" b.reset(g(std::move(a))); \n "
" c.reset(g(std::move(a))); \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-12-11 22:32:57 +01:00
}
void moveResetMoveReset ( ) {
check ( " void h(A a); \n "
" void f() { "
" A a; \n "
" g(std::move(a)); \n "
" h(a); \n "
" a.reset(b); \n "
" h(a); \n "
" g(std::move(a)); \n "
" h(a); \n "
" a.reset(b); \n "
" h(a); \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Access of moved variable 'a'. \n "
" [test.cpp:8]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-12-11 22:32:57 +01:00
}
2016-11-20 15:14:49 +01:00
void moveAndFunctionParameter ( ) {
check ( " void g(A a); \n "
" void f() { \n "
" A a; \n "
" A b = std::move(a); \n "
" g(a); \n "
" A c = a; \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Access of moved variable 'a'. \n "
" [test.cpp:6]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void moveAndFunctionParameterReference ( ) {
check ( " void g(A & a); \n "
" void f() { \n "
" A a; \n "
" A b = std::move(a); \n "
" g(a); \n "
" A c = a; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void moveAndFunctionParameterConstReference ( ) {
check ( " void g(A const & a); \n "
" void f() { \n "
" A a; \n "
" A b = std::move(a); \n "
" g(a); \n "
" A c = a; \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Access of moved variable 'a'. \n "
" [test.cpp:6]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void moveAndFunctionParameterUnknown ( ) {
check ( " void f() { \n "
" A a; \n "
" A b = std::move(a); \n "
" g(a); \n "
" A c = a; \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning, inconclusive) Access of moved variable 'a'. \n "
" [test.cpp:5]: (warning, inconclusive) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void moveAndReturn ( ) {
check ( " int f(int i) { \n "
" A a; \n "
" A b; \n "
" g(std::move(a)); \n "
" if (i) \n "
" return g(std::move(b)); \n "
" return h(std::move(a),std::move(b)); \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Access of moved variable 'a'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void moveAndClear ( ) {
check ( " void f() { \n "
" V v; \n "
" g(std::move(v)); \n "
" v.clear(); \n "
" if (v.empty()) {} \n "
" } " ) ;
2016-12-12 14:29:27 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void movedPointer ( ) {
check ( " void f() { \n "
" P p; \n "
" g(std::move(p)); \n "
" x = p->x; \n "
" y = p->y; \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Access of moved variable 'p'. \n "
" [test.cpp:5]: (warning) Access of moved variable 'p'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
void partiallyMoved ( ) {
check ( " void f() { \n "
" A a; \n "
" gx(std::move(a).x()); \n "
" gy(std::move(a).y()); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void moveAndLambda ( ) {
check ( " void f() { \n "
" A a; \n "
" auto h = [a=std::move(a)](){return g(std::move(a));}; "
" b = a; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void forwardAndUsed ( ) {
check ( " template<typename T> \n "
" void f(T && t) { \n "
" g(std::forward<T>(t)); \n "
" T s = t; \n "
" } " ) ;
2018-01-07 10:57:19 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Access of forwarded variable 't'. \n " , errout . str ( ) ) ;
2016-11-20 15:14:49 +01:00
}
2017-01-05 08:52:11 +01:00
void funcArgNamesDifferent ( ) {
check ( " void func1(int a, int b, int c); \n "
" void func1(int a, int b, int c) { } \n "
" void func2(int a, int b, int c); \n "
" void func2(int A, int B, int C) { } \n "
" class Fred { \n "
" void func1(int a, int b, int c); \n "
" void func2(int a, int b, int c); \n "
" void func3(int a = 0, int b = 0, int c = 0); \n "
" void func4(int a = 0, int b = 0, int c = 0); \n "
" }; \n "
" void Fred::func1(int a, int b, int c) { } \n "
" void Fred::func2(int A, int B, int C) { } \n "
" void Fred::func3(int a, int b, int c) { } \n "
" void Fred::func4(int A, int B, int C) { } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (style, inconclusive) Function 'func2' argument 1 names different: declaration 'a' definition 'A'. \n "
" [test.cpp:3] -> [test.cpp:4]: (style, inconclusive) Function 'func2' argument 2 names different: declaration 'b' definition 'B'. \n "
" [test.cpp:3] -> [test.cpp:4]: (style, inconclusive) Function 'func2' argument 3 names different: declaration 'c' definition 'C'. \n "
" [test.cpp:7] -> [test.cpp:12]: (style, inconclusive) Function 'func2' argument 1 names different: declaration 'a' definition 'A'. \n "
" [test.cpp:7] -> [test.cpp:12]: (style, inconclusive) Function 'func2' argument 2 names different: declaration 'b' definition 'B'. \n "
" [test.cpp:7] -> [test.cpp:12]: (style, inconclusive) Function 'func2' argument 3 names different: declaration 'c' definition 'C'. \n "
" [test.cpp:9] -> [test.cpp:14]: (style, inconclusive) Function 'func4' argument 1 names different: declaration 'a' definition 'A'. \n "
" [test.cpp:9] -> [test.cpp:14]: (style, inconclusive) Function 'func4' argument 2 names different: declaration 'b' definition 'B'. \n "
" [test.cpp:9] -> [test.cpp:14]: (style, inconclusive) Function 'func4' argument 3 names different: declaration 'c' definition 'C'. \n " , errout . str ( ) ) ;
}
void funcArgOrderDifferent ( ) {
check ( " void func1(int a, int b, int c); \n "
" void func1(int a, int b, int c) { } \n "
" void func2(int a, int b, int c); \n "
" void func2(int c, int b, int a) { } \n "
" void func3(int, int b, int c); \n "
" void func3(int c, int b, int a) { } \n "
" class Fred { \n "
" void func1(int a, int b, int c); \n "
" void func2(int a, int b, int c); \n "
" void func3(int a = 0, int b = 0, int c = 0); \n "
" void func4(int, int b = 0, int c = 0); \n "
" }; \n "
" void Fred::func1(int a, int b, int c) { } \n "
" void Fred::func2(int c, int b, int a) { } \n "
" void Fred::func3(int c, int b, int a) { } \n "
" void Fred::func4(int c, int b, int a) { } \n " ,
nullptr , false , false ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (warning) Function 'func2' argument order different: declaration 'a, b, c' definition 'c, b, a' \n "
" [test.cpp:5] -> [test.cpp:6]: (warning) Function 'func3' argument order different: declaration ', b, c' definition 'c, b, a' \n "
" [test.cpp:9] -> [test.cpp:14]: (warning) Function 'func2' argument order different: declaration 'a, b, c' definition 'c, b, a' \n "
" [test.cpp:10] -> [test.cpp:15]: (warning) Function 'func3' argument order different: declaration 'a, b, c' definition 'c, b, a' \n "
" [test.cpp:11] -> [test.cpp:16]: (warning) Function 'func4' argument order different: declaration ', b, c' definition 'c, b, a' \n " , errout . str ( ) ) ;
}
2017-01-23 12:50:36 +01:00
2017-01-31 16:22:31 +01:00
// #7846 - Syntax error when using C++11 braced-initializer in default argument
2017-01-23 12:50:36 +01:00
void cpp11FunctionArgInit ( ) {
// syntax error is not expected
ASSERT_NO_THROW ( check (
2017-01-31 16:22:31 +01:00
" \n void foo(int declaration = {}) { "
" \n for (int i = 0; i < 10; i++) {} "
" \n } "
" \n "
) ) ;
2017-01-23 12:50:36 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2008-12-19 22:15:18 +01:00
} ;
2013-05-09 15:39:33 +02:00
REGISTER_TEST ( TestOther )