How to raise warning if return value is disregarded?

后端 未结 8 1010
北海茫月
北海茫月 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 03:48

    A static analyzer will be your best bet here. We use Coverity here, but there are free tools available that you can use as well.

    If you need a quick-and-dirty solution and you have a Linux-style shell handy, you can try something like:

    grep -rn "function_name" * | grep -v "="
    

    That will find every line that references the specified function but does not contain an "=". You can get a lot of false positives (and potentially some false negatives) but if you don't have a static analyzer it's a decent place to start.

提交回复
热议问题