libcellml icon indicating copy to clipboard operation
libcellml copied to clipboard

Calls to std::weak_ptr<T>::lock() should always be checked

Open agarny opened this issue 2 years ago • 0 comments

Dealing with branch coverage, I have seen many instances of code like this:

auto sharedPtr = weakPtr.lock();

if (sharedPtr != nullptr) {
    ...
}

In 99.9% of cases, sharedPtr != nullptr is always true, which means that our branch coverage can never be 100% and that is completely fine.

However, after a very quick look at the overall codebase, I could see that there are cases where we do NOT check for sharedPtr != nullptr and that is (very) bad practice. So, if anything, we really ought to ensure that our whole codebase always check for sharedPtr != nullptr.

agarny avatar Jan 05 '23 21:01 agarny