[PSA] Swapping out `_or_null` with `_if_present`

I saw [llvm] Use dyn_cast_if_present instead of dyn_cast_or_null (NFC) by kazutakahirata · Pull Request #75733 · llvm/llvm-project · GitHub and wanted to leave a comment here.

I also favor *_or_null/isa_and_nonnull. null is more specific (and shorter) than present when dealing with pointers, which are no doubt the most used versions in the repository. I think there is value in accepting a specialized version (null) along with a generic one (if_present), like we still use push_back when emplace_back is available. The specialized version gives the reader more information (this call site is related to a pointer). If I see if_present, I’d think whether there is any underlying magic.

4 Likes