1///
2/// Remove unneeded variable used to store return value.
3///
4// Confidence: Moderate
5// Copyright: (C) 2012 Peter Senna Tschudin, INRIA/LIP6.  GPLv2.
6// URL: http://coccinelle.lip6.fr/
7// Comments: Comments on code can be deleted if near code that is removed.
8//           "when strict" can be removed to get more hits, but adds false
9//           positives
10
11virtual patch
12virtual report
13virtual context
14virtual org
15
16@depends on patch && !(file in "ext")@
17type T;
18constant C;
19identifier ret;
20@@
21- T ret = C;
22... when != ret
23    when strict
24return
25- ret
26+ C
27;
28
29@depends on context && !(file in "ext")@
30type T;
31constant C;
32identifier ret;
33@@
34* T ret = C;
35... when != ret
36    when strict
37* return ret;
38
39@r1 depends on (report || org) && !(file in "ext")@
40type T;
41constant C;
42identifier ret;
43position p1, p2;
44@@
45T ret@p1 = C;
46... when != ret
47    when strict
48return ret@p2;
49
50@script:python depends on report@
51p1 << r1.p1;
52p2 << r1.p2;
53C << r1.C;
54ret << r1.ret;
55@@
56coccilib.report.print_report(p1[0], "Unneeded variable: \"" + ret +
57			     "\". Return \"" + C + "\" on line "
58			     + p2[0].line)
59
60@script:python depends on org@
61p1 << r1.p1;
62p2 << r1.p2;
63C << r1.C;
64ret << r1.ret;
65@@
66cocci.print_main("unneeded \"" + ret + "\" variable", p1)
67cocci.print_sec("return " + C + " here", p2)
68