diff options
| -rw-r--r-- | scripts/coccinelle/misc/returnvar.cocci | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/scripts/coccinelle/misc/returnvar.cocci b/scripts/coccinelle/misc/returnvar.cocci new file mode 100644 index 000000000000..605955a91c44 --- /dev/null +++ b/scripts/coccinelle/misc/returnvar.cocci | |||
| @@ -0,0 +1,66 @@ | |||
| 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 | |||
| 12 | virtual patch | ||
| 13 | virtual report | ||
| 14 | virtual context | ||
| 15 | virtual org | ||
| 16 | |||
| 17 | @depends on patch@ | ||
| 18 | type T; | ||
| 19 | constant C; | ||
| 20 | identifier ret; | ||
| 21 | @@ | ||
| 22 | - T ret = C; | ||
| 23 | ... when != ret | ||
| 24 | when strict | ||
| 25 | return | ||
| 26 | - ret | ||
| 27 | + C | ||
| 28 | ; | ||
| 29 | |||
| 30 | @depends on context@ | ||
| 31 | type T; | ||
| 32 | constant C; | ||
| 33 | identifier ret; | ||
| 34 | @@ | ||
| 35 | * T ret = C; | ||
| 36 | ... when != ret | ||
| 37 | when strict | ||
| 38 | * return ret; | ||
| 39 | |||
| 40 | @r1 depends on report || org@ | ||
| 41 | type T; | ||
| 42 | constant C; | ||
| 43 | identifier ret; | ||
| 44 | position p1, p2; | ||
| 45 | @@ | ||
| 46 | T ret@p1 = C; | ||
| 47 | ... when != ret | ||
| 48 | when strict | ||
| 49 | return ret@p2; | ||
| 50 | |||
| 51 | @script:python depends on report@ | ||
| 52 | p1 << r1.p1; | ||
| 53 | p2 << r1.p2; | ||
| 54 | C << r1.C; | ||
| 55 | ret << r1.ret; | ||
| 56 | @@ | ||
| 57 | coccilib.report.print_report(p1[0], "Unneeded variable: \"" + ret + "\". Return \"" + C + "\" on line " + p2[0].line) | ||
| 58 | |||
| 59 | @script:python depends on org@ | ||
| 60 | p1 << r1.p1; | ||
| 61 | p2 << r1.p2; | ||
| 62 | C << r1.C; | ||
| 63 | ret << r1.ret; | ||
| 64 | @@ | ||
| 65 | cocci.print_main("unneeded \"" + ret + "\" variable", p1) | ||
| 66 | cocci.print_sec("return " + C + " here", p2) | ||
