David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /// Bool initializations should use true and false. Bool tests don't need |
| 3 | /// comparisons. Based on contributions from Joe Perches, Rusty Russell |
| 4 | /// and Bruce W Allan. |
| 5 | /// |
| 6 | // Confidence: High |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 7 | // Copyright: (C) 2012 Julia Lawall, INRIA/LIP6. |
| 8 | // Copyright: (C) 2012 Gilles Muller, INRIA/LiP6. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | // URL: http://coccinelle.lip6.fr/ |
| 10 | // Options: --include-headers |
| 11 | |
| 12 | virtual patch |
| 13 | virtual context |
| 14 | virtual org |
| 15 | virtual report |
| 16 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 17 | @boolok@ |
| 18 | symbol true,false; |
| 19 | @@ |
| 20 | ( |
| 21 | true |
| 22 | | |
| 23 | false |
| 24 | ) |
| 25 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 26 | @depends on patch@ |
| 27 | bool t; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | @@ |
| 29 | |
| 30 | ( |
| 31 | - t == true |
| 32 | + t |
| 33 | | |
| 34 | - true == t |
| 35 | + t |
| 36 | | |
| 37 | - t != true |
| 38 | + !t |
| 39 | | |
| 40 | - true != t |
| 41 | + !t |
| 42 | | |
| 43 | - t == false |
| 44 | + !t |
| 45 | | |
| 46 | - false == t |
| 47 | + !t |
| 48 | | |
| 49 | - t != false |
| 50 | + t |
| 51 | | |
| 52 | - false != t |
| 53 | + t |
| 54 | ) |
| 55 | |
| 56 | @depends on patch disable is_zero, isnt_zero@ |
| 57 | bool t; |
| 58 | @@ |
| 59 | |
| 60 | ( |
| 61 | - t == 1 |
| 62 | + t |
| 63 | | |
| 64 | - t != 1 |
| 65 | + !t |
| 66 | | |
| 67 | - t == 0 |
| 68 | + !t |
| 69 | | |
| 70 | - t != 0 |
| 71 | + t |
| 72 | ) |
| 73 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 74 | @depends on patch && boolok@ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | bool b; |
| 76 | @@ |
| 77 | ( |
| 78 | b = |
| 79 | - 0 |
| 80 | + false |
| 81 | | |
| 82 | b = |
| 83 | - 1 |
| 84 | + true |
| 85 | ) |
| 86 | |
| 87 | // --------------------------------------------------------------------- |
| 88 | |
| 89 | @r1 depends on !patch@ |
| 90 | bool t; |
| 91 | position p; |
| 92 | @@ |
| 93 | |
| 94 | ( |
| 95 | * t@p == true |
| 96 | | |
| 97 | * true == t@p |
| 98 | | |
| 99 | * t@p != true |
| 100 | | |
| 101 | * true != t@p |
| 102 | | |
| 103 | * t@p == false |
| 104 | | |
| 105 | * false == t@p |
| 106 | | |
| 107 | * t@p != false |
| 108 | | |
| 109 | * false != t@p |
| 110 | ) |
| 111 | |
| 112 | @r2 depends on !patch disable is_zero, isnt_zero@ |
| 113 | bool t; |
| 114 | position p; |
| 115 | @@ |
| 116 | |
| 117 | ( |
| 118 | * t@p == 1 |
| 119 | | |
| 120 | * t@p != 1 |
| 121 | | |
| 122 | * t@p == 0 |
| 123 | | |
| 124 | * t@p != 0 |
| 125 | ) |
| 126 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 127 | @r3 depends on !patch && boolok@ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 128 | bool b; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 129 | position p1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 130 | @@ |
| 131 | ( |
| 132 | *b@p1 = 0 |
| 133 | | |
| 134 | *b@p1 = 1 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 135 | ) |
| 136 | |
| 137 | @r4 depends on !patch@ |
| 138 | bool b; |
| 139 | position p2; |
| 140 | identifier i; |
| 141 | constant c != {0,1}; |
| 142 | @@ |
| 143 | ( |
| 144 | b = i |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 145 | | |
| 146 | *b@p2 = c |
| 147 | ) |
| 148 | |
| 149 | @script:python depends on org@ |
| 150 | p << r1.p; |
| 151 | @@ |
| 152 | |
| 153 | cocci.print_main("WARNING: Comparison to bool",p) |
| 154 | |
| 155 | @script:python depends on org@ |
| 156 | p << r2.p; |
| 157 | @@ |
| 158 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 159 | cocci.print_main("WARNING: Comparison of 0/1 to bool variable",p) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 160 | |
| 161 | @script:python depends on org@ |
| 162 | p1 << r3.p1; |
| 163 | @@ |
| 164 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 165 | cocci.print_main("WARNING: Assignment of 0/1 to bool variable",p1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 166 | |
| 167 | @script:python depends on org@ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 168 | p2 << r4.p2; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 169 | @@ |
| 170 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 171 | cocci.print_main("ERROR: Assignment of non-0/1 constant to bool variable",p2) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 172 | |
| 173 | @script:python depends on report@ |
| 174 | p << r1.p; |
| 175 | @@ |
| 176 | |
| 177 | coccilib.report.print_report(p[0],"WARNING: Comparison to bool") |
| 178 | |
| 179 | @script:python depends on report@ |
| 180 | p << r2.p; |
| 181 | @@ |
| 182 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 183 | coccilib.report.print_report(p[0],"WARNING: Comparison of 0/1 to bool variable") |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 184 | |
| 185 | @script:python depends on report@ |
| 186 | p1 << r3.p1; |
| 187 | @@ |
| 188 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 189 | coccilib.report.print_report(p1[0],"WARNING: Assignment of 0/1 to bool variable") |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 190 | |
| 191 | @script:python depends on report@ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 192 | p2 << r4.p2; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 193 | @@ |
| 194 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 195 | coccilib.report.print_report(p2[0],"ERROR: Assignment of non-0/1 constant to bool variable") |