g++ How to get warning on ignoring function return value

前端 未结 5 1140
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 04:52

lint produces some warning like:

foo.c XXX Warning 534: Ignoring return value of function bar()

From the lint manual

5条回答
  •  忘掉有多难
    2020-11-30 05:14

    The answers about using __attribute__((warn_unused_result)) are correct. GCC isn't so good at this functionality, though! Be aware: it will not warn for non-POD types. That means, for example, if you return a class with a destructor (or a class with instance variables with destructors) you'll never see a warning about ignoring the result.

    Relevant bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66177

    Example where it fails:

    struct Error {
    ~Error();
    };
    
    __attribute__((warn_unused_result)) Error test();
    
    int main()
    {
        test();
        return 0;
    }
    

    So, don't rely on this for return types which aren't pretty simple.

提交回复
热议问题