returnvar.cocci 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. virtual patch
  12. virtual report
  13. virtual context
  14. virtual org
  15. @depends on patch@
  16. type T;
  17. constant C;
  18. identifier ret;
  19. @@
  20. - T ret = C;
  21. ... when != ret
  22. when strict
  23. return
  24. - ret
  25. + C
  26. ;
  27. @depends on context@
  28. type T;
  29. constant C;
  30. identifier ret;
  31. @@
  32. * T ret = C;
  33. ... when != ret
  34. when strict
  35. * return ret;
  36. @r1 depends on report || org@
  37. type T;
  38. constant C;
  39. identifier ret;
  40. position p1, p2;
  41. @@
  42. T ret@p1 = C;
  43. ... when != ret
  44. when strict
  45. return ret@p2;
  46. @script:python depends on report@
  47. p1 << r1.p1;
  48. p2 << r1.p2;
  49. C << r1.C;
  50. ret << r1.ret;
  51. @@
  52. coccilib.report.print_report(p1[0], "Unneeded variable: \"" + ret + "\". Return \"" + C + "\" on line " + p2[0].line)
  53. @script:python depends on org@
  54. p1 << r1.p1;
  55. p2 << r1.p2;
  56. C << r1.C;
  57. ret << r1.ret;
  58. @@
  59. cocci.print_main("unneeded \"" + ret + "\" variable", p1)
  60. cocci.print_sec("return " + C + " here", p2)