xref: /linux/scripts/coccinelle/misc/returnvar.cocci (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1///
2/// Removes 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// Options: --no-includes --include-headers
11
12virtual patch
13virtual report
14virtual context
15virtual org
16
17@depends on patch@
18type T;
19constant C;
20identifier ret;
21@@
22- T ret = C;
23... when != ret
24    when strict
25return
26- ret
27+ C
28;
29
30@depends on context@
31type T;
32constant C;
33identifier ret;
34@@
35* T ret = C;
36... when != ret
37    when strict
38* return ret;
39
40@r1 depends on report || org@
41type T;
42constant C;
43identifier ret;
44position p1, p2;
45@@
46T ret@p1 = C;
47... when != ret
48    when strict
49return ret@p2;
50
51@script:python depends on report@
52p1 << r1.p1;
53p2 << r1.p2;
54C << r1.C;
55ret << r1.ret;
56@@
57coccilib.report.print_report(p1[0], "Unneeded variable: \"" + ret + "\". Return \"" + C + "\" on line " + p2[0].line)
58
59@script:python depends on org@
60p1 << r1.p1;
61p2 << r1.p2;
62C << r1.C;
63ret << r1.ret;
64@@
65cocci.print_main("unneeded \"" + ret + "\" variable", p1)
66cocci.print_sec("return " + C + " here", p2)
67