aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/coccinelle/misc/simple_return.cocci
blob: e8b6313b116f57d1d27b66efac786efe92fcd6ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/// Simplify a trivial if-return sequence.  Possibly combine with a
/// preceding function call.
///
// Confidence: High
// Copyright: (C) 2014 Julia Lawall, INRIA/LIP6.  GPLv2.
// Copyright: (C) 2014 Gilles Muller, INRIA/LiP6.  GPLv2.
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options: --no-includes --include-headers

virtual patch
virtual context
virtual org
virtual report

@r depends on patch@
local idexpression e;
identifier i,f,fn;
@@

fn(...) { <...
- e@i =
+ return
    f(...);
-if (i != 0) return i;
-return 0;
...> }

@depends on patch@
identifier r.i;
type t;
@@

-t i;
 ... when != i

@depends on patch@
expression e;
@@

-if (e != 0)
   return e;
-return 0;

// -----------------------------------------------------------------------

@s1 depends on context || org || report@
local idexpression e;
identifier i,f,fn;
position p,p1,p2;
@@

fn(...) { <...
* e@i@p = f(...);
  if (\(i@p1 != 0\|i@p2 < 0\))
     return i;
  return 0;
...> }

@s2 depends on context || org || report forall@
identifier s1.i;
type t;
position q,s1.p;
expression e,f;
@@

* t i@q;
  ... when != i
  e@p = f(...);

@s3 depends on context || org || report@
expression e;
position p1!=s1.p1;
position p2!=s1.p2;
@@

*if (\(e@p1 != 0\|e@p2 < 0\))
   return e;
 return 0;

// -----------------------------------------------------------------------

@script:python depends on org@
p << s1.p;
p1 << s1.p1;
q << s2.q;
@@

cocci.print_main("decl",q)
cocci.print_secs("use",p)
cocci.include_match(False)

@script:python depends on org@
p << s1.p;
p2 << s1.p2;
q << s2.q;
@@

cocci.print_main("decl",q)
cocci.print_secs("use with questionable test",p)
cocci.include_match(False)

@script:python depends on org@
p << s1.p;
p1 << s1.p1;
@@

cocci.print_main("use",p)

@script:python depends on org@
p << s1.p;
p2 << s1.p2;
@@

cocci.print_main("use with questionable test",p)

@script:python depends on org@
p << s3.p1;
@@

cocci.print_main("test",p)

@script:python depends on org@
p << s3.p2;
@@

cocci.print_main("questionable test",p)

// -----------------------------------------------------------------------

@script:python depends on report@
p << s1.p;
p1 << s1.p1;
q << s2.q;
@@

msg = "WARNING: end returns can be simpified and declaration on line %s can be dropped" % (q[0].line)
coccilib.report.print_report(p[0],msg)
cocci.include_match(False)

@script:python depends on report@
p << s1.p;
p1 << s1.p1;
q << s2.q
;
@@

msg = "WARNING: end returns may be simpified if negative or 0 value and declaration on line %s can be dropped" % (q[0].line)
coccilib.report.print_report(p[0],msg)
cocci.include_match(False)

@script:python depends on report@
p << s1.p;
p1 << s1.p1;
@@

msg = "WARNING: end returns can be simpified"
coccilib.report.print_report(p[0],msg)

@script:python depends on report@
p << s1.p;
p2 << s1.p2;
@@

msg = "WARNING: end returns can be simpified if negative or 0 value"
coccilib.report.print_report(p[0],msg)

@script:python depends on report@
p << s3.p1;
@@

msg = "WARNING: end returns can be simpified"
coccilib.report.print_report(p[0],msg)

@script:python depends on report@
p << s3.p2;
@@

msg = "WARNING: end returns can be simpified if tested value is negative or 0"
coccilib.report.print_report(p[0],msg)