File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed
test/query-tests/Likely Bugs/UseInOwnInitializer Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change @@ -27,4 +27,7 @@ where init.getDeclaration() = v
2727 va = mi .getExpr ( )
2828 )
2929 )
30+ and not (
31+ va .getEnclosingStmt ( ) .isInMacroExpansion ( )
32+ )
3033select va , v .getName ( ) + " is used in its own initializer."
Original file line number Diff line number Diff line change 11typedef long size_t ;
22
33void test1 () {
4- int x = x;
4+ int x = x; // BAD
55}
66
77void test2 () {
8- int x = x = 2 ;
8+ int x = x = 2 ; // BAD
99}
1010
1111void test3 () {
12- int x = 2 ;
12+ int x = 2 ; // GOOD
1313}
1414
1515void test4 () {
16- void * x = (void *)&x;
16+ void * x = (void *)&x; // GOOD
1717}
1818
1919void test5 () {
20- size_t x = sizeof (x);
20+ size_t x = sizeof (x); // GOOD
2121}
2222
2323typedef void *voidptr;
@@ -28,7 +28,7 @@ voidptr address_of(voidptr &var) {
2828}
2929
3030void test6 () {
31- voidptr x = address_of (x); // initialize to it's own address
31+ voidptr x = address_of (x); // GOOD (implicit conversion to reference)
3232}
3333
3434
@@ -38,25 +38,31 @@ struct MyString {
3838};
3939
4040void test7 () {
41- MyString ms = {ms.array , {0 }}; // initialize to ""
41+ MyString ms = {ms.array , {0 }}; // GOOD (implicit conversion to pointer)
4242}
4343
4444#define uninitialized (x ) x
4545
4646void test8 () {
47- int x = uninitialized (x);
47+ int x = uninitialized (x); // GOOD (rval is a macro)
4848}
4949
5050#define uninitialized2 (x ) x = x
5151
5252void test9 () {
53- int uninitialized2 (x);
53+ int uninitialized2 (x); // GOOD (initializer is a macro)
5454}
5555
5656void test10 () {
57- int x = x + 1 ;
57+ int x = x + 1 ; // BAD: x is evaluated on the right hand side
5858}
5959
6060void test11 () {
61- int x = uninitialized (x) + 1 ;
61+ int x = uninitialized (x) + 1 ; // BAD: x is evaluated on the right hand side
62+ }
63+
64+ #define self_initialize (t, x ) t x = x
65+
66+ void test12 () {
67+ self_initialize (int , x); // GOOD (statement is from a macro)
6268}
You can’t perform that action at this time.
0 commit comments