The compiler may remove the nullptr check in:
ptr->foo = 1;
if (ptr == nullptr)
return;
but it may not remove the nullptr check in: if (ptr == nullptr)
return;
ptr->foo = 1;