boolreturn.cocci 698 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SPDX-License-Identifier: GPL-2.0
  2. /// Return statements in functions returning bool should use
  3. /// true/false instead of 1/0.
  4. //
  5. // Confidence: High
  6. // Options: --no-includes --include-headers
  7. virtual patch
  8. virtual report
  9. virtual context
  10. @r1 depends on patch@
  11. identifier fn;
  12. typedef bool;
  13. symbol false;
  14. symbol true;
  15. @@
  16. bool fn ( ... )
  17. {
  18. <...
  19. return
  20. (
  21. - 0
  22. + false
  23. |
  24. - 1
  25. + true
  26. )
  27. ;
  28. ...>
  29. }
  30. @r2 depends on report || context@
  31. identifier fn;
  32. position p;
  33. @@
  34. bool fn ( ... )
  35. {
  36. <...
  37. return
  38. (
  39. * 0@p
  40. |
  41. * 1@p
  42. )
  43. ;
  44. ...>
  45. }
  46. @script:python depends on report@
  47. p << r2.p;
  48. fn << r2.fn;
  49. @@
  50. msg = "WARNING: return of 0/1 in function '%s' with return type bool" % fn
  51. coccilib.report.print_report(p[0], msg)