1// SPDX-License-Identifier: GPL-2.0-only
2///
3/// Remove unneeded variable used to store return value.
4///
5// Confidence: Moderate
6// Copyright: (C) 2012 Peter Senna Tschudin, INRIA/LIP6.
7// URL: https://coccinelle.gitlabpages.inria.fr/website
8// Comments: Comments on code can be deleted if near code that is removed.
9//           "when strict" can be removed to get more hits, but adds false
10//           positives
11// Options: --no-includes --include-headers
12
13virtual patch
14virtual report
15virtual context
16virtual org
17
18@depends on patch@
19type T;
20constant C;
21identifier ret;
22@@
23- T ret = C;
24... when != ret
25    when strict
26return
27- ret
28+ C
29;
30
31@depends on context@
32type T;
33constant C;
34identifier ret;
35@@
36* T ret = C;
37... when != ret
38    when strict
39* return ret;
40
41@r1 depends on report || org@
42type T;
43constant C;
44identifier ret;
45position p1, p2;
46@@
47T ret@p1 = C;
48... when != ret
49    when strict
50return ret@p2;
51
52@script:python depends on report@
53p1 << r1.p1;
54p2 << r1.p2;
55C << r1.C;
56ret << r1.ret;
57@@
58coccilib.report.print_report(p1[0], "Unneeded variable: \"" + ret + "\". Return \"" + C + "\" on line " + 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