ARMmbed / core-util

DEPRECATED: Mbed 3 utilities library
Other
12 stars 17 forks source link

CORE_UTIL_ASSERT can lead to unused variable warnings. #97

Closed bremoran closed 8 years ago

bremoran commented 8 years ago

Suppose that it is possible for a function to fail at the end of a void function An assert can be used to catch this, but it's not usable otherwise.

void foo(int i) {
    int rc = bar(i);
    assert (rc == 0);
}

Here, bar() must be called regardless of whether debug is enabled, but we can check whether rc is valid only in debug. Since bar() must be called, its result must be stored to a variable, which can never be used.

The definition of assert could be improved so that:

#define ASSERT(test)\
    if(test) {\
        assert(test);\
    }

Alternatively, it could be defined like this:

#define ASSERT(test)\
    do {\
        bool rc = (test);\
        (void) rc;\
        assert(rc);\
    } while(0)
bremoran commented 8 years ago

I'm not convinced this is possible without breaking expected assert semantics.