2013-04-10 18:25:50 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2018-06-10 22:07:21 +02:00
* Copyright ( C ) 2007 - 2018 Cppcheck team .
2013-04-10 18:25:50 +02: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
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2017-05-27 04:33:47 +02:00
2013-04-10 18:25:50 +02:00
# include "checkbool.h"
2017-05-27 04:33:47 +02:00
# include "settings.h"
2013-04-10 18:25:50 +02:00
# include "testsuite.h"
2017-05-27 04:33:47 +02:00
# include "tokenize.h"
2013-04-10 18:25:50 +02:00
class TestBool : public TestFixture {
public :
2014-11-20 14:20:09 +01:00
TestBool ( ) : TestFixture ( " TestBool " ) {
2013-08-07 16:27:37 +02:00
}
2013-04-10 18:25:50 +02:00
private :
2015-10-07 18:33:57 +02:00
Settings settings ;
2013-04-10 18:25:50 +02:00
2018-05-15 16:37:40 +02:00
void run ( ) override {
2015-10-07 18:33:57 +02:00
settings . addEnabled ( " style " ) ;
settings . addEnabled ( " warning " ) ;
settings . inconclusive = true ;
2013-04-10 18:25:50 +02:00
TEST_CASE ( bitwiseOnBoolean ) ; // if (bool & bool)
TEST_CASE ( incrementBoolean ) ;
TEST_CASE ( assignBoolToPointer ) ;
2014-05-24 18:35:49 +02:00
TEST_CASE ( assignBoolToFloat ) ;
2013-04-10 18:25:50 +02:00
TEST_CASE ( comparisonOfBoolExpressionWithInt1 ) ;
TEST_CASE ( comparisonOfBoolExpressionWithInt2 ) ;
TEST_CASE ( comparisonOfBoolExpressionWithInt3 ) ;
2013-09-07 11:32:11 +02:00
TEST_CASE ( comparisonOfBoolExpressionWithInt4 ) ;
2013-04-10 18:25:50 +02:00
TEST_CASE ( comparisonOfBoolWithInt1 ) ;
TEST_CASE ( comparisonOfBoolWithInt2 ) ;
TEST_CASE ( comparisonOfBoolWithInt3 ) ;
TEST_CASE ( comparisonOfBoolWithInt4 ) ;
TEST_CASE ( comparisonOfBoolWithInt5 ) ;
TEST_CASE ( comparisonOfBoolWithInt6 ) ; // #4224 - integer is casted to bool
2013-08-21 16:17:19 +02:00
TEST_CASE ( comparisonOfBoolWithInt7 ) ; // #4846 - (!x == true)
2013-04-10 18:25:50 +02:00
TEST_CASE ( checkComparisonOfFuncReturningBool1 ) ;
TEST_CASE ( checkComparisonOfFuncReturningBool2 ) ;
TEST_CASE ( checkComparisonOfFuncReturningBool3 ) ;
TEST_CASE ( checkComparisonOfFuncReturningBool4 ) ;
TEST_CASE ( checkComparisonOfFuncReturningBool5 ) ;
TEST_CASE ( checkComparisonOfFuncReturningBool6 ) ;
2014-01-06 17:09:21 +01:00
TEST_CASE ( checkComparisonOfBoolWithBool ) ;
2013-12-23 18:39:05 +01:00
// Converting pointer addition result to bool
TEST_CASE ( pointerArithBool1 ) ;
2013-04-10 18:25:50 +02:00
}
2014-11-20 14:20:09 +01:00
void check ( const char code [ ] , bool experimental = false , const char filename [ ] = " test.cpp " ) {
2013-04-10 18:25:50 +02:00
// Clear the error buffer..
errout . str ( " " ) ;
settings . experimental = experimental ;
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
2013-10-07 18:41:07 +02:00
tokenizer . tokenize ( istr , filename ) ;
2013-04-10 18:25:50 +02:00
// Check...
CheckBool checkBool ( & tokenizer , & settings , this ) ;
checkBool . runChecks ( & tokenizer , & settings , this ) ;
2013-12-30 17:45:28 +01:00
tokenizer . simplifyTokenList2 ( ) ;
2013-04-10 18:25:50 +02:00
checkBool . runSimplifiedChecks ( & tokenizer , & settings , this ) ;
}
2014-11-20 14:20:09 +01:00
void assignBoolToPointer ( ) {
2013-04-10 18:25:50 +02:00
check ( " void foo(bool *p) { \n "
" p = false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
2013-09-26 01:03:34 +02:00
2014-03-27 05:39:48 +01:00
check ( " void foo(bool *p) { \n "
" p = (x<y); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
check ( " void foo(bool *p) { \n "
" p = (x||y); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
check ( " void foo(bool *p) { \n "
" p = (x&&y); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
2013-09-26 01:03:34 +02:00
// check against potential false positives
check ( " void foo(bool *p) { \n "
" *p = false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// ticket #5046 - false positive: Boolean value assigned to pointer
check ( " struct S { \n "
" bool *p; \n "
" }; \n "
" void f() { \n "
2013-10-05 08:53:37 +02:00
" S s = {0}; \n "
2013-09-26 01:03:34 +02:00
" *s.p = true; \n "
" } \n " ) ;
2013-10-05 08:53:37 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct S { \n "
" bool *p; \n "
" }; \n "
" void f() { \n "
" S s = {0}; \n "
" s.p = true; \n "
" } \n " ) ;
2013-10-29 16:16:52 +01:00
ASSERT_EQUALS ( " [test.cpp:6]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
2014-03-31 15:55:54 +02:00
// ticket #5627 - false positive: template
check ( " void f() { \n "
" X *p = new ::std::pair<int,int>[rSize]; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-05-30 22:57:54 +02:00
2015-05-31 22:40:13 +02:00
// ticket #6588 (c mode)
check ( " struct MpegEncContext { int *q_intra_matrix, *q_chroma_intra_matrix; }; \n "
" void dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int n, int qscale) { \n "
" const int *qmat = n < 4; \n " /* KO */
" const int *rmat = n < 4 ? " /* OK */
" ctx->q_intra_matrix : "
" ctx->q_chroma_intra_matrix; \n "
" } " , /*experimental=*/ false , " test.c " ) ;
ASSERT_EQUALS ( " [test.c:3]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
// ticket #6588 (c++ mode)
2015-05-30 22:57:54 +02:00
check ( " struct MpegEncContext { int *q_intra_matrix, *q_chroma_intra_matrix; }; \n "
" void dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int n, int qscale) { \n "
" const int *qmat = n < 4; \n " /* KO */
" const int *rmat = n < 4 ? " /* OK */
" ctx->q_intra_matrix : "
" ctx->q_chroma_intra_matrix; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
2015-06-05 23:44:03 +02:00
// ticket #6665
check ( " void pivot_big(char *first, int compare(const void *, const void *)) { \n "
" char *a = first, *b = first + 1, *c = first + 2; \n "
" char* m1 = compare(a, b) < 0 \n "
" ? (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a)) \n "
" : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b)); \n "
" } " , /*experimental=*/ false , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-02-05 20:22:30 +01:00
// #7381
check ( " void foo(bool *p, bool b) { \n "
" p = b; \n "
" p = &b; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Boolean value assigned to pointer. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
2014-11-20 14:20:09 +01:00
void assignBoolToFloat ( ) {
2014-05-24 18:35:49 +02:00
check ( " void foo1() { \n "
" double d = false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Boolean value assigned to floating point variable. \n " , errout . str ( ) ) ;
check ( " void foo2() { \n "
" float d = true; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Boolean value assigned to floating point variable. \n " , errout . str ( ) ) ;
check ( " void foo3() { \n "
" long double d = (2>1); \n "
" } " ) ;
2014-05-24 20:02:12 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style) Boolean value assigned to floating point variable. \n " , errout . str ( ) ) ;
2014-05-24 19:15:43 +02:00
// stability - don't crash:
check ( " void foo4() { \n "
" unknown = false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-02-05 19:46:47 +01:00
check ( " struct S { \n "
" float p; \n "
" }; \n "
" void f() { \n "
" S s = {0}; \n "
" s.p = true; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (style) Boolean value assigned to floating point variable. \n " , errout . str ( ) ) ;
2014-05-24 18:35:49 +02:00
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolExpressionWithInt1 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(int x) { \n "
" if ((x && 0x0f)==6) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x && 0x0f)==0) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x || 0x0f)==6) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x || 0x0f)==0) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x & 0x0f)==6) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x | 0x0f)==6) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x)==3) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x)==3 || (8 && x)==9) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x)!=3) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x) > 3) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x) > 0) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x) < 0) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x) < 1) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x) > 1) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (0 < (5 && x)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (0 > (5 && x)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (1 > (5 && x)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (1 < (5 && x)) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( x > false ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( false < x ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( x < false ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( false > x ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( x >= false ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( false >= x ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( x <= false ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " void f(bool x ) { \n "
" if ( false <= x ) \n "
" a++; \n "
" } \n "
) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
check ( " typedef int (*func)(bool invert); \n "
" void x(int, func f); \n "
" void foo(int error) { \n "
" if (error == ABC) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-07 16:16:07 +02:00
2013-10-07 16:37:51 +02:00
check ( " int f() { return !a+b<c; } " ) ; // #5072
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() { return (!a+b<c); } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-07 18:41:07 +02:00
{
const char code [ ] = " void f(int x, bool y) { if ( x != y ) {} } " ;
check ( code , false , " test.cpp " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (warning) Comparison of a boolean expression with an integer. \n " , errout . str ( ) ) ;
2013-10-07 18:41:07 +02:00
check ( code , false , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2013-10-09 05:43:50 +02:00
check ( " int f() { return (a+(b<5)<=c); } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolExpressionWithInt2 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(int x) { \n "
" if (!x == 10) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (!x != 10) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (x != 10) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (10 == !x) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (10 != !x) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
check ( " void f(int x, int y) { \n "
" if (y != !x) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
2013-10-07 17:44:19 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(int x, bool y) { \n "
" if (y != !x) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (10 != x) { \n "
" printf( \" x not equal to 10 \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-09-30 20:21:28 +02:00
check ( " void f(int x, int y) { \n "
" return (!y == !x); \n "
" } " ) ;
2013-10-05 08:33:33 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-06 10:39:08 +02:00
check ( " int f(int a) { \n "
" return (x()+1 == !a); \n "
" } " ) ;
TODO_ASSERT_EQUALS ( " error " , " " , errout . str ( ) ) ;
2013-10-07 16:37:51 +02:00
check ( " void f() { if (!!a+!!b+!!c>1){} } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-07 17:44:19 +02:00
check ( " void f(int a, int b, int c) { if (a != !b || c) {} } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-07 18:01:08 +02:00
check ( " void f(int a, int b, int c) { if (1 < !!a + !!b + !!c) {} } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int a, int b, int c) { if (1 < !(a+b)) {} } " ) ;
2014-03-27 16:10:43 +01:00
ASSERT_EQUALS ( " [test.cpp:1]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolExpressionWithInt3 ( ) {
2013-04-10 18:25:50 +02:00
check ( " int f(int x) { \n "
" return t<0>() && x; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolExpressionWithInt4 ( ) {
2013-09-07 11:32:11 +02:00
// #5016
check ( " void f() { \n "
" for(int i = 4; i > -1 < 5 ; --i) {} \n "
" } " ) ;
2014-03-27 16:10:43 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-09-07 18:24:35 +02:00
2013-10-06 10:39:08 +02:00
check ( " void f(int a, int b, int c) { \n "
" return (a > b) < c; \n "
" } " ) ;
2014-03-27 16:10:43 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer. \n " , errout . str ( ) ) ;
2013-10-06 10:39:08 +02:00
check ( " void f(int a, int b, int c) { \n "
" return x(a > b) < c; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int a, int b, int c) { \n "
" return a > b == c; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-09-07 18:24:35 +02:00
// templates
check ( " struct Tokenizer { TokenList list; }; \n "
" void Tokenizer::f() { \n "
" std::list<Token*> locationList; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-10-06 10:39:08 +02:00
// #5063 - or
check ( " void f() { \n "
" return a > b or c < d; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-02-19 21:38:54 +01:00
check ( " int f() { \n "
" return (a < b) != 0U; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() { \n "
" return (a < b) != 0x0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() { \n "
" return (a < b) != 42U; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-09-07 11:32:11 +02:00
}
2014-11-20 14:20:09 +01:00
void checkComparisonOfFuncReturningBool1 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(){ \n "
" int temp = 4; \n "
" if(compare1(temp) > compare2(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" bool compare1(int temp){ \n "
" if(temp==4){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } \n "
" bool compare2(int temp){ \n "
" if(temp==4){ \n "
" return false; \n "
" } \n "
" else \n "
" return true; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void checkComparisonOfFuncReturningBool2 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(){ \n "
" int temp = 4; \n "
" bool a = true; \n "
" if(compare(temp) > a){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" bool compare(int temp){ \n "
" if(temp==4){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void checkComparisonOfFuncReturningBool3 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(){ \n "
" int temp = 4; \n "
" if(compare(temp) > temp){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" bool compare(int temp){ \n "
" if(temp==4){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Comparison of a boolean expression with an integer. \n "
" [test.cpp:3]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
2014-11-20 14:20:09 +01:00
void checkComparisonOfFuncReturningBool4 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(){ \n "
" int temp = 4; \n "
" bool b = compare2(6); \n "
" if(compare1(temp)> b){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" bool compare1(int temp){ \n "
" if(temp==4){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } \n "
" bool compare2(int temp){ \n "
" if(temp == 5){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void checkComparisonOfFuncReturningBool5 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(){ \n "
" int temp = 4; \n "
" if(compare1(temp) > !compare2(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" bool compare1(int temp){ \n "
" if(temp==4){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } \n "
" bool compare2(int temp){ \n "
" if(temp==4){ \n "
" return false; \n "
" } \n "
" else \n "
" return true; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void checkComparisonOfFuncReturningBool6 ( ) {
2013-04-10 18:25:50 +02:00
check ( " int compare1(int temp); \n "
" namespace Foo { \n "
" bool compare1(int temp); \n "
" } \n "
" void f(){ \n "
" int temp = 4; \n "
" if(compare1(temp) > compare2(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " namespace Foo { \n "
" bool compare1(int temp); \n "
" } \n "
" int compare1(int temp); \n "
" void f(){ \n "
" int temp = 4; \n "
" if(compare1(temp) > compare2(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int compare1(int temp); \n "
" namespace Foo { \n "
" bool compare1(int temp); \n "
" void f(){ \n "
" int temp = 4; \n "
" if(compare1(temp) > compare2(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
check ( " int compare1(int temp); \n "
" namespace Foo { \n "
" bool compare1(int temp); \n "
" void f(){ \n "
" int temp = 4; \n "
" if(::compare1(temp) > compare2(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool compare1(int temp); \n "
" void f(){ \n "
" int temp = 4; \n "
" if(foo.compare1(temp) > compare2(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void checkComparisonOfBoolWithBool ( ) {
2013-04-10 18:25:50 +02:00
const char code [ ] = " void f(){ \n "
" int temp = 4; \n "
" bool b = compare2(6); \n "
" bool a = compare1(4); \n "
" if(b > a){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
" bool compare1(int temp){ \n "
" if(temp==4){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } \n "
" bool compare2(int temp){ \n "
" if(temp == 5){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } \n " ;
check ( code , true ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
check ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void bitwiseOnBoolean ( ) { // 3062
2013-04-10 18:25:50 +02:00
check ( " void f(_Bool a, _Bool b) { \n "
" if(a & b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
check ( " void f(_Bool a, _Bool b) { \n "
" if(a | b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
check ( " void f(bool a, bool b) { \n "
" if(a & !b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
check ( " void f(bool a, bool b) { \n "
" if(a | !b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
check ( " bool a, b; \n "
" void f() { \n "
" if(a & b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
check ( " bool a, b; \n "
" void f() { \n "
" if(a & !b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
check ( " bool a, b; \n "
" void f() { \n "
" if(a | b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
check ( " bool a, b; \n "
" void f() { \n "
" if(a | !b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
check ( " void f(bool a, int b) { \n "
" if(a & b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
check ( " void f(int a, bool b) { \n "
" if(a & b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean variable 'b' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
check ( " void f(int a, int b) { \n "
" if(a & b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(bool b) { \n "
" foo(bar, &b); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void incrementBoolean ( ) {
2013-04-10 18:25:50 +02:00
check ( " bool bValue = true; \n "
" void f() { bValue++; } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead. \n " , errout . str ( ) ) ;
check ( " void f(bool test){ \n "
" test++; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead. \n " , errout . str ( ) ) ;
check ( " void f(int test){ \n "
" test++; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolWithInt1 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(bool x) { \n "
" if (x < 10) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool x) { \n "
" if (10 >= x) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool x) { \n "
" if (x != 0) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(bool x) { \n " // #3356
" if (x == 1) { \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(bool x) { \n "
" if (x != 10) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool x) { \n "
" if (x == 10) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool x) { \n "
" if (x == 0) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " DensePropertyMap<int, true> visited; " ) ; // #4075
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolWithInt2 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(bool x, int y) { \n "
" if (x == y) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(int x, bool y) { \n "
" if (x == y) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool x, bool y) { \n "
" if (x == y) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(bool x, fooClass y) { \n "
" if (x == y) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolWithInt3 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(int y) { \n "
" if (y > false) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer. \n "
" [test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(int y) { \n "
" if (true == y) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool y) { \n "
" if (y == true) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(bool y) { \n "
" if (false < 5) { \n "
" printf( \" foo \" ); \n "
" } \n "
" } " ) ;
2016-02-05 15:48:51 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolWithInt4 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void f(int x) { \n "
" if (!x == 1) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolWithInt5 ( ) {
2013-04-10 18:25:50 +02:00
check ( " void SetVisible(int index, bool visible) { \n "
" bool (SciTEBase::*ischarforsel)(char ch); \n "
" if (visible != GetVisible(index)) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void comparisonOfBoolWithInt6 ( ) { // #4224 - integer is casted to bool
2013-04-10 18:25:50 +02:00
check ( " void SetVisible(bool b, int i) { \n "
" if (b == (bool)i) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2013-08-21 16:17:19 +02:00
2014-11-20 14:20:09 +01:00
void comparisonOfBoolWithInt7 ( ) { // #4846 - (!x==true)
2013-08-21 16:17:19 +02:00
check ( " void f(int x) { \n "
" if (!x == true) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2013-12-23 18:39:05 +01:00
2014-11-20 14:20:09 +01:00
void pointerArithBool1 ( ) { // #5126
2013-12-23 18:39:05 +01:00
check ( " void f(char *p) { \n "
" if (p+1){} \n "
" } " ) ;
2013-12-25 19:56:00 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour. \n " , errout . str ( ) ) ;
2013-12-23 18:39:05 +01:00
2015-03-12 20:07:48 +01:00
check ( " void f(char *p) { \n "
" do {} while (p+1); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour. \n " , errout . str ( ) ) ;
check ( " void f(char *p) { \n "
" while (p-1) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour. \n " , errout . str ( ) ) ;
check ( " void f(char *p) { \n "
" for (int i = 0; p+1; i++) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour. \n " , errout . str ( ) ) ;
2013-12-23 18:39:05 +01:00
check ( " void f(char *p) { \n "
" if (p && p+1){} \n "
" } " ) ;
2013-12-25 19:56:00 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour. \n " , errout . str ( ) ) ;
2015-03-12 20:07:48 +01:00
check ( " void f(char *p) { \n "
" if (p+2 || p) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour. \n " , errout . str ( ) ) ;
2013-12-23 18:39:05 +01:00
}
2013-04-10 18:25:50 +02:00
} ;
REGISTER_TEST ( TestBool )