2013-04-10 18:25:50 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2023-01-28 10:16:34 +01:00
* Copyright ( C ) 2007 - 2023 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"
2022-01-27 19:03:20 +01:00
# include "errortypes.h"
2017-05-27 04:33:47 +02:00
# include "settings.h"
2023-01-27 08:18:32 +01:00
# include "fixture.h"
2017-05-27 04:33:47 +02:00
# include "tokenize.h"
2013-04-10 18:25:50 +02:00
2023-03-02 21:50:14 +01:00
# include <list>
2022-09-16 07:15:49 +02:00
# include <sstream> // IWYU pragma: keep
2013-04-10 18:25:50 +02:00
class TestBool : public TestFixture {
public :
2021-08-07 20:51:18 +02:00
TestBool ( ) : TestFixture ( " TestBool " ) { }
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
2022-02-10 23:02:24 +01:00
void run ( ) override {
2021-02-24 22:00:06 +01:00
settings . severity . enable ( Severity : : style ) ;
settings . severity . enable ( Severity : : warning ) ;
settings . certainty . enable ( Certainty : : inconclusive ) ;
2015-10-07 18:33:57 +02:00
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)
2019-06-27 07:48:44 +02:00
TEST_CASE ( comparisonOfBoolWithInt8 ) ; // #9165
2019-09-01 09:51:53 +02:00
TEST_CASE ( comparisonOfBoolWithInt9 ) ; // #9304
2022-09-06 23:11:39 +02:00
TEST_CASE ( comparisonOfBoolWithInt10 ) ; // #10935
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 ) ;
2022-05-11 08:09:32 +02:00
TEST_CASE ( checkComparisonOfFuncReturningBool7 ) ; // #7197
2022-05-11 11:30:02 +02:00
TEST_CASE ( checkComparisonOfFuncReturningBool8 ) ; // #4103
2020-05-19 07:53:54 +02:00
// Integration tests..
TEST_CASE ( checkComparisonOfFuncReturningBoolIntegrationTest1 ) ; // #7798 overloaded functions
2019-06-15 13:01:34 +02:00
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 ) ;
2018-11-01 11:08:16 +01:00
TEST_CASE ( returnNonBool ) ;
2019-05-14 08:56:28 +02:00
TEST_CASE ( returnNonBoolLambda ) ;
TEST_CASE ( returnNonBoolLogicalOp ) ;
TEST_CASE ( returnNonBoolClass ) ;
2013-04-10 18:25:50 +02:00
}
2021-11-29 07:34:39 +01:00
# define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
2022-05-10 18:25:13 +02:00
void check_ ( const char * file , int line , const char code [ ] , const char filename [ ] = " test.cpp " ) {
2013-04-10 18:25:50 +02:00
// Clear the error buffer..
errout . str ( " " ) ;
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
2021-11-29 07:34:39 +01:00
ASSERT_LOC ( tokenizer . tokenize ( istr , filename ) , file , line ) ;
2013-04-10 18:25:50 +02:00
// Check...
2023-01-29 17:23:03 +01:00
runChecks < CheckBool > ( & tokenizer , & settings , this ) ;
2013-04-10 18:25:50 +02:00
}
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
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 "
2022-05-10 18:25:13 +02:00
" } " , " test.c " ) ;
2015-05-31 22:40:13 +02:00
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 "
2022-05-10 18:25:13 +02:00
" } " , " test.c " ) ;
2015-06-05 23:44:03 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2016-02-05 19:46:47 +01:00
ASSERT_EQUALS ( " [test.cpp:6]: (style) Boolean value assigned to floating point variable. \n " , errout . str ( ) ) ;
2019-09-21 08:24:54 +02:00
check ( " struct S { \n "
" float* p[1]; \n "
" }; \n "
" void f() { \n "
" S s = {0}; \n "
" *s.p[0] = true; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-21 08:24:54 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x || 0x0f)==6) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x & 0x0f)==6) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((x | 0x0f)==6) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x)==3) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x) < 0) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } "
2021-08-07 20:51:18 +02:00
) ;
2020-06-27 08:13:22 +02: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) { \n "
" if ((5 && x) < 1) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if ((5 && x) > 1) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } "
2021-08-07 20:51:18 +02:00
) ;
2020-06-27 08:13:22 +02: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) { \n "
" if (0 < (5 && x)) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (0 > (5 && x)) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } "
2021-08-07 20:51:18 +02:00
) ;
2020-06-27 08:13:22 +02: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) { \n "
" if (1 > (5 && x)) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n "
" if (1 < (5 && x)) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } "
2021-08-07 20:51:18 +02:00
) ;
2020-06-27 08:13:22 +02: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 ) { \n "
" if ( x > false ) \n "
" a++; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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 "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-04-10 18:25:50 +02:00
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
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)) {} } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (warning) Comparison of a boolean expression with an integer. \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 "
" } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " " , 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 ( ) {
2019-12-04 16:12:10 +01:00
check ( " void leftOfComparison(){ \n "
2013-04-10 18:25:50 +02:00
" int temp = 4; \n "
" bool a = true; \n "
" if(compare(temp) > a){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
2019-12-04 16:12:10 +01:00
" void rightOfComparison(){ \n "
" int temp = 4; \n "
" bool a = true; \n "
" if(a < compare(temp)){ \n "
" printf( \" foo \" ); \n "
" } \n "
" } \n "
2013-04-10 18:25:50 +02:00
" bool compare(int temp){ \n "
" if(temp==4){ \n "
" return true; \n "
" } \n "
" else \n "
" return false; \n "
" } " ) ;
2019-12-04 16:12:10 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. \n "
" [test.cpp:11]: (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 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 "
2020-06-27 08:13:22 +02:00
" bool compare(int temp); " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. \n "
2016-02-05 15:48:51 +01:00
" [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 ( ) ) ;
}
2022-05-11 08:09:32 +02:00
void checkComparisonOfFuncReturningBool7 ( ) { // #7197
check ( " struct C { \n "
" bool isEmpty(); \n "
" }; \n "
" void f() { \n "
" C c1, c2; \n "
" if ((c1.isEmpty()) < (c2.isEmpty())) {} \n "
" if (!c1.isEmpty() < !!c2.isEmpty()) {} \n "
" if ((int)c1.isEmpty() < (int)c2.isEmpty()) {} \n "
" if (static_cast<int>(c1.isEmpty()) < static_cast<int>(c2.isEmpty())) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. \n "
" [test.cpp:7]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. \n "
" [test.cpp:8]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. \n "
" [test.cpp:9]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. \n " ,
errout . str ( ) ) ;
}
2022-05-11 11:30:02 +02:00
void checkComparisonOfFuncReturningBool8 ( ) { // #4103
// op: >
check ( " int main(void){ \n "
" bool a = true; \n "
" bool b = false; \n "
" if(b > a){ \n " // here warning should be displayed
" ; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
// op: <
check ( " int main(void){ \n "
" bool a = true; \n "
" bool b = false; \n "
" if(b < a){ \n " // here warning should be displayed
" ; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
// op: >=
check ( " int main(void){ \n "
" bool a = true; \n "
" bool b = false; \n "
" if(b >= a){ \n " // here warning should be displayed
" ; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
// op: <=
check ( " int main(void){ \n "
" bool a = true; \n "
" bool b = false; \n "
" if(b <= a){ \n " // here warning should be displayed
" ; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. \n " , errout . str ( ) ) ;
}
2020-05-19 07:53:54 +02:00
void checkComparisonOfFuncReturningBoolIntegrationTest1 ( ) { // #7798
check ( " bool eval(double *) { return false; } \n "
" double eval(char *) { return 1.0; } \n "
" int main(int argc, char *argv[]) \n "
" { \n "
" if ( eval(argv[1]) > eval(argv[2]) ) \n "
" return 1; \n "
" return 0; \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 " ;
2022-05-10 18:25:13 +02:00
check ( code ) ;
2013-04-10 18:25:50 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. \n " , 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 "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(_Bool a, _Bool b) { \n "
" if(a | b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool a, bool b) { \n "
" if(a & !b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool a, bool b) { \n "
" if(a | !b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " bool a, b; \n "
" void f() { \n "
" if(a & b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " bool a, b; \n "
" void f() { \n "
" if(a & !b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " bool a, b; \n "
" void f() { \n "
" if(a | b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " bool a, b; \n "
" void f() { \n "
" if(a | !b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(bool a, int b) { \n "
" if(a & b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
check ( " void f(int a, bool b) { \n "
" if(a & b) {} \n "
" } " ) ;
2019-10-06 09:57:31 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
check ( " void f(int a, int b) { \n "
" if((a > 0) & (b < 0)) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'a>0' is used in bitwise operation. Did you mean '&&'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
2021-09-19 15:20:57 +02:00
check ( " void f(bool a, int b) { \n "
" if(a | b) {} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression '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 expression 'b' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
check ( " int f(bool a, int b) { \n "
" return a | b; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(bool a, int b) { \n "
" return a | b; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
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 ( ) ) ;
2019-12-20 19:38:30 +01:00
check ( " void f(bool b) { \n " // #9405
" class C { void foo(bool &b) {} }; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2021-09-19 15:20:57 +02:00
check ( " bool f(); \n "
" bool g() { \n "
" return f() | f(); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-01-02 08:08:28 +01:00
check ( " uint8 outcode(float p) { \n "
" float d = 0.; \n "
" return ((p - xm >= d) << 1) | (x - p > d); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-06-14 23:08:17 +02:00
check ( " int g(); \n " // #10655
" void f(bool b) { \n "
" if (g() | b) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
check ( " int g(); \n "
" void f(bool b) { \n "
" if (b | g()) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int g(); \n "
" bool f(bool b, bool c) { \n "
" return b | g() | c; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean expression 'c' is used in bitwise operation. Did you mean '||'? \n " , errout . str ( ) ) ;
2022-07-11 22:58:37 +02:00
check ( " void f(int i) { \n " // #4233
" bool b = true, c = false; \n "
" b &= i; \n "
" c |= i; \n "
" if (b || c) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. \n "
" [test.cpp:4]: (style, inconclusive) Boolean expression 'c' is used in bitwise operation. \n " ,
errout . str ( ) ) ;
check ( " void f(int i, int j, bool b) { \n "
" i &= b; \n "
" j |= b; \n "
" if (b || c) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-07-14 20:59:39 +02:00
check ( " bool f(bool b, int i) { \n "
" b &= (i == 5); \n "
" return b; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
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 ( ) ) ;
2019-09-25 13:07:39 +02:00
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(bool* test){ \n "
" test[0]++; \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 ( ) ) ;
2013-04-10 18:25:50 +02:00
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 "
" } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " " , 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 "
" } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " " , 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 "
" } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " [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 "
" } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " " , 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
2019-06-27 07:48:44 +02:00
void comparisonOfBoolWithInt8 ( ) { // #9165
check ( " bool Fun(); \n "
" void Test(bool expectedResult) { \n "
" auto res = Fun(); \n "
" if (expectedResult == res) \n "
" throw 2; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int Fun(); \n "
" void Test(bool expectedResult) { \n "
" auto res = Fun(); \n "
" if (expectedResult == res) \n "
" throw 2; \n "
" } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-06-27 07:48:44 +02:00
check ( " bool Fun(); \n "
" void Test(bool expectedResult) { \n "
" auto res = Fun(); \n "
" if (5 + expectedResult == res) \n "
" throw 2; \n "
" } " ) ;
2020-06-27 08:13:22 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-06-27 07:48:44 +02:00
check ( " int Fun(); \n "
" void Test(bool expectedResult) { \n "
" auto res = Fun(); \n "
" if (5 + expectedResult == res) \n "
" throw 2; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int Fun(); \n "
" void Test(bool expectedResult) { \n "
" auto res = Fun(); \n "
" if (expectedResult == res + 5) \n "
" throw 2; \n "
" } " ) ;
2020-06-27 08:13:22 +02:00
TODO_ASSERT_EQUALS ( " error " , " " , errout . str ( ) ) ;
2019-06-27 07:48:44 +02:00
}
2019-09-01 09:51:53 +02:00
void comparisonOfBoolWithInt9 ( ) { // #9304
2019-09-02 06:59:07 +02:00
check ( " bool f(int a, bool b) \n "
" { \n "
" if ((a == 0 ? false : true) != b) { \n "
" b = !b; \n "
" } \n "
" return b; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-09-01 09:51:53 +02:00
}
2022-06-10 20:40:37 +02:00
void comparisonOfBoolWithInt10 ( ) { // #10935
check ( " enum class E { H = 2 }; \n "
" template <bool H> \n "
" void f(bool v) { \n "
" if (v == H) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " namespace N { \n "
" enum class E { H = 2 }; \n "
" } \n "
" void f(bool v) { \n "
" if (v == N::H) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-06-27 07:48:44 +02: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
}
2018-11-01 11:08:16 +01:00
void returnNonBool ( ) {
check ( " bool f(void) { \n "
" return 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" return 1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" return 2; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" return -1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" return 1 + 1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" int x = 0; \n "
" return x; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" int x = 10; \n "
" return x; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" return 2 < 1; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" int ret = 0; \n "
" if (a) \n "
" ret = 1; \n "
" return ret; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" int ret = 0; \n "
" if (a) \n "
" ret = 3; \n "
" return ret; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" if (a) \n "
" return 3; \n "
" return 4; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) Non-boolean value returned from function returning bool \n "
" [test.cpp:4]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
" return; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-05-14 08:56:28 +02:00
}
2018-11-01 11:08:16 +01:00
2019-05-14 08:56:28 +02:00
void returnNonBoolLambda ( ) {
2018-11-01 11:08:16 +01:00
check ( " bool f(void) { \n "
2019-05-14 08:56:28 +02:00
" auto x = [](void) { return -1; }; \n "
" return false; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-11-01 11:08:16 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
2019-05-14 08:56:28 +02:00
" auto x = [](void) { return -1; }; \n "
" return 2; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-11-01 11:08:16 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
2019-05-14 08:56:28 +02:00
" auto x = [](void) -> int { return -1; }; \n "
" return false; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-11-01 11:08:16 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(void) { \n "
2019-05-14 08:56:28 +02:00
" auto x = [](void) -> int { return -1; }; \n "
" return 2; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-11-01 11:08:16 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
2019-05-14 08:56:28 +02:00
}
2019-05-02 07:00:27 +02:00
2019-05-14 08:56:28 +02:00
void returnNonBoolLogicalOp ( ) {
2019-05-02 07:00:27 +02:00
check ( " bool f(int x) { \n "
" return x & 0x4; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(int x, int y) { \n "
" return x | y; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(int x) { \n "
" return (x & 0x2); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-11-01 11:08:16 +01:00
}
2019-05-14 08:56:28 +02:00
void returnNonBoolClass ( ) {
check ( " class X { \n "
" public: \n "
" bool f() { return -1;} \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
check ( " bool f() { \n "
" struct X { \n "
" public: \n "
" int f() { return -1;} \n "
" }; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f() { \n "
" class X { \n "
" public: \n "
" int f() { return -1;} \n "
" }; \n "
" return false; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f() { \n "
" class X { \n "
" public: \n "
" bool f() { return -1;} \n "
" }; \n "
" return -1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (style) Non-boolean value returned from function returning bool \n "
" [test.cpp:4]: (style) Non-boolean value returned from function returning bool \n " , errout . str ( ) ) ;
}
2013-04-10 18:25:50 +02:00
} ;
REGISTER_TEST ( TestBool )