coccinelle: misc: minmax: suppress patch generation for err returns
[linux-2.6-microblaze.git] / scripts / coccinelle / misc / minmax.cocci
1 // SPDX-License-Identifier: GPL-2.0-only
2 ///
3 /// Check for opencoded min(), max() implementations.
4 /// Generated patches sometimes require adding a cast to fix compile warning.
5 /// Warnings/patches scope intentionally limited to a function body.
6 ///
7 // Confidence: Medium
8 // Copyright: (C) 2021 Denis Efremov ISPRAS
9 // Options: --no-includes --include-headers
10 //
11 // Keywords: min, max
12 //
13
14
15 virtual report
16 virtual org
17 virtual context
18 virtual patch
19
20 @rmax depends on !patch@
21 identifier func;
22 expression x, y;
23 binary operator cmp = {>, >=};
24 position p;
25 @@
26
27 func(...)
28 {
29         <...
30 *       ((x) cmp@p (y) ? (x) : (y))
31         ...>
32 }
33
34 @rmaxif depends on !patch@
35 identifier func;
36 expression x, y;
37 expression max_val;
38 binary operator cmp = {>, >=};
39 position p;
40 @@
41
42 func(...)
43 {
44         <...
45 *       if ((x) cmp@p (y)) {
46 *               max_val = (x);
47 *       } else {
48 *               max_val = (y);
49 *       }
50         ...>
51 }
52
53 @rmin depends on !patch@
54 identifier func;
55 expression x, y;
56 binary operator cmp = {<, <=};
57 position p;
58 @@
59
60 func(...)
61 {
62         <...
63 *       ((x) cmp@p (y) ? (x) : (y))
64         ...>
65 }
66
67 @rminif depends on !patch@
68 identifier func;
69 expression x, y;
70 expression min_val;
71 binary operator cmp = {<, <=};
72 position p;
73 @@
74
75 func(...)
76 {
77         <...
78 *       if ((x) cmp@p (y)) {
79 *               min_val = (x);
80 *       } else {
81 *               min_val = (y);
82 *       }
83         ...>
84 }
85
86 @pmax depends on patch@
87 identifier func;
88 expression x, y;
89 binary operator cmp = {>=, >};
90 @@
91
92 func(...)
93 {
94         <...
95 -       ((x) cmp (y) ? (x) : (y))
96 +       max(x, y)
97         ...>
98 }
99
100 @pmaxif depends on patch@
101 identifier func;
102 expression x, y;
103 expression max_val;
104 binary operator cmp = {>=, >};
105 @@
106
107 func(...)
108 {
109         <...
110 -       if ((x) cmp (y)) {
111 -               max_val = x;
112 -       } else {
113 -               max_val = y;
114 -       }
115 +       max_val = max(x, y);
116         ...>
117 }
118
119 // Don't generate patches for errcode returns.
120 @errcode depends on patch@
121 position p;
122 identifier func;
123 expression x;
124 binary operator cmp = {<, <=};
125 @@
126
127 func(...)
128 {
129         <...
130         return ((x) cmp@p 0 ? (x) : 0);
131         ...>
132 }
133
134 @pmin depends on patch@
135 identifier func;
136 expression x, y;
137 binary operator cmp = {<=, <};
138 position p != errcode.p;
139 @@
140
141 func(...)
142 {
143         <...
144 -       ((x) cmp@p (y) ? (x) : (y))
145 +       min(x, y)
146         ...>
147 }
148
149 @pminif depends on patch@
150 identifier func;
151 expression x, y;
152 expression min_val;
153 binary operator cmp = {<=, <};
154 @@
155
156 func(...)
157 {
158         <...
159 -       if ((x) cmp (y)) {
160 -               min_val = x;
161 -       } else {
162 -               min_val = y;
163 -       }
164 +       min_val = min(x, y);
165         ...>
166 }
167
168 @script:python depends on report@
169 p << rmax.p;
170 @@
171
172 for p0 in p:
173         coccilib.report.print_report(p0, "WARNING opportunity for max()")
174
175 @script:python depends on org@
176 p << rmax.p;
177 @@
178
179 for p0 in p:
180         coccilib.org.print_todo(p0, "WARNING opportunity for max()")
181
182 @script:python depends on report@
183 p << rmaxif.p;
184 @@
185
186 for p0 in p:
187         coccilib.report.print_report(p0, "WARNING opportunity for max()")
188
189 @script:python depends on org@
190 p << rmaxif.p;
191 @@
192
193 for p0 in p:
194         coccilib.org.print_todo(p0, "WARNING opportunity for max()")
195
196 @script:python depends on report@
197 p << rmin.p;
198 @@
199
200 for p0 in p:
201         coccilib.report.print_report(p0, "WARNING opportunity for min()")
202
203 @script:python depends on org@
204 p << rmin.p;
205 @@
206
207 for p0 in p:
208         coccilib.org.print_todo(p0, "WARNING opportunity for min()")
209
210 @script:python depends on report@
211 p << rminif.p;
212 @@
213
214 for p0 in p:
215         coccilib.report.print_report(p0, "WARNING opportunity for min()")
216
217 @script:python depends on org@
218 p << rminif.p;
219 @@
220
221 for p0 in p:
222         coccilib.org.print_todo(p0, "WARNING opportunity for min()")