How to raise warning if return value is disregarded?

后端 未结 8 1008
北海茫月
北海茫月 2020-11-27 03:35

I\'d like to see all the places in my code (C++) which disregard return value of a function. How can I do it - with gcc or static code analysis tool?

Bad code exampl

8条回答
  •  情深已故
    2020-11-27 04:06

    As far as I'm aware there is no GCC option to give this warning. However, if you are interested in specific functions, you can tag them with an attribute:

    int fn() __attribute__((warn_unused_result));
    

    which would give a warning if the return value of fn() was not used. Caveat: I've never used this feature myself.

提交回复
热议问题