cellml / libcellml

Repository for libCellML development.
https://libcellml.org
Apache License 2.0
16 stars 21 forks source link

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

Open agarny opened 1 year ago

agarny commented 1 year ago

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.