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